Debian Codegraph @

include/fcml_disassembler.hpp

raw
1 /*
2 * FCML - Free Code Manipulation Library.
3 * Copyright (C) 2010-2020 Slawomir Wojtasiak
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /** @file fcml_disassembler.hpp
21 * C++ wrapper for the FCML disassembler.
22 *
23 * @copyright Copyright (C) 2010-2020 Slawomir Wojtasiak. All rights reserved.
24 * This project is released under the GNU Lesser General Public License.
25 */
26
27 #ifndef FCML_DISASSEMBLER_HPP_
28 #define FCML_DISASSEMBLER_HPP_
29
30 #include <string.h>
31
32 #include "fcml_disassembler.h"
33
34 #include "fcml_common.hpp"
35 #include "fcml_errors.hpp"
36 #include "fcml_dialect.hpp"
37
38 namespace fcml {
39
40 /**
41 * Component can not be initialized correctly.
42 * @since 1.1.0
43 */
44 class DisassemblingFailedException: public ErrorContainerAwareException {
45 public:
46 DisassemblingFailedException(const fcml_cstring &msg,
47 const ErrorContainer &errorContainer,
48 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR) :
49 ErrorContainerAwareException(msg, errorContainer, error) {
50 }
51 };
52
53 /** Disassembler configuration.
54 *
55 * It's counterpart to the fcml_st_disassembler_conf structure.
56 * @since 1.1.0
57 */
58 class DisassemblerConf {
59 public:
60
61 /**
62 * Default constructor.
63 * @since 1.1.0
64 */
65 DisassemblerConf() :
66 _throwExceptionOnError(true),
67 _incrementIP(true),
68 _enableErrorMessages(true),
69 _carryFlagConditionalSuffix(false),
70 _conditionalGroup(false),
71 _shortForms(false),
72 _extendDispToASA(false),
73 _failIfUnknownInstruction(false) {
74 }
75
76 /** @since 1.1.0 */
77 bool isCarryFlagConditionalSuffix() const {
78 return _carryFlagConditionalSuffix;
79 }
80
81 /** @since 1.1.0 */
82 void setCarryFlagConditionalSuffix(bool carryFlagConditionalSuffix) {
83 _carryFlagConditionalSuffix = carryFlagConditionalSuffix;
84 }
85
86 /** @since 1.1.0 */
87 fcml_uint8_t getConditionalGroup() const {
88 return _conditionalGroup;
89 }
90
91 /** @since 1.1.0 */
92 void setConditionalGroup(fcml_uint8_t conditionalGroup) {
93 _conditionalGroup = conditionalGroup;
94 }
95
96 /** @since 1.1.0 */
97 bool isEnableErrorMessages() const {
98 return _enableErrorMessages;
99 }
100
101 /** @since 1.1.0 */
102 void setEnableErrorMessages(bool enableErrorMessages) {
103 _enableErrorMessages = enableErrorMessages;
104 }
105
106 /** @since 1.1.0 */
107 bool isExtendDispToAsa() const {
108 return _extendDispToASA;
109 }
110
111 /** @since 1.1.0 */
112 void setExtendDispToAsa(bool extendDispToAsa) {
113 _extendDispToASA = extendDispToAsa;
114 }
115
116 /** @since 1.1.0 */
117 bool isFailIfUnknownInstruction() const {
118 return _failIfUnknownInstruction;
119 }
120
121 /** @since 1.1.0 */
122 void setFailIfUnknownInstruction(bool failIfUnknownInstruction) {
123 _failIfUnknownInstruction = failIfUnknownInstruction;
124 }
125
126 /** @since 1.1.0 */
127 bool isIncrementIp() const {
128 return _incrementIP;
129 }
130
131 /** @since 1.1.0 */
132 void setIncrementIp(bool incrementIp) {
133 _incrementIP = incrementIp;
134 }
135
136 /** @since 1.1.0 */
137 bool isShortForms() const {
138 return _shortForms;
139 }
140
141 /** @since 1.1.0 */
142 void setShortForms(bool shortForms) {
143 _shortForms = shortForms;
144 }
145
146 /**
147 * Returns true if exception should be thrown when disassembling fails.
148 *
149 * @return True if exception is the preferred way of error
150 * handling in case of failure.
151 * @since 1.1.0
152 */
153 bool isThrowExceptionOnError() const {
154 return _throwExceptionOnError;
155 }
156
157 /**
158 * Sets the way how the error handling is done.
159 *
160 * @param throwExceptionOnError True if exception should be thrown
161 * in case of failure.
162 * @since 1.1.0
163 */
164 void setThrowExceptionOnError(bool throwExceptionOnError) {
165 _throwExceptionOnError = throwExceptionOnError;
166 }
167
168 private:
169 bool _throwExceptionOnError;
170 bool _incrementIP;
171 bool _enableErrorMessages;
172 bool _carryFlagConditionalSuffix;
173 fcml_uint8_t _conditionalGroup;
174 bool _shortForms;
175 bool _extendDispToASA;
176 bool _failIfUnknownInstruction;
177
178 };
179
180 /** Disassembler context.
181 * @since 1.1.0
182 */
183 class DisassemblerContext {
184 public:
185
186 /**
187 * Creates empty disassembler context.
188 * @since 1.1.0
189 */
190 DisassemblerContext() :
191 _code(NULL), _codeLength(0) {
192 }
193
194 /**
195 * Creates disassembler context for given piece of machine code.
196 *
197 * @param code A buffer with the machine code.
198 * @param codeLength Size of the buffer in bytes.
199 * @since 1.1.0
200 */
201 DisassemblerContext(fcml_ptr code, fcml_usize codeLength) :
202 _code(code), _codeLength(codeLength) {
203 }
204
205 public:
206
207 /**
208 * Gets pointer to the machine code buffer.
209 *
210 * @return A pointer to the machine code.
211 * @since 1.1.0
212 */
213 fcml_ptr getCode() const {
214 return _code;
215 }
216
217 /**
218 * Sets a new buffer with machine code for the context.
219 *
220 * @param code The new code buffer.
221 * @since 1.1.0
222 */
223 void setCode(fcml_ptr code) {
224 _code = code;
225 }
226
227 /**
228 * Gets length of the buffer in bytes.
229 *
230 * @return Size of the buffer.
231 * @since 1.1.0
232 */
233 fcml_usize getCodeLength() const {
234 return _codeLength;
235 }
236
237 /**
238 * Sets length of the code buffer in bytes.
239 *
240 * @param codeLength Size of the code.
241 * @since 1.1.0
242 */
243 void setCodeLength(fcml_usize codeLength) {
244 _codeLength = codeLength;
245 }
246
247 /**
248 * Gets a reference to the configuration object associated with the context.
249 *
250 * @return The disassembler configuration.
251 * @since 1.1.0
252 */
253 const DisassemblerConf& getDisassemblerConf() const {
254 return _disassemblerConf;
255 }
256
257 /**
258 * Gets a reference to the configuration object associated with the context.
259 *
260 * @return The disassembler configuration.
261 * @since 1.1.0
262 */
263 DisassemblerConf& getDisassemblerConf() {
264 return _disassemblerConf;
265 }
266
267 /**
268 * Sets a new disassembler configuration for the context.
269 *
270 * @param disassemblerConf The new disassembler configuration.
271 * @since 1.1.0
272 */
273 void setDisassemblerConf(DisassemblerConf disassemblerConf) {
274 _disassemblerConf = disassemblerConf;
275 }
276
277 /**
278 * Gets reference to the constant entry point instance
279 * associated with the context.
280 *
281 * @return Reference to the constant entry point.
282 * @since 1.1.0
283 */
284 const EntryPoint& getEntryPoint() const {
285 return _entryPoint;
286 }
287
288 /**
289 * Gets reference to the entry point instance associated with the context.
290 *
291 * @return Reference to the entry point.
292 * @since 1.1.0
293 */
294 EntryPoint& getEntryPoint() {
295 return _entryPoint;
296 }
297 /**
298 * Copies given entry point to the instance associated with the context.
299 * Deep copy is performed, so passed object not need to be maintained as
300 * long as the context is used.
301 *
302 * @param entryPoint The entry point which is copied to the context.
303 * @since 1.1.0
304 */
305 void setEntryPoint(const EntryPoint &entryPoint) {
306 _entryPoint = entryPoint;
307 }
308
309 /**
310 * Sets instruction pointer directly into the entry point.
311 *
312 * @param ip The new IP.
313 * @since 1.1.0
314 */
315 void setIP(fcml_ip ip) {
316 _entryPoint.setIP(ip);
317 }
318
319 /**
320 * Increments entry point by given number of bytes.
321 *
322 * @param ip Number of bytes the instruction pointer has to be
323 * incremented by.
324 * @since 1.1.0
325 */
326 void incrementIP(fcml_ip ip) {
327 _entryPoint.incrementIP(ip);
328 }
329
330 /**
331 * Sets processor operating mode directly into the entry point.
332 *
333 * @param operatingMode Processor operating mode to be set.
334 * @since 1.1.0
335 */
336 void setOperatingMode(EntryPoint::OperatingMode operatingMode) {
337 _entryPoint.setOpMode(operatingMode);
338 }
339
340 /**
341 * Sets a new address size attribute for the entry point.
342 *
343 * @param addressSizeAttribute The address size attribute.
344 * @since 1.1.0
345 */
346 void setAddressSizeAttribute(fcml_usize addressSizeAttribute) {
347 _entryPoint.setAddressSizeAttribute(addressSizeAttribute);
348 }
349
350 /**
351 * Sets a new operand size attribute for the entry point.
352 *
353 * @param operandSizeAttribute The operand size attribute.
354 * @since 1.1.0
355 */
356 void setOperandSizeAttribute(fcml_usize operandSizeAttribute) {
357 _entryPoint.setOperandSizeAttribute(operandSizeAttribute);
358 }
359
360 private:
361 /** Disassembler configuration. */
362 DisassemblerConf _disassemblerConf;
363 /** Entry point. */
364 EntryPoint _entryPoint;
365 /** Code buffer. */
366 fcml_ptr _code;
367 /** Code length. */
368 fcml_usize _codeLength;
369 };
370
371 /** Instruction prefix.
372 * @since 1.1.0
373 */
374 class InstructionPrefixDetails {
375 public:
376
377 /** Type of the instruction prefix.
378 * @since 1.1.0
379 */
380 enum PrefixType {
381 PT_GROUP_UNKNOWN = FCML_PT_GROUP_UNKNOWN,
382 PT_GROUP_1 = FCML_PT_GROUP_1,
383 PT_GROUP_2 = FCML_PT_GROUP_2,
384 PT_GROUP_3 = FCML_PT_GROUP_3,
385 PT_GROUP_4 = FCML_PT_GROUP_4,
386 PT_REX = FCML_PT_REX,
387 PT_VEX = FCML_PT_VEX,
388 PT_XOP = FCML_PT_XOP,
389 PT_EVEX = FCML_PT_EVEX
390 };
391
392 /**
393 * Returns true if it's a mandatory prefix.
394 *
395 * @return True if it's a mandatory prefix.
396 * @since 1.1.0
397 */
398 bool isMandatoryPrefix() const {
399 return _mandatoryPrefix;
400 }
401
402 /**
403 * Sets mandatory prefix flag for the prefix.
404 *
405 * @param mandatoryPrefix Set to true in order to make the
406 * prefix mandatory one.
407 * @since 1.1.0
408 */
409 void setMandatoryPrefix(bool mandatoryPrefix) {
410 _mandatoryPrefix = mandatoryPrefix;
411 }
412
413 /**
414 * Gets the prefix byte.
415 *
416 * @return The prefix byte.
417 * @since 1.1.0
418 */
419 fcml_uint8_t getPrefix() const {
420 return _prefix;
421 }
422
423 /**
424 * Sets the prefix byte.
425 *
426 * @param prefix The prefix byte.
427 * @since 1.1.0
428 */
429 void setPrefix(fcml_uint8_t prefix) {
430 _prefix = prefix;
431 }
432
433 /**
434 * Gets the prefix type.
435 *
436 * @return The prefix byte.
437 * @since 1.1.0
438 */
439 PrefixType getPrefixType() const {
440 return _prefixType;
441 }
442
443 /**
444 * Sets a new prefix type.
445 *
446 * @param prefixType The prefix byte to be set.
447 * @since 1.1.0
448 */
449 void setPrefixType(PrefixType prefixType) {
450 _prefixType = prefixType;
451 }
452
453 /**
454 * Gets the second and third bytes of the XOP/VEX prefix.
455 *
456 * @return A pointer to the VEX/XOP bytes.
457 * @since 1.2.0
458 */
459 const fcml_uint8_t* getAvxBytes() const {
460 return _AvxBytes;
461 }
462
463 /**
464 * Gets the second and third bytes of the XOP/VEX prefix.
465 *
466 * @return A pointer to the VEX/XOP bytes.
467 * @since 1.2.0
468 */
469 fcml_uint8_t* getAvxBytes() {
470 return _AvxBytes;
471 }
472
473 private:
474 /** The first byte of the prefix. */
475 fcml_uint8_t _prefix;
476 /** Tye type of the prefix. */
477 PrefixType _prefixType;
478 /** True if the prefix is a mandatory one. */
479 bool _mandatoryPrefix;
480 /** The second and third byte of the AVX prefix.
481 * since 1.2.0
482 */
483 fcml_uint8_t _AvxBytes[3];
484 };
485
486 /** Prefixes details.
487 * @since 1.1.0
488 */
489 class PrefixesDetails {
490 public:
491
492 /**
493 * Default constructor.
494 * @since 1.1.0
495 */
496 PrefixesDetails() :
497 _prefixesCount(0),
498 _prefixesBytesCount(0),
499 _isBranch(false),
500 _isNobranch(false),
501 _isLock(false),
502 _isRep(false),
503 _isRepne(false),
504 _isXrelease(false),
505 _isXacquire(false),
506 _isVex(false),
507 _isEvex(false),
508 _isXop(false),
509 _isAvx(false),
510 _isRex(false),
511 _AvxPrefixFirstByte(false),
512 _R(0),
513 _RPrim(0),
514 _X(0),
515 _B(0),
516 _W(0),
517 _L(0),
518 _LPrim(0),
519 _mmmm(0),
520 _vvvv(0),
521 _VPrim(0),
522 _pp(0),
523 _aaa(0),
524 _b(false),
525 _z(false) {
526 }
527
528 /**
529 * Gets reference to the instruction prefix at given index.
530 * @param index Number of the prefix.
531 * @return The instruction prefix for given index.
532 * @throw IllegalArgumentException Index out of bound.
533 * @since 1.1.0
534 */
535 const InstructionPrefixDetails& operator[](fcml_usize index) const {
536 if (index >= FCML_DASM_PREFIXES_COUNT) {
537 throw IllegalArgumentException(FCML_TEXT("Index out of bound."));
538 }
539 return _prefixes[index];
540 }
541
542 /**
543 * Gets reference to the instruction prefix at given index.
544 * @param index Number of the prefix.
545 * @return The instruction prefix for given index.
546 * @throw IllegalArgumentException Index out of bound.
547 * @since 1.1.0
548 */
549 InstructionPrefixDetails& operator[](fcml_usize index) {
550 if (index >= FCML_DASM_PREFIXES_COUNT) {
551 throw IllegalArgumentException(FCML_TEXT("Index out of bound."));
552 }
553 return _prefixes[index];
554 }
555
556 /**
557 * Gets b flag.
558 *
559 * @return The B flag.
560 * @since 1.1.0
561 */
562 fcml_uint8_t getB() const {
563 return _B;
564 }
565
566 /**
567 * Sets B flag.
568 *
569 * @param b The B flag.
570 * @since 1.1.0
571 */
572 void setB(fcml_uint8_t B) {
573 _B = B;
574 }
575
576 /**
577 * Gets true if branch prefix is available.
578 *
579 * @return True if branch prefix is available.
580 * @since 1.1.0
581 */
582 bool isBranch() const {
583 return _isBranch;
584 }
585
586 /**
587 * Sets branch prefix availability.
588 *
589 * @param isBranch True if branch prefix is available.
590 * @since 1.1.0
591 */
592 void setBranch(bool isBranch) {
593 _isBranch = isBranch;
594 }
595
596 /**
597 * Gets true if lock prefix is available.
598 *
599 * @return True if lock prefix is available.
600 * @since 1.1.0
601 */
602 bool isLock() const {
603 return _isLock;
604 }
605
606 /**
607 * Sets lock prefix availability.
608 *
609 * @param isLock True if lock prefix is available.
610 * @since 1.1.0
611 */
612 void setLock(bool isLock) {
613 _isLock = isLock;
614 }
615
616 /**
617 * Gets true if no-branch prefix is available.
618 *
619 * @return True if no-branch prefix is available.
620 * @since 1.1.0
621 */
622 bool isNobranch() const {
623 return _isNobranch;
624 }
625
626 /**
627 * Sets no-branch prefix availability.
628 *
629 * @param isNobranch True if no-branch prefix is available.
630 * @since 1.1.0
631 */
632 void setNobranch(bool isNobranch) {
633 _isNobranch = isNobranch;
634 }
635
636 /**
637 * Gets true if Rep prefix is available.
638 *
639 * @return True if Rep prefix is available.
640 * @since 1.1.0
641 */
642 bool isRep() const {
643 return _isRep;
644 }
645
646 /**
647 * Sets Rep prefix availability.
648 *
649 * @param isRep True if Rep prefix is available.
650 * @since 1.1.0
651 */
652 void setRep(bool isRep) {
653 _isRep = isRep;
654 }
655
656 /**
657 * Gets true if Repne prefix is available.
658 *
659 * @return True if Repne prefix is available.
660 * @since 1.1.0
661 */
662 bool isRepne() const {
663 return _isRepne;
664 }
665
666 /**
667 * Sets Repne prefix availability.
668 *
669 * @param isRepne True if branch prefix is available.
670 * @since 1.1.0
671 */
672 void setRepne(bool isRepne) {
673 _isRepne = isRepne;
674 }
675
676 /**
677 * Gets true if Rex prefix is available.
678 *
679 * @return True if Rex prefix is available.
680 * @since 1.1.0
681 */
682 bool isRex() const {
683 return _isRex;
684 }
685
686 /**
687 * Sets REX prefix availability.
688 *
689 * @param isRex True if REX prefix is available.
690 * @since 1.1.0
691 */
692 void setRex(bool isRex) {
693 _isRex = isRex;
694 }
695
696 /**
697 * Gets true if Vex prefix is available.
698 *
699 * @return True if Vex prefix is available.
700 * @since 1.1.0
701 */
702 bool isVex() const {
703 return _isVex;
704 }
705
706 /**
707 * Sets VEX prefix availability.
708 *
709 * @param isVex True if VEX prefix is available.
710 * @since 1.1.0
711 */
712 void setVex(bool isVex) {
713 _isVex = isVex;
714 }
715
716 /**
717 * Sets EVEX prefix availability.
718 *
719 * @param isEvex True if EVEX prefix is available.
720 * @since 1.2.0
721 */
722 void setEvex(bool isEvex) {
723 _isEvex = isEvex;
724 }
725
726 /**
727 * Gets true if EVEX prefix is available.
728 *
729 * @return True if EVEX prefix is available.
730 * @since 1.2.0
731 */
732 bool isEvex() const {
733 return _isEvex;
734 }
735
736 /**
737 * Gets true if xacquire prefix is available.
738 *
739 * @return True if xacquire prefix is available.
740 * @since 1.1.0
741 */
742 bool isXacquire() const {
743 return _isXacquire;
744 }
745
746 /**
747 * Sets xacquire prefix availability.
748 *
749 * @param isXacquire True if xacquire prefix is available.
750 * @since 1.1.0
751 */
752 void setXacquire(bool isXacquire) {
753 _isXacquire = isXacquire;
754 }
755
756 /**
757 * Gets true if XOP prefix is available.
758 *
759 * @return True if XOP prefix is available.
760 * @since 1.1.0
761 */
762 bool isXop() const {
763 return _isXop;
764 }
765
766 /**
767 * Sets XOP prefix availability.
768 *
769 * @param isXop True if XOP prefix is available.
770 * @since 1.1.0
771 */
772 void setXop(bool isXop) {
773 _isXop = isXop;
774 }
775
776 /**
777 * Gets true if any AVX prefix is available.
778 *
779 * @return True if any AVX prefix is available.
780 * @since 1.2.0
781 */
782 bool isAvx() const {
783 return _isAvx;
784 }
785
786 /**
787 * Sets XOP prefix availability.
788 *
789 * @param isXop True if XOP prefix is available.
790 * @since 1.2.0
791 */
792 void setAvx(bool isAvx) {
793 _isAvx = isAvx;
794 }
795
796 /**
797 * Gets true if xrelease prefix is available.
798 *
799 * @return True if xrelease prefix is available.
800 * @since 1.1.0
801 */
802 bool isXrelease() const {
803 return _isXrelease;
804 }
805
806 /**
807 * Sets xrelease prefix availability.
808 *
809 * @param isXrelease True if xrelease prefix is available.
810 * @since 1.1.0
811 */
812 void setXrelease(bool isXrelease) {
813 _isXrelease = isXrelease;
814 }
815
816 /**
817 * Gets L flag.
818 *
819 * @return The L flag.
820 * @since 1.1.0
821 */
822 fcml_uint8_t getL() const {
823 return _L;
824 }
825
826 /**
827 * Sets L flag.
828 *
829 * @param l The L flag.
830 * @since 1.1.0
831 */
832 void setL(fcml_uint8_t L) {
833 _L = L;
834 }
835
836 /**
837 * Gets L' flag.
838 *
839 * @return The L' flag.
840 * @since 1.2.0
841 */
842 fcml_uint8_t getLPrim() const {
843 return _LPrim;
844 }
845
846 /**
847 * Sets L' flag.
848 *
849 * @param l The L' flag.
850 * @since 1.2.0
851 */
852 void setLPrim(fcml_uint8_t lPrim) {
853 _LPrim = lPrim;
854 }
855
856 /**
857 * Gets MMMM field.
858 *
859 * @return MMMM field.
860 * @since 1.1.0
861 */
862 fcml_uint8_t getMmmm() const {
863 return _mmmm;
864 }
865
866 /**
867 * Sets MMMM field.
868 *
869 * @param mmmm MMMM field.
870 * @since 1.1.0
871 */
872 void setMmmm(fcml_uint8_t mmmm) {
873 _mmmm = mmmm;
874 }
875
876 /**
877 * Gets PP field.
878 *
879 * @return The PP field.
880 * @since 1.1.0
881 */
882 fcml_uint8_t getPp() const {
883 return _pp;
884 }
885
886 /**
887 * Sets PP field.
888 *
889 * @param pp PP field.
890 * @since 1.1.0
891 */
892 void setPp(fcml_uint8_t pp) {
893 _pp = pp;
894 }
895
896 /**
897 * Gets a reference to the prefix of the given index.
898 *
899 * @param index Index for the prefix.
900 * @return The prefix at given index.
901 * @throw BadArgumentException Array index out of bound.
902 * @since 1.1.0
903 */
904 const InstructionPrefixDetails& getPrefixes(fcml_usize index) const {
905 if (index > FCML_DASM_PREFIXES_COUNT) {
906 throw BadArgumentException(FCML_TEXT("Array index out of bound."),
907 FCML_CEH_GEC_VALUE_OUT_OF_RANGE);
908 }
909 return _prefixes[index];
910 }
911
912 /**
913 * Gets a reference to the prefix of the given index.
914 *
915 * @param index Index for the prefix.
916 * @return The prefix at given index.
917 * @throw BadArgumentException Array index out of bound.
918 * @since 1.1.0
919 */
920 InstructionPrefixDetails& getPrefixes(fcml_usize index) {
921 if (index > FCML_DASM_PREFIXES_COUNT) {
922 throw BadArgumentException(FCML_TEXT("Array index out of bound."),
923 FCML_CEH_GEC_VALUE_OUT_OF_RANGE);
924 }
925 return _prefixes[index];
926 }
927
928 /**
929 * Gets number of bytes interpreted to be prefixes.
930 *
931 * @return Number of prefixes bytes.
932 * @since 1.1.0
933 */
934 fcml_int getPrefixesBytesCount() const {
935 return _prefixesBytesCount;
936 }
937
938 /**
939 * Sets number of prefixes bytes available for the instruction.
940 *
941 * @param prefixesBytesCount Number of the prefixes bytes.
942 * @since 1.1.0
943 */
944 void setPrefixesBytesCount(fcml_int prefixesBytesCount) {
945 _prefixesBytesCount = prefixesBytesCount;
946 }
947
948 /**
949 * Gets number of prefixes available for the instruction.
950 *
951 * @return Number of the prefixes for the instruction.
952 * @since 1.1.0
953 */
954 fcml_int getPrefixesCount() const {
955 return _prefixesCount;
956 }
957
958 /**
959 * Sets number of prefixes available for the instruction.
960 *
961 * @param prefixesCount Number of the available prefixes.
962 * @since 1.1.0
963 */
964 void setPrefixesCount(fcml_int prefixesCount) {
965 _prefixesCount = prefixesCount;
966 }
967
968 /**
969 * Gets R flag.
970 *
971 * @return The R flag.
972 * @since 1.1.0
973 */
974 fcml_uint8_t getR() const {
975 return _R;
976 }
977
978 /**
979 * Sets R flag.
980 *
981 * @param r The R flag.
982 * @since 1.1.0
983 */
984 void setR(fcml_uint8_t r) {
985 _R = r;
986 }
987
988 /**
989 * Gets R' flag.
990 *
991 * @return The R' flag.
992 * @since 1.2.0
993 */
994 fcml_uint8_t getRPrim() const {
995 return _RPrim;
996 }
997
998 /**
999 * Sets R' flag.
1000 *
1001 * @param rPrim The R' flag.
1002 * @since 1.2.0
1003 */
1004 void setRPrim(fcml_uint8_t rPrim) {
1005 _RPrim = rPrim;
1006 }
1007
1008 /**
1009 * Gets the first byte of the AVX prefix.
1010 *
1011 * @return The first byte of the AVX prefix.
1012 * @since 1.2.0
1013 */
1014 fcml_uint8_t getAvxFirstByte() const {
1015 return _AvxPrefixFirstByte;
1016 }
1017
1018 /**
1019 * Sets a first byte of the XOP/VEX prefix.
1020 *
1021 * @param avxFirstByte The first AVX prefix byte.
1022 * @since 1.2.0
1023 */
1024 void setAvxFirstByte(fcml_uint8_t avxFirstByte) {
1025 _AvxPrefixFirstByte = avxFirstByte;
1026 }
1027
1028 /**
1029 * Gets VVVV field of the XOP/VEX prefix.
1030 *
1031 * @return VVVV field of the XOP/VEX prefix.
1032 * @since 1.1.0
1033 */
1034 fcml_uint8_t getVvvv() const {
1035 return _vvvv;
1036 }
1037
1038 /**
1039 * Sets VVVV field of the XOP/VEX prefix.
1040 *
1041 * @param vvvv The VVVV field.
1042 * @since 1.1.0
1043 */
1044 void setVvvv(fcml_uint8_t vvvv) {
1045 _vvvv = vvvv;
1046 }
1047
1048 /**
1049 * Gets 'aaa' field of the EVEX prefix.
1050 *
1051 * @return aaa field of the EVEX prefix.
1052 * @since 1.2.0
1053 */
1054 fcml_uint8_t getAaa() const {
1055 return _aaa;
1056 }
1057
1058 /**
1059 * Sets 'aaa' field of the EVEX prefix.
1060 *
1061 * @param aaa The 'aaa' field.
1062 * @since 1.1.0
1063 */
1064 void setAaa(fcml_uint8_t aaa) {
1065 _aaa = aaa;
1066 }
1067
1068 /**
1069 * Gets V' flag.
1070 *
1071 * @return The V' flag.
1072 * @since 1.2.0
1073 */
1074 fcml_uint8_t getVPrim() const {
1075 return _VPrim;
1076 }
1077
1078 /**
1079 * Sets V' flag.
1080 *
1081 * @param vPrim The V' flag.
1082 * @since 1.2.0
1083 */
1084 void setVPrim(fcml_uint8_t vPrim) {
1085 _VPrim = vPrim;
1086 }
1087
1088 /**
1089 * Gets W flag.
1090 *
1091 * @return The W flag.
1092 * @since 1.1.0
1093 */
1094 fcml_uint8_t getW() const {
1095 return _W;
1096 }
1097
1098 /**
1099 * Sets W flag.
1100 *
1101 * @param w The W flag.
1102 * @since 1.1.0
1103 */
1104 void setW(fcml_uint8_t W) {
1105 _W = W;
1106 }
1107
1108 /**
1109 * Gets X flag.
1110 *
1111 * @return The X flag.
1112 * @since 1.1.0
1113 */
1114 fcml_uint8_t getX() const {
1115 return _X;
1116 }
1117
1118 /**
1119 * Sets X flag.
1120 *
1121 * @param x The X flag.
1122 * @since 1.1.0
1123 */
1124 void setX(fcml_uint8_t X) {
1125 _X = X;
1126 }
1127
1128 /**
1129 * Gets EVEX.b bit.
1130 *
1131 * @return The EVEX.b bit.
1132 * @since 1.2.0
1133 */
1134 bool getBcast() const {
1135 return _b;
1136 }
1137
1138 /**
1139 * Sets EVEX.b bit.
1140 *
1141 * @param b The EVEX.b bit.
1142 * @since 1.2.0
1143 */
1144 void setBcast(bool b) {
1145 _b = b;
1146 }
1147
1148 /**
1149 * Gets EVEX.z bit.
1150 *
1151 * @return The EVEX.z bit.
1152 * @since 1.2.0
1153 */
1154 bool getZ() const {
1155 return _z;
1156 }
1157
1158 /**
1159 * Sets EVEX.z bit.
1160 *
1161 * @param z The EVEX.z bit.
1162 * @since 1.2.0
1163 */
1164 void setZ(bool z) {
1165 _z = z;
1166 }
1167
1168 private:
1169 /** Array with decoded prefixes. */
1170 InstructionPrefixDetails _prefixes[FCML_DASM_PREFIXES_COUNT];
1171 /** Number of decoded prefixes. */
1172 fcml_int _prefixesCount;
1173 /** Number of bytes used by all decoded prefixes. */
1174 fcml_int _prefixesBytesCount;
1175 /** FCML_TRUE if branch prefix exists. */
1176 bool _isBranch;
1177 /** FCML_TRUE if nobranch prefix exists. */
1178 bool _isNobranch;
1179 /** FCML_TRUE if lock explicit prefix exists. */
1180 bool _isLock;
1181 /** FCML_TRUE if rep explicit prefix exists. */
1182 bool _isRep;
1183 /** FCML_TRUE if repne explicit prefix exists. */
1184 bool _isRepne;
1185 /** FCML_TRUE if xrelease explicit prefix exists. */
1186 bool _isXrelease;
1187 /** FCML_TRUE if xacquire explicit prefix exists. */
1188 bool _isXacquire;
1189 /** FCML_TRUE if VEX prefix exists. */
1190 bool _isVex;
1191 /** FCML_TRUE if EVEX prefix exists.
1192 * since 1.2.0
1193 */
1194 bool _isEvex;
1195 /** FCML_TRUE if XOP prefix exists. */
1196 bool _isXop;
1197 /** FCML_TRUE if any AVX prefix exists.
1198 * @since 1.2.0
1199 */
1200 bool _isAvx;
1201 /** FCML_TRUE if REX prefix exists. */
1202 bool _isRex;
1203 /** Various fields encoded inside decoded prefixes.
1204 * since 1.2.0
1205 */
1206 fcml_uint8_t _AvxPrefixFirstByte;
1207 /** R field of REX,XOP,VEX or EVEX prefix. */
1208 fcml_uint8_t _R;
1209 /** R' field of EVEX prefix.
1210 * since 1.2.0
1211 */
1212 fcml_uint8_t _RPrim;
1213 /** X field of REX,XOP,VEX or EVEX prefix. */
1214 fcml_uint8_t _X;
1215 /** B field of REX,XOP,VEX or EVEX prefix. */
1216 fcml_uint8_t _B;
1217 /** W field of REX,XOP,VEX or EVEX prefix. */
1218 fcml_uint8_t _W;
1219 /** L field of XOP, VEX or EVEX prefix. */
1220 fcml_uint8_t _L;
1221 /** L' field of EVEX prefix.
1222 * since 1.2.0
1223 */
1224 fcml_uint8_t _LPrim;
1225 /** m-mmmm field of XOP or VEX prefix. */
1226 fcml_uint8_t _mmmm;
1227 /** vvvv field of XOP or VEX prefix. */
1228 fcml_uint8_t _vvvv;
1229 /** V' field of EVEX prefix.
1230 * since 1.2.0
1231 */
1232 fcml_uint8_t _VPrim;
1233 /** pp field of XOP or VEX prefix. */
1234 fcml_uint8_t _pp;
1235 /** aaa field of EVEX prefix.
1236 * since 1.2.0
1237 */
1238 fcml_uint8_t _aaa;
1239 /** EVEX.b bit.
1240 * since 1.2.0
1241 */
1242 bool _b;
1243 /** EVEX.z bit.
1244 * since 1.2.0
1245 */
1246 bool _z;
1247 };
1248
1249 /** Operand details.
1250 * @since 1.1.0
1251 */
1252 class OperandDetails {
1253 public:
1254
1255 /** @since 1.1.0 */
1256 enum AccessMode {
1257 /** Undefined mode. */
1258 AM_ACCESS_MODE_UNDEFINED = FCML_AM_ACCESS_MODE_UNDEFINED,
1259 /** Operand is read by instruction. */
1260 AM_READ = FCML_AM_READ,
1261 /** Operand is set by instruction */
1262 AM_WRITE = FCML_AM_WRITE,
1263 /** Operand is read but can be also set. */
1264 AM_READ_WRITE = AM_READ | AM_WRITE
1265 };
1266
1267 /**
1268 * Creates default operand details with an undefined access mode.
1269 * @since 1.1.0
1270 */
1271 OperandDetails() :
1272 _accessMode(AM_ACCESS_MODE_UNDEFINED) {
1273 }
1274
1275 /**
1276 * Creates operand details for given access mode.
1277 *
1278 * @param accessMode Access mode.
1279 * @since 1.1.0
1280 */
1281 OperandDetails(AccessMode accessMode) :
1282 _accessMode(accessMode) {
1283 }
1284
1285 /**
1286 * Gets access mode for the operand.
1287 *
1288 * @return Access mode.
1289 * @since 1.1.0
1290 */
1291 AccessMode getAccessMode() const {
1292 return _accessMode;
1293 }
1294
1295 /**
1296 * Sets an access mode for the operand.
1297 *
1298 * @param accessMode The access mode for the operand.
1299 * @since 1.1.0
1300 */
1301 void setAccessMode(AccessMode accessMode) {
1302 _accessMode = accessMode;
1303 }
1304
1305 private:
1306 /** Operan's access mode. */
1307 AccessMode _accessMode;
1308 };
1309
1310 /** ModRM details.
1311 * @since 1.1.0
1312 */
1313 class DecodedModRMDetails {
1314 public:
1315
1316 /**
1317 * Creates an empty ModR/M details.
1318 * @since 1.1.0
1319 */
1320 DecodedModRMDetails() :
1321 _isRip(false) {
1322 }
1323
1324 /**
1325 * Gets true if RIP byte is available.
1326 *
1327 * @return True if RIP byte is available.
1328 * @since 1.1.0
1329 */
1330 bool isRip() const {
1331 return _isRip;
1332 }
1333
1334 /**
1335 * Sets RIP byte availability.
1336 *
1337 * @param isRip True if RIP byte is available.
1338 * @since 1.1.0
1339 */
1340 void setRip(bool isRip) {
1341 _isRip = isRip;
1342 }
1343
1344 /**
1345 * Gets ModR/M nullable byte.
1346 *
1347 * @return ModR/M nullable byte.
1348 * @since 1.1.0
1349 */
1350 const Nullable<fcml_uint8_t>& getModRM() const {
1351 return _modRM;
1352 }
1353
1354 /**
1355 * Gets ModR/M nullable byte.
1356 *
1357 * @return ModR/M nullable byte.
1358 * @since 1.1.0
1359 */
1360 Nullable<fcml_uint8_t>& getModRM() {
1361 return _modRM;
1362 }
1363
1364 /**
1365 * Sets ModR/M nullable byte.
1366 *
1367 * @param modRM ModR/M nullable byte.
1368 * @since 1.1.0
1369 */
1370 void setModRM(const Nullable<fcml_uint8_t> &modRM) {
1371 _modRM = modRM;
1372 }
1373
1374 /**
1375 * Gets SIB nullable byte.
1376 *
1377 * @return SIB nullable byte.
1378 * @since 1.1.0
1379 */
1380 const Nullable<fcml_uint8_t>& getSib() const {
1381 return _sib;
1382 }
1383
1384 /**
1385 * Gets SIB nullable byte.
1386 *
1387 * @return SIB nullable byte.
1388 * @since 1.1.0
1389 */
1390 Nullable<fcml_uint8_t>& getSib() {
1391 return _sib;
1392 }
1393
1394 /**
1395 * Sets SIB nullable byte.
1396 *
1397 * @param sib The SIB nullable byte.
1398 * @since 1.1.0
1399 */
1400 void setSib(const Nullable<fcml_uint8_t> &sib) {
1401 _sib = sib;
1402 }
1403
1404 /**
1405 * Gets constant N (see AVX-512 compressed disp8).
1406 *
1407 * @return N as nullable value.
1408 * @since 1.2.0
1409 */
1410 const Nullable<fcml_uint32_t>& getN() const {
1411 return _N;
1412 }
1413
1414 /**
1415 * Gets N (see compressed AVX-512 disp8).
1416 *
1417 * @return N nullable value.
1418 * @since 1.2.0
1419 */
1420 Nullable<fcml_uint32_t>& getN() {
1421 return _N;
1422 }
1423
1424 /**
1425 * Sets N (see compressed AVX-512 disp8).
1426 *
1427 * @param N N nullable value.
1428 * @since 1.2.0
1429 */
1430 void setN(const Nullable<fcml_uint32_t> &N) {
1431 _N = N;
1432 }
1433
1434 /**
1435 * Gets constant raw displacement.
1436 *
1437 * @return Displacement.
1438 * @since 1.2.0
1439 */
1440 const Integer& getDisplacement() const {
1441 return _displacement;
1442 }
1443
1444 /**
1445 * Gets raw displacement.
1446 *
1447 * @return Displacement.
1448 * @since 1.2.0
1449 */
1450 Integer& getDisplacement() {
1451 return _displacement;
1452 }
1453
1454 /**
1455 * Sets displacement.
1456 *
1457 * @param displacement Displacement.
1458 * @since 1.2.0
1459 */
1460 void setDisplacement(const Integer &displacement) {
1461 _displacement = displacement;
1462 }
1463
1464 private:
1465 /** ModR/M byte if exists.*/
1466 Nullable<fcml_uint8_t> _modRM;
1467 /** SIB byte if exists.*/
1468 Nullable<fcml_uint8_t> _sib;
1469 /** True if RIP encoding is used by decoded instruction. This flag is
1470 * used only in 64 bit mode. */
1471 bool _isRip;
1472 /** Raw displacement.
1473 * since 1.2.0
1474 */
1475 Integer _displacement;
1476 /* N from AVX-512 compressed disp8.
1477 * since 1.2.0
1478 */
1479 Nullable<fcml_uint32_t> _N;
1480 };
1481
1482 /** Additional details about an instruction.
1483 * @since 1.1.0
1484 */
1485 class InstructionDetails {
1486 public:
1487
1488 /**
1489 * Gets address mode/instruction form. This information is used
1490 * internally and is rather useless in day to day usage, but if
1491 * you are interested in it do not hesitate to take a look at
1492 * the manual.
1493 *
1494 * @return Instruction form.
1495 * @since 1.1.0
1496 */
1497 fcml_uint16_t getAddrMode() const {
1498 return _addrMode;
1499 }
1500
1501 /**
1502 * Sets instruction form.
1503 *
1504 * @param addrMode Addressing mode.
1505 * @since 1.1.0
1506 */
1507 void setAddrMode(fcml_uint16_t addrMode) {
1508 _addrMode = addrMode;
1509 }
1510
1511 /**
1512 * Gets instruction code. See fcml_en_instruction for more details.
1513 *
1514 * @return Instruction code.
1515 * @since 1.1.0
1516 */
1517 fcml_en_instruction getInstruction() const {
1518 return _instruction;
1519 }
1520
1521 /**
1522 * Gets a new instruction code for the instruction.
1523 *
1524 * @param instruction The new instruction code.
1525 * @since 1.1.0
1526 */
1527 void setInstruction(fcml_en_instruction instruction) {
1528 _instruction = instruction;
1529 }
1530
1531 /**
1532 * Gets a pointer to the instruction code. See fcml_en_instruction
1533 * for more details.
1534 *
1535 * @return The pointer to the instruction code.
1536 * @since 1.1.0
1537 */
1538 const fcml_uint8_t* getInstructionCode() const {
1539 return _instructionCode;
1540 }
1541
1542 /**
1543 * Gets a pointer to the instruction code. See fcml_en_instruction
1544 * for more details.
1545 *
1546 * @return The pointer to the instruction code.
1547 * @since 1.1.0
1548 */
1549 fcml_uint8_t* getInstructionCode() {
1550 return _instructionCode;
1551 }
1552
1553 /**
1554 * Gets instruction group. See fcml_instructions.h for all
1555 * available groups.
1556 *
1557 * @return The instruction group.
1558 * @since 1.1.0
1559 */
1560 fcml_uint64_t getInstructionGroup() const {
1561 return _instructionGroup;
1562 }
1563
1564 /**
1565 * Sets an instruction group. See fcml_instructions.h for all
1566 * available groups.
1567 *
1568 * @param instructionGroup The instruction group.
1569 * @since 1.1.0
1570 */
1571 void setInstructionGroup(fcml_uint64_t instructionGroup) {
1572 _instructionGroup = instructionGroup;
1573 }
1574
1575 /**
1576 * Instruction size in bytes.
1577 *
1578 * @return Size of the instruction in bytes.
1579 * @since 1.1.0
1580 */
1581 fcml_usize getInstructionSize() const {
1582 return _instructionSize;
1583 }
1584
1585 /**
1586 * Sets the instruction size in bytes.
1587 *
1588 * @param instructionSize The instruction size.
1589 * @since 1.1.0
1590 */
1591 void setInstructionSize(fcml_usize instructionSize) {
1592 _instructionSize = instructionSize;
1593 }
1594
1595 /**
1596 * Gets true if it's a shortcut instruction.
1597 *
1598 * @return True if it's a shortcut instruction.
1599 * @since 1.1.0
1600 */
1601 bool isShortcut() const {
1602 return _isShortcut;
1603 }
1604
1605 /**
1606 * Marks the instruction as a shortcut.
1607 *
1608 * @param isShortcut True if it's a shortcut instruction.
1609 * @since 1.1.0
1610 */
1611 void setShortcut(bool isShortcut) {
1612 _isShortcut = isShortcut;
1613 }
1614
1615 /**
1616 * Gets ModR/M instruction details.
1617 *
1618 * @return ModR/M details.
1619 * @since 1.1.0
1620 */
1621 const DecodedModRMDetails& getModRmDetails() const {
1622 return _modRMDetails;
1623 }
1624
1625 /**
1626 * Gets ModR/M instruction details.
1627 *
1628 * @return ModR/M details.
1629 * @since 1.1.0
1630 */
1631 DecodedModRMDetails& getModRmDetails() {
1632 return _modRMDetails;
1633 }
1634
1635 /**
1636 * Sets a new instruction details for the instruction.
1637 *
1638 * @param modRmDetails The new instruction details.
1639 * @since 1.1.0
1640 */
1641 void setModRmDetails(const DecodedModRMDetails &modRmDetails) {
1642 _modRMDetails = modRmDetails;
1643 }
1644
1645 /**
1646 * Gets opcode field 'S'.
1647 *
1648 * @return 'S' opcode field.
1649 * @since 1.1.0
1650 */
1651 bool isOpcodeFieldSBit() const {
1652 return _opcodeFieldSBit;
1653 }
1654
1655 /**
1656 * Sets 'S' field of the opcode byte.
1657 *
1658 * @param opcodeFieldSBit 'S' opcode byte field.
1659 * @since 1.1.0
1660 */
1661 void setOpcodeFieldSBit(bool opcodeFieldSBit) {
1662 _opcodeFieldSBit = opcodeFieldSBit;
1663 }
1664
1665 /**
1666 * Gets opcode field 'W'.
1667 *
1668 * @return 'W' opcode field.
1669 * @since 1.1.0
1670 */
1671 bool isOpcodeFieldWBit() const {
1672 return _opcodeFieldWBit;
1673 }
1674
1675 /**
1676 * Sets 'W' field of the opcode byte.
1677 *
1678 * @param opcodeFieldWBit 'W' opcode byte field.
1679 * @since 1.1.0
1680 */
1681 void setOpcodeFieldWBit(bool opcodeFieldWBit) {
1682 _opcodeFieldWBit = opcodeFieldWBit;
1683 }
1684
1685 /**
1686 * Gets the operand details for given index.
1687 *
1688 * @param index Index of the instruction details.
1689 * @return The operand details for the given index.
1690 * @throw BadArgumentException Array index out of bound.
1691 * @since 1.1.0
1692 */
1693 const OperandDetails& getOperandDetails(fcml_usize index) const {
1694 if (index > FCML_OPERANDS_COUNT) {
1695 throw BadArgumentException(FCML_TEXT("Array index out of bound."),
1696 FCML_CEH_GEC_VALUE_OUT_OF_RANGE);
1697 }
1698 return _operandDetails[index];
1699 }
1700
1701 /**
1702 * Gets the operand details for given index.
1703 *
1704 * @param index Index of the instruction details.
1705 * @return The operand details for the given index.
1706 * @throw BadArgumentException Array index out of bound.
1707 * @since 1.1.0
1708 */
1709 OperandDetails& getOperandDetails(fcml_usize index) {
1710 if (index > FCML_OPERANDS_COUNT) {
1711 throw BadArgumentException(FCML_TEXT("Array index out of bound."),
1712 FCML_CEH_GEC_VALUE_OUT_OF_RANGE);
1713 }
1714 return _operandDetails[index];
1715 }
1716
1717 /**
1718 * Gets instruction prefixes details.
1719 *
1720 * @return The instruction prefix details.
1721 * @since 1.1.0
1722 */
1723 const PrefixesDetails& getPrefixesDetails() const {
1724 return _prefixesDetails;
1725 }
1726
1727 /**
1728 * Gets instruction prefixes details.
1729 *
1730 * @return The instruction prefix details.
1731 * @since 1.1.0
1732 */
1733 PrefixesDetails& getPrefixesDetails() {
1734 return _prefixesDetails;
1735 }
1736
1737 /**
1738 * Sets a new instruction prefixes details.
1739 *
1740 * @param prefixesDetails The new prefixes details.
1741 * @since 1.1.0
1742 */
1743 void setPrefixesDetails(const PrefixesDetails &prefixesDetails) {
1744 _prefixesDetails = prefixesDetails;
1745 }
1746
1747 /**
1748 * Gets pseudo operation code.
1749 *
1750 * @return The pseudo operation associated with the instruction.
1751 * @since 1.1.0
1752 */
1753 fcml_en_pseudo_operations getPseudoOp() const {
1754 return _pseudoOp;
1755 }
1756
1757 /**
1758 * Sets pseudo operation for the instruction.
1759 *
1760 * @param pseudoOp The pseudo operation.
1761 * @since 1.1.0
1762 */
1763 void setPseudoOp(fcml_en_pseudo_operations pseudoOp) {
1764 _pseudoOp = pseudoOp;
1765 }
1766
1767 /**
1768 * Gets true is it's a pseudo operation.
1769 *
1770 * @return True if the instruction is a pseudo operation.
1771 * @since 1.1.0
1772 */
1773 bool isPseudoOp() const {
1774 return _isPseudoOp;
1775 }
1776
1777 /**
1778 * Sets pseudo operation flag.
1779 *
1780 * @param isPseudoOp True if the instruction is a pseudo operation.
1781 * @since 1.1.0
1782 */
1783 void setIsPseudoOp(bool isPseudoOp) {
1784 _isPseudoOp = isPseudoOp;
1785 }
1786
1787 /**
1788 * Gets avx-512 tuple type.
1789 * @since 1.2.0
1790 */
1791 fcml_uint8_t getTupleType() const {
1792 return _tupleType;
1793 }
1794
1795 /**
1796 * Sets avx-512 tuple type.
1797 *
1798 * @param isPseudoOp True if the instruction is a pseudo operation.
1799 * @since 1.2.0
1800 */
1801 void setTupleType(fcml_uint8_t tupleType) {
1802 _tupleType = tupleType;
1803 }
1804
1805 private:
1806
1807 /**
1808 * True if this is a shortcut.
1809 * A good example of such instruction is 'cmpsb' as opposed to 'cmps
1810 * byte ptr [si],byte ptr [di]'. It is very important to take this
1811 * information into consideration when instruction models are analyzed
1812 * because there is no operands in the GIM for shortcuts.
1813 */
1814 bool _isShortcut;
1815 /**
1816 * True if given instruction is a short form of pseudo-ops instructions.
1817 * See 'vcmpunordsd' for instance.
1818 */
1819 bool _isPseudoOp;
1820 /**
1821 * Code of the disassembled instruction.
1822 */
1823 fcml_uint8_t _instructionCode[FCML_INSTRUCTION_SIZE];
1824 /**
1825 * Instruction size in bytes.
1826 */
1827 fcml_usize _instructionSize;
1828 /**
1829 * Some additional information about decoded instruction prefixes.
1830 */
1831 PrefixesDetails _prefixesDetails;
1832 /**
1833 * All disassembler specific information about operands going there.
1834 */
1835 OperandDetails _operandDetails[FCML_OPERANDS_COUNT];
1836 /**
1837 * Details about decoded ModR/M and SIB bytes.
1838 */
1839 DecodedModRMDetails _modRMDetails;
1840 /** Opcode field 's'.
1841 * This is set only for informational purpose only and you should not use
1842 * it for any critical functionality.
1843 */
1844 bool _opcodeFieldSBit;
1845 /** Opcode field 'w'.
1846 * This is set only for informational purpose only and you should not
1847 * use it for any critical functionality.
1848 */
1849 bool _opcodeFieldWBit;
1850 /**
1851 * Instruction code/number. @see fcml_instructions.h header file.
1852 */
1853 fcml_en_instruction _instruction;
1854 /**
1855 * Pseudo operation code.
1856 */
1857 fcml_en_pseudo_operations _pseudoOp;
1858 /**
1859 * Code of the instruction form/addressing mode of the instruction above.
1860 */
1861 fcml_uint16_t _addrMode;
1862 /**
1863 * Instruction group.
1864 */
1865 fcml_uint64_t _instructionGroup;
1866 /**
1867 * Tuple type used by avx-512 instructions to calculate disp8.
1868 */
1869 fcml_uint8_t _tupleType;
1870 };
1871
1872 /** Disassembler result.
1873 *
1874 * It's a counterpart to the fcml_st_disassembler_result structure.
1875 * @since 1.1.0
1876 */
1877 class DisassemblerResult {
1878
1879 public:
1880
1881 /**
1882 * Gets errors container with errors related to the failed
1883 * disassembling process.
1884 *
1885 * @return The error container.
1886 * @since 1.1.0
1887 */
1888 const ErrorContainer& getErrorContainer() const {
1889 return _errorContainer;
1890 }
1891
1892 /**
1893 * Gets errors container with errors related to the failed
1894 * disassembling process.
1895 *
1896 * @return The error container.
1897 * @since 1.1.0
1898 */
1899 const Instruction& getInstruction() const {
1900 return _instruction;
1901 }
1902
1903 /**
1904 * Gets instruction details associated with the instruction.
1905 *
1906 * @return The instruction details.
1907 * @since 1.1.0
1908 */
1909 const InstructionDetails& getInstructionDetails() const {
1910 return _instructionDetails;
1911 }
1912
1913 /**
1914 * Cleans the disassembling result.
1915 * @since 1.1.0
1916 */
1917 void clean() {
1918 _errorContainer.clean();
1919 _instructionDetails = InstructionDetails();
1920 _instruction = Instruction();
1921 }
1922
1923 protected:
1924
1925 friend class Disassembler;
1926 friend class DisassemblerTypeConverter;
1927
1928 /**
1929 * Gets mutable instruction details.
1930 * @return Instruction details.
1931 * @since 1.1.0
1932 */
1933 InstructionDetails& getInstructionDetailsInternal() {
1934 return _instructionDetails;
1935 }
1936
1937 /**
1938 * Sets new instruction details for the disassembler.
1939 * @param instructionDetails The instruction details.
1940 * @since 1.1.0
1941 */
1942 void setInstructionDetails(const InstructionDetails &instructionDetails) {
1943 _instructionDetails = instructionDetails;
1944 }
1945
1946 /**
1947 * Gets mutable instruction.
1948 * @return The mutable instruction.
1949 * @since 1.1.0
1950 */
1951 Instruction& getInstructionInternal() {
1952 return _instruction;
1953 }
1954
1955 /**
1956 * Sets a new instruction for the result.
1957 * @param instruction The instruction to be copied to the result.
1958 * @since 1.1.0
1959 */
1960 void setInstruction(const Instruction &instruction) {
1961 _instruction = instruction;
1962 }
1963
1964 /**
1965 * Sets error container.
1966 * @param errorContainer The error container.
1967 * @since 1.1.0
1968 */
1969 void setErrorContainer(const ErrorContainer &errorContainer) {
1970 _errorContainer = errorContainer;
1971 }
1972
1973 private:
1974
1975 /** Errors container */
1976 ErrorContainer _errorContainer;
1977 /** Instruction details. */
1978 InstructionDetails _instructionDetails;
1979 /** The disassembled instruction. */
1980 Instruction _instruction;
1981
1982 };
1983
1984 /**
1985 * Converts objects to their structures counterparts.
1986 * @since 1.1.0
1987 * @remarks Internal API, not intended to be used outside.
1988 */
1989 class DisassemblerTypeConverter {
1990
1991 public:
1992
1993 static void convert(const DisassemblerContext &src,
1994 fcml_st_disassembler_context &dest) {
1995 dest.code = src.getCode();
1996 dest.code_length = src.getCodeLength();
1997 TypeConverter::convert(src.getEntryPoint(), dest.entry_point);
1998 convert(src.getDisassemblerConf(), dest.configuration);
1999 }
2000
2001 static void convert(const DisassemblerConf &src,
2002 fcml_st_disassembler_conf &dest) {
2003 dest.conditional_group = src.getConditionalGroup();
2004 dest.carry_flag_conditional_suffix = src.isCarryFlagConditionalSuffix();
2005 dest.enable_error_messages = src.isEnableErrorMessages();
2006 dest.extend_disp_to_asa = src.isExtendDispToAsa();
2007 dest.fail_if_unknown_instruction = src.isFailIfUnknownInstruction();
2008 dest.increment_ip = src.isIncrementIp();
2009 dest.short_forms = src.isShortForms();
2010 }
2011
2012 static void convert(const fcml_st_decoded_modrm_details &src,
2013 DecodedModRMDetails &dest) {
2014 dest.setRip(FCML_TO_CPP_BOOL(src.is_rip));
2015 Nullable<fcml_uint8_t> &modRM = dest.getModRM();
2016 modRM.setNotNull(FCML_TO_CPP_BOOL(src.is_modrm));
2017 modRM.setValue(src.modrm);
2018 Nullable<fcml_uint8_t> &sib = dest.getSib();
2019 sib.setNotNull(FCML_TO_CPP_BOOL(src.sib.is_not_null));
2020 sib.setValue(src.sib.value);
2021 TypeConverter::convert(src.displacement.displacement,
2022 dest.getDisplacement());
2023 Nullable<fcml_uint32_t> N;
2024 N.setNotNull(FCML_TO_CPP_BOOL(src.displacement.N.is_not_null));
2025 N.setValue(src.displacement.N.value);
2026 dest.setN(N);
2027 }
2028
2029 static void convert(const DecodedModRMDetails &src,
2030 fcml_st_decoded_modrm_details &dest) {
2031 dest.is_modrm = src.getModRM().isNotNull();
2032 dest.is_rip = src.isRip();
2033 dest.modrm = src.getModRM().getValue();
2034 fcml_nuint8_t &sib = dest.sib;
2035 sib.is_not_null = src.getSib().isNotNull();
2036 sib.value = src.getSib().getValue();
2037 TypeConverter::convert(src.getDisplacement(),
2038 dest.displacement.displacement);
2039 dest.displacement.N.is_not_null = src.getN().isNotNull();
2040 dest.displacement.N.value = src.getN().getValue();
2041 }
2042
2043 static void convert(const fcml_st_operand_details &src,
2044 OperandDetails &dest) {
2045 dest.setAccessMode(
2046 static_cast<OperandDetails::AccessMode>(src.access_mode));
2047 }
2048
2049 static void convert(const OperandDetails &src,
2050 fcml_st_operand_details &dest) {
2051 dest.access_mode =
2052 static_cast<fcml_en_access_mode>(src.getAccessMode());
2053 }
2054
2055 static void convert(const fcml_st_instruction_prefix &src,
2056 InstructionPrefixDetails &dest) {
2057 dest.setMandatoryPrefix(FCML_TO_CPP_BOOL(src.mandatory_prefix));
2058 dest.setPrefix(src.prefix);
2059 dest.setPrefixType(
2060 static_cast<InstructionPrefixDetails::PrefixType>(src.prefix_type));
2061 ::memcpy(dest.getAvxBytes(), src.avx_bytes, sizeof(src.avx_bytes));
2062 }
2063
2064 static void convert(const InstructionPrefixDetails &src,
2065 fcml_st_instruction_prefix &dest) {
2066 dest.mandatory_prefix = src.isMandatoryPrefix();
2067 dest.prefix = src.getPrefix();
2068 dest.prefix_type =
2069 static_cast<fcml_en_prefix_types>(src.getPrefixType());
2070 ::memcpy(dest.avx_bytes, src.getAvxBytes(), sizeof(dest.avx_bytes));
2071 }
2072
2073 static void convert(const fcml_st_prefixes_details src,
2074 PrefixesDetails &dest) {
2075 for (int i = 0; i < FCML_DASM_PREFIXES_COUNT; i++) {
2076 convert(src.prefixes[i], dest.getPrefixes(i));
2077 }
2078 dest.setPrefixesCount(src.prefixes_count);
2079 dest.setPrefixesBytesCount(src.prefixes_bytes_count);
2080 dest.setBranch(FCML_TO_CPP_BOOL(src.is_branch));
2081 dest.setNobranch(FCML_TO_CPP_BOOL(src.is_nobranch));
2082 dest.setLock(FCML_TO_CPP_BOOL(src.is_lock));
2083 dest.setRep(FCML_TO_CPP_BOOL(src.is_rep));
2084 dest.setRepne(FCML_TO_CPP_BOOL(src.is_repne));
2085 dest.setXrelease(FCML_TO_CPP_BOOL(src.is_xrelease));
2086 dest.setXacquire(FCML_TO_CPP_BOOL(src.is_xacquire));
2087 dest.setVex(FCML_TO_CPP_BOOL(src.is_vex));
2088 dest.setEvex(FCML_TO_CPP_BOOL(src.is_evex));
2089 dest.setXop(FCML_TO_CPP_BOOL(src.is_xop));
2090 dest.setAvx(FCML_TO_CPP_BOOL(src.is_avx));
2091 dest.setRex(FCML_TO_CPP_BOOL(src.is_rex));
2092 dest.setAvxFirstByte(src.avx_first_byte);
2093 dest.setR(src.R);
2094 dest.setRPrim(src.R_prim);
2095 dest.setX(src.X);
2096 dest.setB(src.B);
2097 dest.setW(src.W);
2098 dest.setL(src.L);
2099 dest.setLPrim(src.L_prim);
2100 dest.setMmmm(src.mmmm);
2101 dest.setVvvv(src.vvvv);
2102 dest.setVPrim(src.V_prim);
2103 dest.setPp(src.pp);
2104 dest.setAaa(src.aaa);
2105 dest.setBcast(src.b);
2106 dest.setZ(src.z);
2107 }
2108
2109 static void convert(const PrefixesDetails src,
2110 fcml_st_prefixes_details &dest) {
2111 for (int i = 0; i < FCML_DASM_PREFIXES_COUNT; i++) {
2112 convert(src.getPrefixes(i), dest.prefixes[i]);
2113 }
2114 dest.prefixes_count = src.getPrefixesCount();
2115 dest.prefixes_bytes_count = src.getPrefixesBytesCount();
2116 dest.is_branch = src.isBranch();
2117 dest.is_nobranch = src.isNobranch();
2118 dest.is_lock = src.isLock();
2119 dest.is_rep = src.isRep();
2120 dest.is_repne = src.isRepne();
2121 dest.is_xrelease = src.isXrelease();
2122 dest.is_xacquire = src.isXacquire();
2123 dest.is_vex = src.isVex();
2124 dest.is_xop = src.isXop();
2125 dest.is_avx = src.isAvx();
2126 dest.is_evex = src.isEvex();
2127 dest.is_rex = src.isRex();
2128 dest.avx_first_byte = src.getAvxFirstByte();
2129 dest.R = src.getR();
2130 dest.R_prim = src.getRPrim();
2131 dest.X = src.getX();
2132 dest.B = src.getB();
2133 dest.W = src.getW();
2134 dest.L = src.getL();
2135 dest.L_prim = src.getLPrim();
2136 dest.mmmm = src.getMmmm();
2137 dest.vvvv = src.getVvvv();
2138 dest.V_prim = src.getVPrim();
2139 dest.pp = src.getPp();
2140 dest.aaa = src.getAaa();
2141 dest.b = src.getBcast();
2142 dest.z = src.getZ() ? 1 : 0;
2143 }
2144
2145 static void convert(const fcml_st_instruction_details &src,
2146 InstructionDetails &dest) {
2147 dest.setTupleType(src.tuple_type);
2148 dest.setAddrMode(src.addr_mode);
2149 dest.setInstruction(src.instruction);
2150 dest.setInstructionGroup(src.instruction_group);
2151 dest.setInstructionSize(src.instruction_size);
2152 dest.setOpcodeFieldSBit(FCML_TO_CPP_BOOL(src.opcode_field_s_bit));
2153 dest.setOpcodeFieldWBit(FCML_TO_CPP_BOOL(src.opcode_field_w_bit));
2154 dest.setIsPseudoOp(FCML_TO_CPP_BOOL(src.is_pseudo_op));
2155 dest.setPseudoOp(src.pseudo_op);
2156 dest.setShortcut(FCML_TO_CPP_BOOL(src.is_shortcut));
2157 convert(src.modrm_details, dest.getModRmDetails());
2158 for (int i = 0; i < FCML_OPERANDS_COUNT; i++) {
2159 convert(src.operand_details[i], dest.getOperandDetails(i));
2160 }
2161 fcml_uint8_t *code = dest.getInstructionCode();
2162 for (int i = 0; i < FCML_INSTRUCTION_SIZE; i++) {
2163 code[i] = src.instruction_code[i];
2164 }
2165 convert(src.prefixes_details, dest.getPrefixesDetails());
2166 }
2167
2168 static void convert(const InstructionDetails &src,
2169 fcml_st_instruction_details &dest) {
2170 dest.tuple_type = src.getTupleType();
2171 dest.addr_mode = src.getAddrMode();
2172 dest.instruction = src.getInstruction();
2173 dest.instruction_group = src.getInstructionGroup();
2174 dest.instruction_size = src.getInstructionSize();
2175 dest.opcode_field_s_bit = src.isOpcodeFieldSBit();
2176 dest.opcode_field_w_bit = src.isOpcodeFieldWBit();
2177 dest.is_pseudo_op = src.isPseudoOp();
2178 dest.pseudo_op = src.getPseudoOp();
2179 dest.is_shortcut = src.isShortcut();
2180 convert(src.getModRmDetails(), dest.modrm_details);
2181 for (int i = 0; i < FCML_OPERANDS_COUNT; i++) {
2182 convert(src.getOperandDetails(i), dest.operand_details[i]);
2183 }
2184 for (int i = 0; i < FCML_INSTRUCTION_SIZE; i++) {
2185 dest.instruction_code[i] = src.getInstructionCode()[i];
2186 }
2187 convert(src.getPrefixesDetails(), dest.prefixes_details);
2188 }
2189
2190 static void convert(const fcml_st_disassembler_result &src,
2191 DisassemblerResult &dest) {
2192 TypeConverter::convert(src.instruction, dest.getInstructionInternal());
2193 convert(src.instruction_details, dest.getInstructionDetailsInternal());
2194 }
2195
2196 static void convert(const DisassemblerResult &src,
2197 fcml_st_disassembler_result &dest) {
2198 TypeConverter::convert(src.getInstruction(), dest.instruction);
2199 convert(src.getInstructionDetails(), dest.instruction_details);
2200 }
2201
2202 static void free(fcml_st_disassembler_result &src) {
2203 TypeConverter::free(src.instruction);
2204 }
2205
2206 };
2207
2208 /** Disassembler wrapper.
2209 * @since 1.1.0
2210 */
2211 class Disassembler: public NonCopyable, protected DialectAware {
2212 public:
2213
2214 /**
2215 * Creates a disassembler instance for the given dialect.
2216 *
2217 * @param dialect The dialect for the disassembler.
2218 * @throw InitException Cannot initialize the disassembler.
2219 * @since 1.1.0
2220 */
2221 Disassembler(Dialect &dialect) :
2222 _dialect(dialect) {
2223 fcml_ceh_error error = ::fcml_fn_disassembler_init(
2224 extractDialect(dialect), &_disassembler);
2225 if (error) {
2226 throw InitException(
2227 FCML_TEXT("Cannot initialize the disassembler."), error);
2228 }
2229 }
2230
2231 /**
2232 * Destructor.
2233 * @since 1.1.0
2234 */
2235 virtual ~Disassembler() {
2236 if (_disassembler) {
2237 ::fcml_fn_disassembler_free(_disassembler);
2238 _disassembler = NULL;
2239 }
2240 }
2241
2242 public:
2243
2244 /**
2245 * Disassembled the next instruction from the context.
2246 *
2247 * @param ctx Context describing the next instruction to disassemble.
2248 * @param[out] disassemblerResult Disassembler result.
2249 * @throw DisassemblingFailedException Disassemblation failed.
2250 * @return Error code.
2251 * @since 1.1.0
2252 */
2253 fcml_ceh_error disassemble(DisassemblerContext &ctx,
2254 DisassemblerResult &disassemblerResult) {
2255
2256 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
2257
2258 fcml_st_disassembler_context context;
2259 DisassemblerTypeConverter::convert(ctx, context);
2260
2261 context.disassembler = _disassembler;
2262
2263 /* Prepare assembler result. */
2264 fcml_st_disassembler_result disassembler_result;
2265 ::fcml_fn_disassembler_result_prepare(&disassembler_result);
2266
2267 try {
2268
2269 disassemblerResult.clean();
2270
2271 error = ::fcml_fn_disassemble(&context, &disassembler_result);
2272
2273 ErrorContainer errorContainer;
2274 ErrorTypeConverter::convert(disassembler_result.errors,
2275 errorContainer);
2276
2277 disassemblerResult.setErrorContainer(errorContainer);
2278
2279 if (error && ctx.getDisassemblerConf().isThrowExceptionOnError()) {
2280 ::fcml_fn_disassembler_result_free(&disassembler_result);
2281 throw DisassemblingFailedException(
2282 FCML_TEXT("Assembling failed."), errorContainer, error);
2283 }
2284
2285 if (!error) {
2286
2287 // Convert result.
2288 DisassemblerTypeConverter::convert(disassembler_result,
2289 disassemblerResult);
2290
2291 ctx.getEntryPoint().setIP(context.entry_point.ip);
2292 ctx.setCode(context.code);
2293 ctx.setCodeLength(context.code_length);
2294
2295 }
2296
2297 ::fcml_fn_disassembler_result_free(&disassembler_result);
2298
2299 } catch (std::exception &exc) {
2300 // If anything failed, free assembler results.
2301 ::fcml_fn_disassembler_result_free(&disassembler_result);
2302 throw exc;
2303 }
2304
2305 return error;
2306 }
2307
2308 /**
2309 * Gets dialect associated with the disassembler.
2310 * @return The dialect instance associated with the disassembler.
2311 * @since 1.1.0
2312 */
2313 Dialect& getDialect() const {
2314 return _dialect;
2315 }
2316
2317 private:
2318
2319 /** The dialect associated with the disassembler. */
2320 Dialect &_dialect;
2321 /** The disassembler instance. */
2322 fcml_st_disassembler *_disassembler;
2323
2324 };
2325
2326 }
2327
2328 #endif //FCML_DISASSEMBLER_HPP_
2329