def from_source(cls, text, arch, base=0x1000): text = source.remove_comments(text) try: blob = assembler.assemble_source(arch, text, base=base) except keystone.KsError as error: logger.error( "Failed to assemble instruction: '{}' at: 0x{:04x}".format( text, base)) raise error return cls.from_bytes(blob, arch, base=base)
def _permutation_bytes_replacements(self): # if replacing instructions, operate at the source level to use labels src_code = self.permutation_source(replacements=True) exec_seg_src = str(src_code) exec_seg_src = source.remove_comments(exec_seg_src) try: blob = bytes(self.arch.keystone.asm(exec_seg_src, self.address)[0]) except keystone.KsError as error: logger.error('Failed to assemble source, error: ' + error.message) return None return blob
def assemble_source(arch, text, base=0x1000): if isinstance(text, source.SourceCode): text = str(text) text = source.remove_comments(text) if text and isinstance(arch, (archinfo.ArchAMD64, archinfo.ArchX86)): # apply this syntax fixup to add 'ptr' to reference operations # example: `mov eax, dword [rdx+60]` -> `mov eax, dword ptr [rdx+60]` text = re.sub(r'(\s[dq]?word|byte) \[', r'\1 ptr [', text, flags=re.IGNORECASE) # apply this syntax fixup to move segment selectors outside of brackets # example: `mov rdx, [gs:rdx+96]` -> `mov rdx, gs:[rdx+96]` text = re.sub(r'([\w,]\s*)\[([cdefgs]s):(\s*\w)', r'\1\2:[\3', text, flags=re.IGNORECASE) # discard the NASM "BITS" directive if it's the first line, see: https://www.nasm.us/xdoc/2.13rc23/html/nasmdoc6.html first_line, _ = text.split('\n', 1) if re.match(r'\s*\[\s*BITS\s+(32|64)\s*\]$', first_line): _, text = text.split('\n', 1) return bytes(arch.keystone.asm(text, base)[0])
def assemble_source(arch, text): text = source.remove_comments(text) return bytes(arch.keystone.asm(text, 0x1000)[0])
def from_source(cls, text, arch, base=0x1000): blob, _ = arch.keystone.asm(source.remove_comments(text), base) return cls(bytes(blob), arch, base=base)