int fn_disassemble_xed(xed_syntax_enum_t syntax, char* buf, int buflen, xed_decoded_inst_t* xedd, xed_uint64_t runtime_instruction_address, void* caller_data) { /* the caller_data is for symbolic disam */ char buffer[XED_TMP_BUF_LEN]; int blen = buflen; int ok = xed_format_context(syntax, xedd, buffer, XED_TMP_BUF_LEN, runtime_instruction_address, caller_data); if(ok) blen = xed_strncpy(buf, buffer, buflen); else { blen = buflen; blen = xed_strncpy(buf, "Error disassembling ", blen); blen = xed_strncat(buf, xed_syntax_enum_t2str(syntax), blen); blen = xed_strncat(buf, " syntax.", blen); } return blen; }
void xed_dot_graph_add_instruction( xed_dot_graph_supp_t* gg, xed_decoded_inst_t* xedd, xed_uint64_t runtime_instr_addr, void* caller_data) { /* make a new node for each operand: if read: make edge from src node for that reg to the new node for each operand: if write: install this node as the writer what about partial writes? what about register nesting? */ char disasm_str[XED_DOT_TMP_BUF_LEN]; char* p = 0; size_t alen = 0; int ok; xed_dot_node_t* n = 0; xed_uint32_t remaining_buffer_bytes = XED_DOT_TMP_BUF_LEN; // put addr on separate line in node label #if defined(XED_WINDOWS) ok = sprintf_s(disasm_str, XED_DOT_TMP_BUF_LEN, XED_FMT_LX "\\n", runtime_instr_addr); #else ok = sprintf(disasm_str, XED_FMT_LX "\\n", runtime_instr_addr); #endif assert(ok > 0); alen = strlen(disasm_str); p = disasm_str + alen; remaining_buffer_bytes -= XED_CAST(xed_uint32_t, alen); ok = xed_format_context(gg->syntax, xedd, p, remaining_buffer_bytes, runtime_instr_addr, caller_data); if (!ok) { (void)xed_strncpy(disasm_str,"???", XED_DOT_TMP_BUF_LEN); } n = xed_dot_node(gg->g, disasm_str); add_read_operands(gg,xedd,n); add_write_operands(gg,xedd,n); }
static PyObject *dump_intel_format(instruction_t *self) { char buf[64]; /* In Pin versions before "pin-2.14-*", `xed_format_context()' takes 6 * parameters instead of 7. Unfortunately, there's no consistent way of * dumping instructions between various XED versions and there's no easy * way of detecting the actual XED version at compile time. */ xed_format_context(XED_SYNTAX_INTEL, self->decoded_inst, buf, sizeof(buf) - 1, self->runtime_address, NULL, NULL); return PyString_FromFormat("%p %s", (void *)self->runtime_address, buf); }
int main(int argc, char** argv) { xed_error_enum_t xed_error; xed_bool_t long_mode = 0; xed_bool_t prot16 = 0; unsigned int first_argv; unsigned int bytes = 0; unsigned char itext[XED_MAX_INSTRUCTION_BYTES]; int i; unsigned int u; xed_decoded_inst_t xedd; #define BUFLEN 1000 char buffer[BUFLEN]; xed_bool_t ok; unsigned int isyntax; xed_syntax_enum_t syntax; xed_machine_mode_enum_t mmode; xed_address_width_enum_t stack_addr_width; xed_encoder_request_t* enc_req; xed_uint8_t array[XED_MAX_INSTRUCTION_BYTES]; unsigned int enc_olen, ilen = XED_MAX_INSTRUCTION_BYTES; xed_error_enum_t encode_okay; xed_bool_t change_to_long_mode = 0; xed_tables_init(); xed_set_verbosity( 99 ); if (argc > 2 && strcmp(argv[1], "-64") == 0) long_mode = 1; else if (argc > 2 && strcmp(argv[1], "-16") == 0) prot16 = 1; if (long_mode) { first_argv = 2; mmode=XED_MACHINE_MODE_LONG_64; stack_addr_width =XED_ADDRESS_WIDTH_64b; } else if (prot16) { first_argv = 2; mmode=XED_MACHINE_MODE_LEGACY_16; stack_addr_width =XED_ADDRESS_WIDTH_16b; } else { first_argv=1; mmode=XED_MACHINE_MODE_LEGACY_32; stack_addr_width =XED_ADDRESS_WIDTH_32b; } xed_decoded_inst_zero(&xedd); xed_decoded_inst_set_mode(&xedd, mmode, stack_addr_width); for( i=first_argv ;i < argc; i++) { xed_uint8_t x = (xed_uint8_t)(xed_atoi_hex(argv[i])); assert(bytes < XED_MAX_INSTRUCTION_BYTES); itext[bytes++] = x; } if (bytes == 0) { fprintf(stderr, "Must supply some hex bytes\n"); exit(1); } printf("PARSING BYTES: "); for( u=0;u<bytes; u++) printf("%02x ", XED_STATIC_CAST(unsigned int,itext[u])); printf("\n"); xed_error = xed_decode(&xedd, XED_REINTERPRET_CAST(const xed_uint8_t*,itext), bytes); switch(xed_error) { case XED_ERROR_NONE: break; case XED_ERROR_BUFFER_TOO_SHORT: fprintf(stderr,"Not enough bytes provided\n"); exit(1); case XED_ERROR_GENERAL_ERROR: fprintf(stderr,"Could not decode given input.\n"); exit(1); default: fprintf(stderr,"Unhandled error code %s\n", xed_error_enum_t2str(xed_error)); exit(1); } //memset(buffer,0,BUFLEN); xed_decoded_inst_dump(&xedd,buffer, BUFLEN); printf("%s\n",buffer); for(isyntax= XED_SYNTAX_XED; isyntax < XED_SYNTAX_LAST; isyntax++) { syntax = XED_STATIC_CAST(xed_syntax_enum_t, isyntax); ok = xed_format_context(syntax, &xedd, buffer, BUFLEN, 0, 0, 0); if (ok) printf("%s syntax: %s\n", xed_syntax_enum_t2str(syntax), buffer); else printf("Error disassembling %s syntax\n", xed_syntax_enum_t2str(syntax)); } printf("\n\nPreparing to encode ...\n"); enc_req = &xedd; // they are basically the same now // convert decode structure to proper encode structure xed_encoder_request_init_from_decode(&xedd); change_to_long_mode = 0; if (change_to_long_mode) { // change to 64b mode xed_state_t state; xed_operand_values_t* ov; xed_state_init2(&state,XED_MACHINE_MODE_LONG_64,XED_ADDRESS_WIDTH_64b); ov = xed_decoded_inst_operands(&xedd); xed_operand_values_set_mode(ov, &state); xed_encoder_request_set_effective_address_size(enc_req, 64); // need to fix base regs... //xed_operand_values_set_operand_reg(ov, XED_OPERAND_BASE0, XED_REG_RSI); xed_encoder_request_set_effective_operand_width(enc_req, 32); } printf("Encoding...\n"); encode_okay = xed_encode(enc_req, array, ilen, &enc_olen); if (encode_okay != XED_ERROR_NONE) { printf("Error code = %s\n", xed_error_enum_t2str(encode_okay)); } else { unsigned int j; printf("Encodable: "); for(j=0;j<enc_olen;j++) { printf("%02x", array[j]); } printf("\n"); } return 0; }
void Disasm::disasm(std::ostream& out, uint8_t* codeStartAddr, uint8_t* codeEndAddr) { #ifdef HAVE_LIBXED auto const endClr = m_opts.m_color.empty() ? "" : ANSI_COLOR_END; char codeStr[MAX_INSTR_ASM_LEN]; xed_uint8_t *frontier; xed_decoded_inst_t xedd; uint64_t codeBase = uint64_t(codeStartAddr); uint64_t ip; // Decode and print each instruction for (frontier = codeStartAddr, ip = (uint64_t)codeStartAddr; frontier < codeEndAddr; ) { xed_decoded_inst_zero_set_mode(&xedd, &m_xedState); xed_decoded_inst_set_input_chip(&xedd, XED_CHIP_INVALID); xed_error_enum_t xed_error = xed_decode(&xedd, frontier, 15); if (xed_error != XED_ERROR_NONE) error("disasm error: xed_decode failed"); // Get disassembled instruction in codeStr auto const syntax = m_opts.m_forceAttSyntax ? XED_SYNTAX_ATT : s_xed_syntax; if (!xed_format_context(syntax, &xedd, codeStr, MAX_INSTR_ASM_LEN, ip, nullptr)) { error("disasm error: xed_format_context failed"); } uint32_t instrLen = xed_decoded_inst_get_length(&xedd); // If it's a jump, we're printing relative offsets, and the dest // is within the range we're printing, add the dest as a relative // offset. std::string jmpComment; auto const cat = xed_decoded_inst_get_category(&xedd); if (cat == XED_CATEGORY_COND_BR || cat == XED_CATEGORY_UNCOND_BR) { if (m_opts.m_relativeOffset) { auto disp = uint64_t(frontier + instrLen + xed_decoded_inst_get_branch_displacement(&xedd) - codeBase); if (disp < uint64_t(codeEndAddr - codeStartAddr)) { jmpComment = folly::format(" # {:#x}", disp).str(); } } } for (int i = 0; i < m_opts.m_indentLevel; ++i) { out << ' '; } out << m_opts.m_color; if (m_opts.m_addresses) { const char* fmt = m_opts.m_relativeOffset ? "{:3x}: " : "{:#10x}: "; out << folly::format(fmt, ip - (m_opts.m_relativeOffset ? codeBase : 0)); } if (m_opts.m_printEncoding) { // print encoding, like in objdump unsigned posi = 0; for (; posi < instrLen; ++posi) { out << folly::format("{:02x} ", (uint8_t)frontier[posi]); } for (; posi < 16; ++posi) { out << " "; } } out << codeStr << jmpComment << endClr << '\n'; frontier += instrLen; ip += instrLen; } #else out << "This binary was compiled without disassembly support\n"; #endif // HAVE_LIBXED }
static string disassemble(UINT64 start, UINT64 stop) { UINT64 pc = start; xed_state_t dstate; xed_syntax_enum_t syntax = XED_SYNTAX_INTEL; xed_error_enum_t xed_error; xed_decoded_inst_t xedd; ostringstream os; if (sizeof(ADDRINT) == 4) xed_state_init(&dstate, XED_MACHINE_MODE_LEGACY_32, XED_ADDRESS_WIDTH_32b, XED_ADDRESS_WIDTH_32b); else xed_state_init(&dstate, XED_MACHINE_MODE_LONG_64, XED_ADDRESS_WIDTH_64b, XED_ADDRESS_WIDTH_64b); /*while( pc < stop )*/ { xed_decoded_inst_zero_set_mode(&xedd, &dstate); UINT32 len = 15; if (stop - pc < 15) len = stop-pc; xed_error = xed_decode(&xedd, reinterpret_cast<const UINT8*>(pc), len); bool okay = (xed_error == XED_ERROR_NONE); iostream::fmtflags fmt = os.flags(); os << std::setfill('0') << "XDIS " << std::hex << std::setw(sizeof(ADDRINT)*2) << pc << std::dec << ": " << std::setfill(' ') << std::setw(4); if (okay) { char buffer[200]; unsigned int dec_len, sp; os << xed_extension_enum_t2str(xed_decoded_inst_get_extension(&xedd)); dec_len = xed_decoded_inst_get_length(&xedd); print_hex_line(buffer, reinterpret_cast<UINT8*>(pc), dec_len); os << " " << buffer; for ( sp=dec_len; sp < 12; sp++) // pad out the instruction bytes os << " "; os << " "; memset(buffer,0,200); int dis_okay = xed_format_context(syntax, &xedd, buffer, 200, pc, 0, 0); if (dis_okay) os << buffer << endl; else os << "Error disasassembling pc 0x" << std::hex << pc << std::dec << endl; pc += dec_len; } else { // print the byte and keep going. UINT8 memval = *reinterpret_cast<UINT8*>(pc); os << "???? " // no extension << std::hex << std::setw(2) << std::setfill('0') << static_cast<UINT32>(memval) << std::endl; pc += 1; } os.flags(fmt); } return os.str(); }
int main(int argc, char** argv) { xed_error_enum_t xed_error; xed_bool_t long_mode = 0; unsigned int bytes = 0; unsigned char itext[XED_MAX_INSTRUCTION_BYTES]; int i; unsigned int u; xed_decoded_inst_t xedd; #define BUFLEN 1000 char buffer[BUFLEN]; xed_bool_t ok; unsigned int isyntax; xed_syntax_enum_t syntax; xed_machine_mode_enum_t mmode; xed_address_width_enum_t stack_addr_width; xed_format_options_t format_options; // one time initialization xed_tables_init(); xed_set_verbosity( 99 ); memset(&format_options,0, sizeof(format_options)); format_options.hex_address_before_symbolic_name=0; format_options.xml_a=0; format_options.omit_unit_scale=0; format_options.no_sign_extend_signed_immediates=0; for(i=1;i<argc;i++) { if (strcmp(argv[i], "-xml") == 0) format_options.xml_a=1; else if (strcmp(argv[i], "-no-unit-scale") == 0) format_options.omit_unit_scale=1; else if (strcmp(argv[i], "-no-sign-extend") == 0) format_options.no_sign_extend_signed_immediates=1; else if (strcmp(argv[i], "-64") == 0) long_mode = 1; else break; } xed_format_set_options( format_options ); /// begin processing of instructions... if (long_mode) { mmode=XED_MACHINE_MODE_LONG_64; stack_addr_width =XED_ADDRESS_WIDTH_64b; } else { mmode=XED_MACHINE_MODE_LEGACY_32; stack_addr_width =XED_ADDRESS_WIDTH_32b; } xed_decoded_inst_zero(&xedd); xed_decoded_inst_set_mode(&xedd, mmode, stack_addr_width); for( ;i < argc; i++) { xed_uint8_t x = (xed_uint8_t)(xed_atoi_hex(argv[i])); assert(bytes < XED_MAX_INSTRUCTION_BYTES); itext[bytes++] = x; } if (bytes == 0) { fprintf(stderr, "Must supply some hex bytes\n"); exit(1); } printf("PARSING BYTES: "); for( u=0;u<bytes; u++) printf("%02x ", XED_STATIC_CAST(unsigned int,itext[u])); printf("\n"); xed_error = xed_decode(&xedd, XED_REINTERPRET_CAST(const xed_uint8_t*,itext), bytes); switch(xed_error) { case XED_ERROR_NONE: break; case XED_ERROR_BUFFER_TOO_SHORT: fprintf(stderr,"Not enough bytes provided\n"); exit(1); case XED_ERROR_GENERAL_ERROR: fprintf(stderr,"Could not decode given input.\n"); exit(1); default: fprintf(stderr,"Unhandled error code %s\n", xed_error_enum_t2str(xed_error)); exit(1); } xed_decoded_inst_dump(&xedd,buffer, BUFLEN); printf("%s\n",buffer); for(isyntax= XED_SYNTAX_XED; isyntax < XED_SYNTAX_LAST; isyntax++) { syntax = XED_STATIC_CAST(xed_syntax_enum_t, isyntax); ok = xed_format_context(syntax, &xedd, buffer, BUFLEN, 0, 0, 0); if (ok) printf("%s syntax: %s\n", xed_syntax_enum_t2str(syntax), buffer); else printf("Error disassembling %s syntax\n", xed_syntax_enum_t2str(syntax)); } return 0; }
void OfflineX86Code::disasm(FILE* file, TCA fileStartAddr, TCA codeStartAddr, uint64_t codeLen, const PerfEventsMap<TCA>& perfEvents, BCMappingInfo bcMappingInfo, bool printAddr /* =true */, bool printBinary /* =false */) { char codeStr[MAX_INSTR_ASM_LEN]; xed_uint8_t* code = (xed_uint8_t*) alloca(codeLen); xed_uint8_t* frontier; TCA ip; TCA r10val = 0; size_t currBC = 0; if (codeLen == 0) return; auto const offset = codeStartAddr - fileStartAddr; if (fseek(file, offset, SEEK_SET)) { error("disasm error: seeking file"); } size_t readLen = fread(code, codeLen, 1, file); if (readLen != 1) { error("Failed to read {} bytes at offset {} from code file due to {}", codeLen, offset, feof(file) ? "EOF" : "read error"); } xed_decoded_inst_t xedd; // Decode and print each instruction for (frontier = code, ip = codeStartAddr; frontier < code + codeLen; ) { xed_decoded_inst_zero_set_mode(&xedd, &xed_state); xed_decoded_inst_set_input_chip(&xedd, XED_CHIP_INVALID); xed_error_enum_t xed_error = xed_decode(&xedd, frontier, 15); if (xed_error != XED_ERROR_NONE) break; // Get disassembled instruction in codeStr if (!xed_format_context(xed_syntax, &xedd, codeStr, MAX_INSTR_ASM_LEN, (uint64_t)ip, nullptr #if XED_ENCODE_ORDER_MAX_ENTRIES != 28 // Newer version of XED library , 0 #endif )) { error("disasm error: xed_format_context failed"); } // Annotate the x86 with its bytecode. currBC = printBCMapping(bcMappingInfo, currBC, (TCA)ip); if (printAddr) printf("%14p: ", ip); uint32_t instrLen = xed_decoded_inst_get_length(&xedd); if (printBinary) { uint32_t i; for (i=0; i < instrLen; i++) { printf("%02X", frontier[i]); } for (; i < 16; i++) { printf(" "); } } // For calls, we try to figure out the destination symbol name. // We look both at relative branches and the pattern: // move r10, IMMEDIATE // call r10 xed_iclass_enum_t iclass = xed_decoded_inst_get_iclass(&xedd); string callDest = ""; if (iclass == XED_ICLASS_CALL_NEAR || iclass == XED_ICLASS_CALL_FAR) { const xed_inst_t *xi = xed_decoded_inst_inst(&xedd); always_assert(xed_inst_noperands(xi) >= 1); const xed_operand_t *opnd = xed_inst_operand(xi, 0); xed_operand_enum_t opndName = xed_operand_name(opnd); if (opndName == XED_OPERAND_RELBR) { if (xed_decoded_inst_get_branch_displacement_width(&xedd)) { xed_int32_t disp = xed_decoded_inst_get_branch_displacement(&xedd); TCA addr = ip + instrLen + disp; callDest = getSymbolName(addr); } } else if (opndName == XED_OPERAND_REG0) { if (xed_decoded_inst_get_reg(&xedd, opndName) == XED_REG_R10) { callDest = getSymbolName(r10val); } } } else if (iclass == XED_ICLASS_MOV) { // Look for moves into r10 and keep r10val updated const xed_inst_t* xi = xed_decoded_inst_inst(&xedd); always_assert(xed_inst_noperands(xi) >= 2); const xed_operand_t *destOpnd = xed_inst_operand(xi, 0); xed_operand_enum_t destOpndName = xed_operand_name(destOpnd); if (destOpndName == XED_OPERAND_REG0 && xed_decoded_inst_get_reg(&xedd, destOpndName) == XED_REG_R10) { const xed_operand_t *srcOpnd = xed_inst_operand(xi, 1); xed_operand_enum_t srcOpndName = xed_operand_name(srcOpnd); if (srcOpndName == XED_OPERAND_IMM0) { TCA addr = (TCA)xed_decoded_inst_get_unsigned_immediate(&xedd); r10val = addr; } } } if (!perfEvents.empty()) { printEventStats((TCA)ip, instrLen, perfEvents); } else { printf("%48s", ""); } printf("%s%s\n", codeStr, callDest.c_str()); frontier += instrLen; ip += instrLen; } }