-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
importer.py
executable file
·1987 lines (1587 loc) · 80.2 KB
/
importer.py
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# pylint: disable=missing-docstring, invalid-name, line-too-long, too-many-lines, fixme
# pylint: disable=too-few-public-methods
from __future__ import (absolute_import, division,
print_function, unicode_literals)
import argparse
import re
import operator
import collections
import sys
class UnexpectedException(Exception):
def __init__(self, message):
super().__init__(message)
def standarize_syntax_objdump(syntax):
"""Change instruction syntax to match Qualcomm's objdump output.
Args:
syntax (str): instruction syntax, probably as was obtained from the parsed manual.
Returns:
str: matching objdump syntax (as close as possible).
TODO:
* Care should be taken not to modify the syntax patterns used in the decoder
to recognize different attributes of the instruction, e.g., ``Rd`` can
be splitted with a space like ``R d``.
* Document the most complex regex.
"""
# Add spaces to certain chars like '=' and '()'
both_spaces = ['=', '+', '-', '*', '/', '&', '|', '<<', '^']
left_space = ['(', '!']
rigth_space = [')', ',']
for c in both_spaces:
syntax = syntax.replace(c, ' ' + c + ' ')
for c in left_space:
syntax = syntax.replace(c, ' ' + c)
for c in rigth_space:
syntax = syntax.replace(c, c + ' ')
syntax = re.sub(r'\s{2,}', ' ', syntax)
# TODO: Special hack for the unary minus.
syntax = re.sub(r'\#\s-\s', '#-', syntax)
syntax = re.sub(r'\(\s*', '(', syntax)
syntax = re.sub(r'\s*\)', ')', syntax)
# Compound assingment
syntax = re.sub(r'([\+\-\*\/\&\|\^\!]) =', r'\1=', syntax)
syntax = syntax.replace(' ,', ',')
syntax = syntax.replace(' .', '.')
# Remove parenthesis from (!p0.new). just to match objdump,
# but I prefer it with parenthesis.
if ';' not in syntax:
m = re.search(r'\( (\s* ! \s* [pP]\w(.new)? \s*) \)', syntax, re.X)
if m:
syntax = syntax.replace('(' + m.group(1) + ')', m.group(1))
# syntax = re.sub(r'\( (\s* ! \s* [pP]\w(.new)? \s*) \)', r'\1', syntax, re.X)
# TODO: The re.sub is not working, don't know why..
syntax = syntax.replace('dfcmp', 'cmp')
syntax = syntax.replace('sfcmp', 'cmp')
# Special cases: ++, ==, !=
syntax = syntax.replace('+ +', '++')
syntax = syntax.replace('= =', '==')
syntax = syntax.replace('! =', '!=')
# Special cases: <<N, <<1, <<16, >>1
syntax = syntax.replace(': << N', ':<<N')
syntax = syntax.replace(': << 1', ':<<1')
syntax = syntax.replace(': >> 1', ':>>1')
syntax = syntax.strip()
return syntax
class OperandTemplate():
# TODO: Document class.
__slots__ = ['syntax_name']
# TODO: Change `syntax_name` to ``syntax``.
def __init__(self, syntax_name):
self.syntax_name = syntax_name
class RegisterTemplate(OperandTemplate):
# TODO: Document class.
__slots__ = ['is_register_pair', 'is_predicate', 'is_control', 'is_system', 'is_newvalue', 'syntax', 'index']
def __init__(self, syntax_name):
super(RegisterTemplate, self).__init__(syntax_name)
self.syntax = syntax_name
self.is_register_pair = False
self.is_control = False
self.is_system = False
self.is_predicate = False
self.is_newvalue = False
self.index = 0
# Register pair analysis.
if self.syntax_name[0] == 'R':
# General register.
if len(self.syntax_name[1:]) == 2:
self.is_register_pair = True
if self.syntax_name[1] != self.syntax_name[2]:
# the two chars of the register pair do not match
raise UnexpectedException("The two chars of the register pair do not match:"
"'{:s}' and '{:s}'".format(self.syntax_name[1], self.syntax_name[2]))
if self.syntax_name[0] == 'P':
# Predicate
self.is_predicate = True
if self.syntax_name[0] == 'C':
# Control register
self.is_control = True
if len(self.syntax_name[1:]) == 2:
self.is_register_pair = True
if self.syntax_name[1] != self.syntax_name[2]:
# the two chars of the register pair do not match
raise UnexpectedException("The two chars of the register pair do not match:"
"'{:s}' and '{:s}'".format(self.syntax_name[1], self.syntax_name[2]))
if self.syntax_name[0] == 'S':
# System control register
self.is_system = True
if len(self.syntax_name[1:]) == 2:
self.is_register_pair = True
if self.syntax_name[1] != self.syntax_name[2]:
# the two chars of the register pair do not match
raise UnexpectedException("The two chars of the register pair do not match:"
"'{:s}' and '{:s}'".format(self.syntax_name[1], self.syntax_name[2]))
if self.syntax_name[0] == 'N':
# New value register
self.is_newvalue = True
# TODO: Check if the general purpose register is the only that uses reg. pairs.
# (the control reg is also possible as reg. pair but they are usually rreferencedby their alias)
class ImmediateTemplate(OperandTemplate):
# TODO: Document class. Develop the notion of immediate type, e.g., r, m, s, etc.
__slots__ = ['scaled', 'type', 'syntax', 'signed', 'index']
# TODO: Change `scaled` to ``scale`` (because it's used as an int, not a bool).
def __init__(self, syntax_name, scaled=0):
super(ImmediateTemplate, self).__init__(syntax_name)
self.syntax = syntax_name
self.scaled = scaled
self.signed = False
self.index = 0
self.type = self.syntax_name[1].lower()
if self.type == 's':
self.signed = True
if self.type not in ['s', 'u', 'm', 'r', 'g', 'n']:
raise UnexpectedException("Unknown immediate type: {:s}".format(self.type))
class OptionalTemplate(OperandTemplate):
__slots__ = ['syntax', 'index', 'syntax_pos']
def __init__(self, syntax_name):
super(OptionalTemplate, self).__init__(syntax_name)
self.syntax = syntax_name
self.index = 0
self.syntax_pos = 0
class EncodingField():
"""Hexagon instruction encoding field, as seen in the manual.
An encoding field can be characterized with only a mask (int) with the 1's set
to indicate the positions of the field chars in the encoding. E.g., in the encoding
(str) ``1011iiiiiiisssssPPiiiiiiiiiddddd``, the field ``s5`` would have a mask
(int) like ``0b00000000000111110000000000000000``.
This mask is used to later extract the value of the field in the instruction
being disassembled, which would be used to generate either an immediate or
register operand.
This value extraction (bit by bit) can be time consuming. To improve performance,
and taking advantage of the fact that most encoding fields are unified, (i.e.,
all their field chars have consecutive order, like the example above), other
(redundant) attributes are added to the class to reflect this.
If a field is unified (``no_mask_split`` is True), the field value can
be extracted just by applying a logical and operation, if the mask is split,
after the logical and, the extracted bits need to be joined (which is time consumig
for the disassembly process, as seen in the profiling results).
Attributes:
mask (int): resulting from setting to 1's the positions in the instruction encoding
where the corresponding field char appears.
mask_len (int): number of times the field char appears on the encoding field (i.e.,
number of 1's in the mask).
no_mask_split (bool): indicates whether all the field chars have a consecutive
bit ordering (i.e., if all the 1's in the mask are together).
mask_lower_pos (int): lowest bit index in the encoding where the field char is found
(i.e. position of the first 1 in the mask).
index (int): the number of operand
TODOs:
* Improve this class explanation, its use is in the disassembler, maybe move
some of the explanation there (to `extract_and_join_mask_bits` function).
* Clearly differentiate between bit-by-bit processing vs manipulating all
bits together (extract bits).
* Change `mask_lower_pos` to ``field_lower_pos`` or ``field_start_pos``.
* Change `no_mask_split` to ``mask_split`` and adjust logic, asking
for ``if not no_mask_split`` is too cumbersome.
"""
__slots__ = ['mask', 'mask_len', 'no_mask_split', 'mask_lower_pos', 'index']
def __init__(self):
self.mask = 0
self.mask_len = 0
# Used to determine the sign of immediates.
# TODO: Is mask_len used just for that?
self.index = 0
class TemplateBranch():
"""Hexagon instruction branch.
Attribute that adds information to the InstructionTemplate, used mainly
in the IDA processor module to perform branch analysis..
Attributes:
type (str): of branch, useful for IDA analysis.
target (OperandTemplate): operand template (register or immediate) in the instruction
that contains the target of the branch.
is_conditional (bool): True if conditional branch (there's an 'if' inside
the syntax); False otherwise.
TODOs:
* Define the branch type inside a class or enum or somewhere unified,
not as strings, and not inside the class.
* Comment on each branch type separately, explaining the difference.
* Change `all_branches` name to ``branch_syntax(es)``.
* Change `all_branches` name to ``branch_syntax(es)``.
* Document a branch as the union of hexagon jumps and calls.
* The branch syntax is used like a regexp pattern, the spaces (added for readability)
are ignored only if ``re.search`` is called with ``re.X`` argument
(e.g., as `analyze_branch` does), enforce/specify this.
* Once the branch types are unified give examples.
"""
__slots__ = ['target', 'is_conditional', 'type']
jump_reg_syntax = r'jumpr (?: :t | :nt)?' # ``?:`` don't capture group
jump_imm_syntax = jump_reg_syntax.replace('jumpr', 'jump')
call_reg_syntax = r'callr'
call_imm_syntax = call_reg_syntax.replace('callr', 'call')
dealloc_ret_syntax = r'dealloc_return'
all_branches = [jump_reg_syntax, jump_imm_syntax, call_reg_syntax, call_imm_syntax, dealloc_ret_syntax]
def __init__(self, type):
self.type = type
self.target = None
self.is_conditional = False
class TemplateToken():
"""Hexagon instruction template token.
Used mainly in the IDA processor module, to print some parts of the syntax (tokens)
in a special manner, matching the strings (`s`) with their corresponding operand (`op`).
Attributes:
s (str): token string.
op (Optional[OperandTemplate]): operand template (if any) that corresponds to the token.
TODOs:
* Change `s` name to something more descriptive, maybe also `op`,
using more than 2 letters is allowed...
"""
__slots__ = ['s', 'op']
def __init__(self, s):
self.s = s
self.op = None
class InstructionEncoding():
"""Hexagon instruction encoding.
Attributes:
text (str): encoding chars, without spaces, of len 32, each char represents one bit of the
instruction, e.g., the encoding of ``Rd=add(Rs,#s16)`` is ``1011iiiiiiisssssPPiiiiiiiiiddddd``,
``text[0]`` corresponds to bit 31 and ``text[31]`` to bit 0 (LSB) of the encoding.
mask (int): resulting from setting to 1's all the instruction defining bits, used in the
disassembly to determine the type of an instruction.
value (int): resulting from extracting only the instruction defining bits, used in conjunction with
the mask to determine the type of an instruction.
fields (Dict[str, EncodingField]): instruction encoding fields, indexed by the field char,
e.g. fields['d'] -> EncodingField(Rd).
TODOs:
* Change `text` attribute's name, so as not to be confused with an instruction text.
* Fields is a redundant attribute, because the encodings fields are contained
in the operands dict. key (of the instruction template),
but it's clearer this way. Should it be eliminated?
"""
__slots__ = ['text', 'value', 'mask', 'fields']
def __init__(self, text):
#if len(text) != 32:
# raise UnexpectedException('There has been a problem during the instruction definition import process.')
# TODO: Check also `text` for spaces.
# TODO: check that ICLASS bits (31:28 and 31:29,13 for duplex) in `text` are always defined to 0/1.
self.text = text
self.fields = {}
self.generate_mask_and_value()
self.generate_fields()
def generate_mask_and_value(self):
"""Generate the mask and value of the instruction encoding, from its text (str).
There are no Args nor Return values, everything is done manipulating the
object attributes: the input would be `self.text` and the output `self.mask`
and `self.value`.
"""
self.mask = 0
self.value = 0
for text_pos in range(32):
mask_pos = 31 - text_pos
# The orders of mask bits (int) and text bits (str) are reversed.
if self.text[text_pos] in ['0', '1']:
self.mask |= (1 << mask_pos)
self.value |= int(self.text[text_pos]) << mask_pos
def generate_fields(self):
"""Generate instruction fields of the instruction encoding, from its text (str).
Parse everything else that's not a instruction defining bit (0/1), like the ICLASS
bits, and generate the corresponding fields from each different spotted char.
The other type of chars ignored (besides 0/1) are '-' (irrelevant bit)
and 'P' (parse bit).
The fields created (EncodingField) do not differentiate between immediate or register,
they are just a bit field at this stage.
The generation of each field mask is pretty straight forward, but the process
has been complicated with the fact that the generated mask is checked to see if
bits are consecutive (no_mask_split), for performance reasons. See `EncodingField`
description.
There are no Args nor Return values, everything is done manipulating the
object attributes: the input would be `self.text` and the output `self.fields`.
TODOs:
* Rethink this explanation. 'P' is a valid field, but I'm skipping it because it
won't be a valid imm. o reg. operand. So even though this encoding fields
are just bits their ultimate use will be for operands.
* Use the terms "specific fields" (from "Instruction-specific fields") and
Common fields (defined in section 10.2 of the manual). ICLASS and parse
bits (common fields) are the ones I'm ignoring.
* The rationale behind the `no_mask_split` is split between here and
`EncodingField`. Unifiy.
* Avoid skipping any field here, create all the bit fields from the instruction,
and then skip them during reg./imm. ops. creation, to simplify the logic
here (less coupling, this function is doing -or knowing- more than it should).
"""
field_last_seen_pos = {} # type: Dict[str, int])
# Used to detect a mask split.
# TODO: Elaborate on this.
# TODO: XXX: add the index of operand for each field
for text_pos in range(32):
mask_pos = 31 - text_pos
# The orders of mask bits (int) and text bits (str) are reversed.
if mask_pos in [14, 15]: # skip 'P', parse bits
continue
# TODO: Remove this check when this function is permitted to parse all fields
# (and discard the P field later when generating the operands).
c = self.text[text_pos]
if c not in ['0', '1', '-']:
# TODO: Change to a continue clause, to remove all the following indentation.
if c not in self.fields:
# Char seen for the first time, create a new field.
self.fields[c] = EncodingField()
self.fields[c].no_mask_split = True
field_last_seen_pos[c] = (-1)
# (-1): used to indicate that it's a new field, and there's
# no last seen char before this one.
self.fields[c].mask |= (1 << mask_pos)
self.fields[c].mask_len += 1
# Detect a split in the field (and if so, reflect it on the mask).
if field_last_seen_pos[c] != -1:
if mask_pos != (field_last_seen_pos[c] - 1): # mask_pos iteration is going ackwards
self.fields[c].no_mask_split = False
field_last_seen_pos[c] = mask_pos
for c in self.fields:
self.fields[c].mask_lower_pos = field_last_seen_pos[c]
# The last seen position in the text (str) of each field is the
# lowest position in the mask (int), as their orders are reversed.
class InstructionDefinition():
"""Definition of an instruction (like the manual): syntax, encoding, and beahvior.
Instructions obtained by the importer (either from the manual or the objdump
headers). It has the minimal processing, only on the instruction encoding, converted
to `InstructionEncoding` (it has no use as a string), the major work is done in
the `InstructionTemplate` through the decoder.
The behavior attribute is optional, because the parser still doesn't support many of
the manual's behavior strings.
Attributes:
syntax (str)
encoding (InstructionEncoding)
behavior (str)
"""
__slots__ = ['syntax', 'encoding', 'behavior']
def __init__(self, syntax, encoding, behavior):
self.syntax = syntax
self.encoding = InstructionEncoding(encoding)
self.behavior = behavior
# TODO: Handle also TAB characters
class InstructionTemplate():
"""Definition of the instruction with the maximum processing done before being used for disassembly.
Created by the decoder from an `InstructionDefinition`.
All the major attributes of the instruction are processed and
stored here, e.g., operands, duplex, branches, tokens, etc.
Attributes:
encoding (InstructionEncoding): Hexagon instruction encoding, as seen in the manual.
syntax (str): Hexagon instruction syntax, as seen in the manual, e.g. ``Rd=add(Rs,#s16)``.
operands (Dict[str, InstructionOperand]): Operands (registers or immediates) indexed by their
respective field char, e.g., operands['d'] -> InstructionOperand(Rd).
mult_inst (bool): Has more than one atomic instruction, i.e., has a ';' in the syntax.
is_duplex (bool): Indicates if this is a duplex instruction.
imm_ops (List[ImmediateTemplate]): List of the instruction register operand templates.
reg_ops (List[RegisterTemplate]): List of the instruction immediate operand templates.
opt_ops (List[OptionalTemplate]): List of the instruction optional operand templates.
branch (Optional[TemplateBranch]): If not None, has the branch being performed by the
instruction, identified by the encoding analyzing the instruction syntax and not
its behavior (as it should).
behavior (str): Hexagon instruction behavior, as seen in the manual, e.g. ``Rd=Rs+#s;``.
imm_ext_op (Optional[ImmediateTemplate]): "Pointer" to the immediate operand that can
be extended in the instruction. It is just a hint for the disassembler, to let it
know what immediate operand can be the target of a constant extension. "Pointer"
here means that it has one of the imm. ops. in the `imm_ops` list.
tokens (List[TemplateToken]): List of strings representing the tokenized behavior, where
splits are done in the cases where part of the syntax can be linked to an operand,
see `HexagonInstructionDecoder.tokenize_syntax`.
name (str): Name.
"""
__slots__ = ['encoding', 'syntax', 'operands', 'mult_inst',
'is_duplex', 'imm_ops', 'reg_ops', 'opt_ops', 'branch', 'behavior',
'imm_ext_op', 'tokens', 'name']
register_operand_field_chars = ['t', 'd', 'x', 'u', 'e', 'y', 'v', 's']
# Seen on the manual
# Added from the objdump headers, but not in the manual
register_operand_field_chars.extend(['f', 'z'])
immediate_operand_field_chars = ['i', 'I']
other_field_chars = ['-', 'P', 'E', 'N']
# 'E' added from the objdump header encodings (not in the manual)
field_chars = register_operand_field_chars + \
immediate_operand_field_chars + \
other_field_chars
# TODO: move all field char definitions inside `generate_operand` or in `common.py`.
def __init__(self, inst_def):
#print(inst_def)
#if not isinstance(inst_def, InstructionTemplate):
# pass
self.encoding = inst_def.encoding
self.syntax = standarize_syntax_objdump(inst_def.syntax)
self.behavior = inst_def.behavior
# TODO: Create an ``InstructionField`` that groups these 3 attributes.
self.imm_ops = []
self.reg_ops = []
self.opt_ops = []
self.operands = {}
# Contains the same info as imm_ops + reg_ops, only used inside
# `generate_instruction_operands`.
# TODO: Remove this attribute.
self.branch = None
self.imm_ext_op = None
self.tokens = []
self.name = None
self.mult_inst = (';' in self.syntax)
self.is_duplex = (self.encoding.text[16:18] == '00')
# PP (parity bits) set to '00'
#print("is duplex? {0}".format(self.is_duplex))
for c in self.encoding.fields:
#print(c)
self.generate_operand(c)
# Calculate operand indexes
self.operand_calculate_indices()
# C: char, ie: inst encoding
def generate_operand(self, c):
"""Generate an operand from an instruction field.
Args:
c (str): Field char.
Returns:
None: the information is added to `reg_ops`/`imm_ops` and `operands`
of the same InstructionTemplate.
"""
#print("syntax = \"{0}\" char = \"{1}\"".format(self.syntax, c))
if c not in InstructionTemplate.field_chars:
print("Field char {:s} not recognized.".format(c))
raise UnexpectedException("Field char {:s} not recognized.".format(c))
if c in self.register_operand_field_chars:
reg = self.match_register_in_syntax(self.syntax, c)
if reg:
self.operands[c] = reg
self.reg_ops.append(reg)
return
print("not register operand match in syntax! [{0:s}]".format(c))
if c in self.immediate_operand_field_chars:
imm = self.match_immediate_char_in_syntax(self.syntax, c)
if imm:
self.operands[c] = imm
self.imm_ops.append(imm)
return
print("no immediate operand match in syntax! [{0:s}]".format(c))
# There is a pretty similar structure in both processings.
# TODO: Can this be abstracted to a more general function?
if c == 'N':
# 'N' operand, it indicates an optional behavior in the instruction (which doesn't happen often).
m = re.search(r"(\[\s*:\s*<<\s*N\s*\])", self.syntax)
if m:
opt = OptionalTemplate('[:<<N]')
self.operands[c] = opt
self.operands[c].syntax_pos = (m.start(1), m.end(1))
self.opt_ops.append(opt)
return
print("no optional operand match in syntax!")
# If it gets here there's an unforeseen field char that was not processed correctly.
print("Field char {:s} not processed correctly.".format(c))
raise UnexpectedException("Field char {:s} not processed correctly.".format(c))
def match_register_in_syntax(self, syntax, reg_char):
"""Find a register operand in the syntax with a specified field char.
Args:
syntax (str): Instruction syntax.
reg_char (str): Field char (str of len 1) used in the instruction encoding to
represent a field that holds the value for a register operand.
Returns:
Optional[RegisterTemplate]: if found, None otherwise.
TODO:
* Check other possible registers, Mx for example.
"""
# Match registers, first generic ones (Rx), then predicates (Px)
reg_templates = [
r"(R" + reg_char + r"{1,2})",
# {1,2}: it can be a double register (e.g. Rdd).
r"(P" + reg_char + r")",
r"(N" + reg_char + r".new)",
r"(M" + reg_char + r")",
r"(C" + reg_char + r")",
# Added from the objdump headers, but not in the manual.
r"(G" + reg_char + r")",
r"(S" + reg_char + r"{1,2})", # Can be double register too
]
for rt in reg_templates: # type: str
m = re.search(rt, syntax)
if m:
return RegisterTemplate(m.group(1))
return None
def match_immediate_char_in_syntax(self, syntax, imm_char):
"""Find an immediate operand in the syntax with a specified field char.
Args:
syntax (str): Instruction syntax.
imm_char (str): Field char (str of len 1) used in the instruction encoding to
represent a field that holds the value for an immediate operand.
Returns:
Optional[ImmediateTemplate]: if found, None otherwise.
"""
if imm_char == 'i':
imm_chars = ['u', 's', 'm', 'r', 'g']
elif imm_char == 'I':
imm_chars = ['U', 'S', 'M', 'R', 'G']
# TODO: Use list comprehensions.
else:
raise UnexpectedException("Unexpected syntax specifier")
for ic in imm_chars: # type: str
m = re.search(r"(#" + ic + r"\d{1,2})" + r"(:\d)?", syntax)
# E.g., ``#s16:2``, the optional ``:2`` indicates a scaled immediate.
# E.g., ``#g16:0`` indicates GP-relative offset
# TODO: Improve readabilty of this regex.
if m:
imm_syntax = m.group(1)
scale_factor = 0
if m.group(2):
imm_syntax += m.group(2)
scale_factor = int(m.group(2)[1])
# ``[1]``: used to skip the ':' in the syntax.
return ImmediateTemplate(imm_syntax, scale_factor)
return None
def operand_calculate_indices(self):
pos = {}
i = ic = rc = oc = 0
for k,v in self.operands.items():
pos[k] = self.syntax.find(v.syntax)
sortedpos = sorted(pos.items(), key=operator.itemgetter(1))
for c,p in sortedpos:
self.operands[c].index = i
# Update imm_ops and reg_ops order too. They were added to imm_ops/reg_ops in
# encoding order, not in syntax order (by generate_operands()) this broke at least
# two instructions.
#
# Syntax-order: Rd = mux(Pu, #s8, #S8)
# Order: [d, u, s, S]; Reg. order: [d, u]; Imm. order: [s, S]
#
# Encoding-order: 0111101uuIIIIIIIPPIiiiiiiiiddddd
# Order: [u, I, i, d]; reg. order: [u, d]; imm. order: [I, i]
# (I = S, s = i etc.)
if isinstance(self.operands[c], ImmediateTemplate):
self.imm_ops[ic] = self.operands[c]
ic += 1
elif isinstance(self.operands[c], RegisterTemplate):
self.reg_ops[rc] = self.operands[c]
rc += 1
elif isinstance(self.operands[c], OptionalTemplate):
self.opt_ops[oc] = self.operands[c]
oc += 1
else:
print("Operand template: {}".format(self.operands[c]))
raise UnexpextectedException("Unknow operands template")
i += 1
class HexagonInstructionDecoder():
"""Hexagon instruction decoder.
Takes instruction definitions and process them to instruction templates.
Attributes:
inst_def_list (List[InstructionDefintion]): List of instruction definitions saved during the parsing stage.
inst_template_list (List[InstructionTemplate]): List of instruction definitions templates generated
by the decoder from the list of definitions.
"""
__slots__ = ['inst_def_list', 'inst_template_list']
def __init__(self, inst_def_list):
"""Load the instruction definitions and convert it to instruction templates.
Creates the InstructionTemplate and processes it.
TODOs:
* All the calls in the loop could be done inside the InstructionTemplate
constructor, should it?
"""
self.inst_def_list = inst_def_list
self.inst_template_list = [InstructionTemplate(inst_def) for inst_def in self.inst_def_list]
for template in self.inst_template_list:
self.analyze_branch(template)
self.resolve_constant_extender(template)
self.tokenize_syntax(template)
def tokenize_syntax(self, template):
"""Generate a list of tokens from the instruction syntax.
Takes the syntax string and split it in smaller strings (tokens). The split is
done to generate a link between the instruction operands and the substrings
that correspond to it, e.g., ``Rd=add(Rs,#s16)`` would be splitted like:
``['Rd', '=add(', 'Rs', ',', '#s16', ')']`` to isolate the three operand strings
(registers ``Rd``, ``Rs`` and immediate ``#s16``) from the rest of the
syntax string.
The substrings are later used to generate TemplateToken objects, which are composed
of a string with its associated operand (if it exists).
Args:
template (InstructionTemplate): to be processed.
Returns:
None: the data is applied to the template itself.
TODOs:
* Should the 2 steps (split and match) be done together?
"""
tokens = [template.syntax] # type: List[str]
# The syntax will be splitted to this list of strings that will be later
# used to create the template tokens.
for op in template.reg_ops + template.imm_ops + template.opt_ops: # type: InstructionOperand
new_tokens = [] # type: List[str]
# New tokens generated from the current tokens, updated at the end of the loop.
# HACK: mask the '[' and ']' characters
reop = op.syntax_name.replace('[', '\[')
for str_token in tokens:
new_tokens.extend(
re.split('(' + reop + ')', str_token)
)
# If a operand is found in the current token, split it to isolate
# the operand, re.split is used because, unlike string.split, it doesn't
# discard the separator (the operator name in this case) when enclosed
# in parenthesis.
if len(new_tokens) != len(tokens) + 2 * template.syntax.count(op.syntax_name):
raise UnexpectedException("Tokens count doesn't match the syntax")
# Every split (appearance of the operand in the syntax)
# has to generate 2 new tokens (an old token is split into 3,
# the separator and left/right tokens, that are always generated
# even if they are empty strings).
tokens = new_tokens
# TODO: use list comprehensions and eliminate `new_tokens`.
# Discard possibly empty generated strings.
tokens = list(filter(lambda s: len(s) > 0, tokens))
# Generate list of TemplateToken and match string tokens to operands.
for str_token in tokens:
#template_token = TemplateToken(str_token.lower())
template_token = TemplateToken(str_token)
# TODO: Is it ok to convert to lowercase here?
# The letter case of the operands text is useful (specially in IDA) to
# identify them quickly in the visual analysis (from the rest of the instruction).
for op in template.reg_ops + template.imm_ops + template.opt_ops: # type: InstructionOperand
if str_token == op.syntax_name:
# The string token names the operand, match them.
template_token.op = op
break
template.tokens.append(template_token)
return
def resolve_constant_extender(self, template):
"""In case there are two imm. operands, indicate to which one would apply a constant extension.
This is done for instructions that can be extended by a constant but have two
immediate operands and it has to be indicated to which one the extension applies.
The function ``apply_extension()`` in instruction behaviours is used as an indication
that a constant extension can be applied, and the argument of the function specifies
the syntax of which immediate operand it applies to.
Args:
template (InstructionTemplate): to be processed.
Returns:
None: the data is applied to the template itself.
TODOs:
* Add to the function description an example of an instruction where
there are two imm. ops. and the ``apply_extension()`` resolves which one.
"""
if len(template.imm_ops) < 2:
# There's no need to perform the check, there's (at most) only one
# immediate operand to choose from.
if template.imm_ops:
template.imm_ext_op = template.imm_ops[0]
return
m = re.search(r"""
# Looking for something like: "apply_extension(...);"
apply_extension
\(
(.*?) # Capture group for the imm. op. name, e.g., ``#s``.
\)
""", template.behavior.replace(' ', ''), re.X)
# The spaces are removed from the behavior string to simplify the regex.
if m is None:
# No constant extension found in the behavior.
# But it has immediates -> assume imm_ops[0]
template.imm_ext_op = template.imm_ops[0]
return
imm_op_ext_name = m.group(1)
# Name of the imm. op. that is the argument of ``apply_extension()``.
for imm_op in template.imm_ops:
if imm_op_ext_name in imm_op.syntax_name:
# An equal comparison is not made in the previous if because
# the op. name in the apply_extension argument is usually a shorter
# version of the name in the syntax (normally because the
# operand's bit size was removed), e.g., ``#s16`` in
# ``Rd=add(Rs,#s16)`` is referenced as ``apply_extension(#s);``.
template.imm_ext_op = imm_op
return
raise UnexpectedException("Cannot parse constant extender")
# If the regex matched, the operand should have been found in the previous loop.
def analyze_branch(self, template):
"""Find a branch in the instruction syntax and generate the template info.
Used in (IDA) static analysis.
Args:
template (InstructionTemplate): to be processed.
Returns:
None: the data is applied to the template itself.
TODOs:
* Change function name to something like 'find_branch(es)'.
* This type of analysis should be done by studying the REIL translation
of the instruction, which truly reflects its behaviour. When the REIL
translation is added this function should be adapted.
* Multiple branches in one instruction: is it possible? I think not,
at most, two branches in one packet but separate. Check this.
* The branch string itself is used to represent it, maybe some constants
should be used instead.
"""
for branch_syntax in TemplateBranch.all_branches: # type: str
# Find any of the possible branch syntaxes in the instruction
# to detect a branch.
m = re.search(branch_syntax, template.syntax, re.X)
if m is None:
continue
if branch_syntax == TemplateBranch.dealloc_ret_syntax:
# The instruction is a 'dealloc_return', a jump to the
# LR as target.
pass
template.branch = TemplateBranch(branch_syntax)
template.branch.is_conditional = ('if' in template.syntax)
# TODO: The if could be applying to another sub-instruction. Improve detection.
if branch_syntax in [TemplateBranch.jump_reg_syntax, TemplateBranch.call_reg_syntax]:
# Branch type: jump/call register.
# Find which register is the target of the branch.
for reg in template.reg_ops: # type: RegisterTemplate
m = re.search(branch_syntax + r'\s*' + reg.syntax_name, template.syntax, re.X)
if m:
template.branch.target = reg
return
# The target register operand was not found, this shouldn't happen, but
# for now the case of register alias (specially the case of LR) is not
# being handled, so an exception can't be raised, and this case is
# tolerated (retuning instead).
# raise UnexpectedException()
return
if branch_syntax in [TemplateBranch.jump_imm_syntax, TemplateBranch.call_imm_syntax]:
# Branch type: jump/call immediate.
for imm in template.imm_ops: # type: ImmediateTemplate
m = re.search(branch_syntax + r'\s*' + imm.syntax_name.replace('#', r'\#'), template.syntax, re.X)
# The '#' (used in imm. op. names) is escaped, as it is interpreted as
# a comment in verbose regex (re.X), and verbose regex is used because
# the branch syntax is written with spaces (verbose style) to improve
# its readability.
if m:
template.branch.target = imm
return
raise UnexpectedException("Cannot find target immediate operand")
# The target immediate operand should have been found.
return
class ManualParser:
def __init__(self, manual_fn):
self.manual = open(manual_fn, 'r', newline=None) # universal newlines, to get rid of '\r' when opening in linux
self.lines = self.manual.read().splitlines()
self.ln = 0
self.current_line = self.lines[self.ln]