PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/pin-2.10-43611-gcc.3.4.6-ia32_intel64-linux/extras/xed2-intel64/examples/xed.cpp

https://bitbucket.org/mihaila/bintrace
C++ | 695 lines | 582 code | 69 blank | 44 comment | 122 complexity | 68c7cb7e54672d587d48aadbeb8a6845 MD5 | raw file
Possible License(s): BSD-3-Clause, CC-BY-SA-3.0
  1. /*BEGIN_LEGAL
  2. Intel Open Source License
  3. Copyright (c) 2002-2011 Intel Corporation. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer. Redistributions
  9. in binary form must reproduce the above copyright notice, this list of
  10. conditions and the following disclaimer in the documentation and/or
  11. other materials provided with the distribution. Neither the name of
  12. the Intel Corporation nor the names of its contributors may be used to
  13. endorse or promote products derived from this software without
  14. specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
  19. ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. END_LEGAL */
  27. /// @file xed.cpp
  28. /// @author Mark Charney <mark.charney@intel.com>
  29. ////////////////////////////////////////////////////////////////////////////
  30. extern "C" {
  31. #include "xed-interface.h"
  32. #include "xed-immdis.h"
  33. #include "xed-portability.h"
  34. #include "xed-examples-util.h"
  35. //void xed_decode_traverse_dump_profile();
  36. }
  37. #include "xed-disas-elf.H"
  38. #include "xed-disas-macho.H"
  39. #include "xed-disas-pecoff.H"
  40. #include "xed-disas-raw.H"
  41. #include "xed-enc-lang.H"
  42. #include <vector>
  43. #include <iostream>
  44. #include <iomanip>
  45. #include <sstream>
  46. #include <cstring>
  47. #include <cstdlib>
  48. using namespace std;
  49. int main(int argc, char** argv);
  50. static int intel_asm_emit = 0;
  51. ////////////////////////////////////////////////////////////////////////////
  52. static xed_uint_t disas_decode(const xed_state_t* dstate,
  53. const char* decode_text,
  54. xed_decoded_inst_t* xedd,
  55. xed_uint64_t runtime_address) {
  56. xed_uint8_t hex_decode_text[XED_MAX_INSTRUCTION_BYTES];
  57. xed_uint_t bytes = xed_convert_ascii_to_hex(decode_text, hex_decode_text,XED_MAX_INSTRUCTION_BYTES);
  58. return disas_decode_binary(dstate, hex_decode_text, bytes, xedd, runtime_address);
  59. }
  60. static unsigned int disas_decode_encode(const xed_state_t* dstate,
  61. const char* decode_text,
  62. xed_decoded_inst_t* xedd,
  63. xed_uint64_t runtime_address) {
  64. xed_uint8_t hex_decode_text[XED_MAX_INSTRUCTION_BYTES];
  65. xed_uint_t bytes = xed_convert_ascii_to_hex(decode_text,
  66. hex_decode_text,
  67. XED_MAX_INSTRUCTION_BYTES);
  68. return disas_decode_encode_binary(dstate, hex_decode_text, bytes, xedd, runtime_address);
  69. }
  70. static void print_bytes_pseudo_op(const xed_uint8_t* array, unsigned int olen) {
  71. unsigned int i;
  72. printf(".byte ");
  73. for(i=0;i<olen;i++) {
  74. if (i>0)
  75. printf(",");
  76. printf("0x%02x",static_cast<xed_uint32_t>(array[i]));
  77. }
  78. printf("\n");
  79. }
  80. static void print_intel_asm_emit(const xed_uint8_t* array, unsigned int olen) {
  81. unsigned int i;
  82. for(i=0;i<olen;i++)
  83. printf(" __emit 0x%02x\n",static_cast<xed_uint32_t>(array[i]));
  84. }
  85. static unsigned int disas_encode(const xed_state_t* dstate,
  86. const char* encode_text) {
  87. unsigned int olen=0;
  88. ascii_encode_request_t areq;
  89. areq.dstate = *dstate;
  90. areq.command = encode_text;
  91. xed_encoder_request_t req = parse_encode_request(areq);
  92. char buf[5000];
  93. xed_encode_request_print(&req, buf, 5000);
  94. printf("Request: %s", buf);
  95. xed_uint8_t array[XED_MAX_INSTRUCTION_BYTES];
  96. unsigned int ilen = XED_MAX_INSTRUCTION_BYTES;
  97. xed_error_enum_t r = xed_encode(&req, array, ilen, &olen);
  98. if (r != XED_ERROR_NONE) {
  99. printf("Could not encode: %s\n", encode_text);
  100. printf("Error code was: %s\n", xed_error_enum_t2str(r));
  101. xedex_derror("Dieing");
  102. }
  103. else if (CLIENT_VERBOSE) {
  104. char buf2[XED_HEX_BUFLEN];
  105. xed_print_hex_line(buf2,array, olen,XED_HEX_BUFLEN);
  106. printf("Encodable! %s\n", buf2);
  107. if (intel_asm_emit)
  108. print_intel_asm_emit(array,olen);
  109. else
  110. print_bytes_pseudo_op(array,olen);
  111. }
  112. return olen;
  113. }
  114. static void no_comments(char* buf) {
  115. size_t len = strlen(buf);
  116. for(size_t i=0;i<len;i++) {
  117. if (buf[i] == ';' || buf[i] == '#') {
  118. buf[i]= 0; // stomp on it
  119. return;
  120. }
  121. }
  122. }
  123. #include <fstream>
  124. static void xed_assemble(const xed_state_t* dstate,
  125. const char* encode_file_name) {
  126. ifstream infile(encode_file_name);
  127. if (!infile) {
  128. printf("Could not open %s\n", encode_file_name);
  129. xedex_derror("Dieing");
  130. }
  131. char buf[1024];
  132. while(infile.getline(buf,sizeof(buf))) {
  133. printf("; %s\n",buf);
  134. no_comments(buf);
  135. if (strlen(buf) == 0)
  136. continue;
  137. unsigned int olen=0;
  138. ascii_encode_request_t areq;
  139. areq.dstate = *dstate;
  140. areq.command = buf;
  141. xed_encoder_request_t req = parse_encode_request(areq);
  142. xed_uint8_t array[XED_MAX_INSTRUCTION_BYTES];
  143. unsigned int ilen = XED_MAX_INSTRUCTION_BYTES;
  144. xed_error_enum_t r = xed_encode(&req, array, ilen, &olen);
  145. if (r != XED_ERROR_NONE) {
  146. printf("Could not encode: %s\n", buf);
  147. printf("Error code was: %s\n", xed_error_enum_t2str(r));
  148. xedex_derror("Dieing");
  149. }
  150. printf(" .byte ");
  151. for(unsigned int i=0;i<olen;i++) {
  152. if (i > 0)
  153. printf(", ");
  154. printf("0x%02x",array[i]);
  155. }
  156. printf("\n");
  157. }
  158. }
  159. static void usage(char* prog) {
  160. unsigned int i;
  161. static const char* usage_msg[] = {
  162. "One of the following is required:",
  163. "\t-i input_file (decode file)",
  164. "\t-ir raw_input_file (decode a raw unformatted binary file)",
  165. "\t-ide input_file (decode/encode file)",
  166. "\t-d hex-string (decode one instruction, must be last)",
  167. "\t-e instruction (encode, must be last)",
  168. "\t-ie file-to-assemble (assemble the contents of the file)",
  169. "\t-de hex-string (decode-then-encode, must be last)",
  170. "",
  171. "Optional arguments:",
  172. "",
  173. "\t-v N (0=quiet, 1=errors, 2=useful-info, 3=trace,",
  174. "\t 5=very verbose)",
  175. "\t-xv N (XED engine verbosity, 0...99)",
  176. "\t-s section (target section for file disassembly,",
  177. "\t PECOFF and ELF formats only)",
  178. "",
  179. "\t-n N (number of instructions to decode. Default 100M,",
  180. "\t accepts K/M/G qualifiers)",
  181. " ",
  182. "\t-b addr (Base address offset, for DLLs/shared libraries.",
  183. "\t Use 0x for hex addresses)",
  184. "\t-as addr (Address to start disassembling.",
  185. "\t Use 0x for hex addresses)",
  186. "\t-ae addr (Address to end disassembling.",
  187. "\t Use 0x for hex addresses)",
  188. "\t-resync (Attempt symbol-based resynchronization algorithm",
  189. "\t for disassembly)",
  190. "",
  191. "\t-I (Intel syntax for disassembly)",
  192. "\t-A (ATT SYSV syntax for disassembly)",
  193. "\t-xml (XML formatting)",
  194. "\t-emit (Output __emit statements for the Intel compiler)",
  195. #if defined(XED_DWARF)
  196. "\t-line (Emit line number information, if present)",
  197. #endif
  198. "",
  199. "\t-r (for REAL_16 mode, 16b addressing (20b addresses),",
  200. "\t 16b default data size)",
  201. "\t-16 (for LEGACY_16 mode, 16b addressing,",
  202. "\t 16b default data size)",
  203. "\t-32 (for LEGACY_32 mode, 32b addressing,",
  204. "\t 32b default data size -- default)",
  205. "\t-64 (for LONG_64 mode w/64b addressing",
  206. "\t Optional on windows/linux)",
  207. "\t-s32 (32b stack addressing, default, not in LONG_64 mode)",
  208. "\t-s16 (16b stack addressing, not in LONG_64 mode)",
  209. " ",
  210. 0
  211. };
  212. printf("%s\n", xed_get_copyright());
  213. printf("XED version: [%s]\n\n", xed_get_version());
  214. cerr << "Usage: " << prog << " [options]" << endl;
  215. for(i=0; usage_msg[i] ; i++)
  216. cerr << usage_msg[i] << endl;
  217. }
  218. #if 1
  219. void test_immdis() {
  220. char buf[1000];
  221. xed_int64_t sv;
  222. xed_immdis_t i;
  223. xed_uint64_t v = 0x11223344;
  224. xed_uint32_t uv =0;
  225. xed_immdis_init(&i,8);
  226. xed_immdis_add_shortest_width_signed(&i,v,4);
  227. xed_immdis_print(&i,buf,1000);
  228. printf("%s\n",buf);
  229. xed_immdis_zero(&i);
  230. xed_immdis_add_shortest_width_unsigned(&i,uv,5);
  231. xed_immdis_print(&i,buf,1000);
  232. printf("%s\n",buf);
  233. xed_immdis_zero(&i);
  234. sv = 0x90;
  235. xed_immdis_add_shortest_width_signed(&i,sv,5);
  236. xed_immdis_print(&i,buf,1000);
  237. printf("%s\n",buf);
  238. xed_immdis_zero(&i);
  239. sv = -128;
  240. xed_immdis_add_shortest_width_signed(&i,sv,5);
  241. xed_immdis_print(&i,buf,1000);
  242. printf("%s\n",buf);
  243. xed_immdis_zero(&i);
  244. sv = -127;
  245. xed_immdis_add_shortest_width_signed(&i,sv,5);
  246. xed_immdis_print(&i,buf,1000);
  247. printf("%s\n",buf);
  248. xed_immdis_zero(&i);
  249. uv = 0x80000000;
  250. xed_immdis_add_shortest_width_unsigned(&i,uv,5);
  251. xed_immdis_print(&i,buf,1000);
  252. printf("%s\n",buf);
  253. #if (!defined(_MSC_VER)) && (!defined(__FreeBSD__)) // MSVS6 VC98 chokes on the LL. Just punt on MS compilers
  254. xed_immdis_zero(&i);
  255. sv = 0xffffffff81223344LL;
  256. xed_immdis_add_shortest_width_signed(&i,sv,4);
  257. xed_immdis_print(&i,buf,1000);
  258. printf("%s\n",buf);
  259. v = 0x1122334455667788ULL;
  260. xed_immdis_zero(&i);
  261. xed_immdis_add_shortest_width_signed(&i,sv,4);
  262. xed_immdis_print(&i,buf,1000);
  263. printf("%s\n",buf);
  264. #endif
  265. v = 0x11223344;
  266. xed_immdis_zero(&i);
  267. xed_immdis_add_shortest_width_signed(&i,v,4);
  268. xed_immdis_print(&i,buf,1000);
  269. printf("%s\n",buf);
  270. v = 0x112233;
  271. xed_immdis_zero(&i);
  272. xed_immdis_add_shortest_width_signed(&i,v,4);
  273. xed_immdis_print(&i,buf,1000);
  274. printf("%s\n",buf);
  275. v = 0x1122;
  276. xed_immdis_zero(&i);
  277. xed_immdis_add_shortest_width_signed(&i,v,4);
  278. xed_immdis_print(&i,buf,1000);
  279. printf("%s\n",buf);
  280. v = 0x11;
  281. xed_immdis_zero(&i);
  282. xed_immdis_add_shortest_width_signed(&i,v,4);
  283. xed_immdis_print(&i,buf,1000);
  284. printf("%s\n",buf);
  285. v = 0x1122;
  286. xed_immdis_zero(&i);
  287. xed_immdis_add_shortest_width_signed(&i,v,3);
  288. xed_immdis_print(&i,buf,1000);
  289. printf("%s\n",buf);
  290. v = 0xffff;
  291. xed_immdis_zero(&i);
  292. xed_immdis_add_shortest_width_signed(&i,v,2);
  293. xed_immdis_print(&i,buf,1000);
  294. printf("%s\n",buf);
  295. v = 0xff00;
  296. xed_immdis_zero(&i);
  297. xed_immdis_add_shortest_width_signed(&i,v,2);
  298. xed_immdis_print(&i,buf,1000);
  299. printf("%s\n",buf);
  300. v = 0xff77;
  301. xed_immdis_zero(&i);
  302. xed_immdis_add_shortest_width_signed(&i,v,7);
  303. xed_immdis_print(&i,buf,1000);
  304. printf("%s\n",buf);
  305. v = 0xff7777;
  306. xed_immdis_zero(&i);
  307. xed_immdis_add_shortest_width_signed(&i,v,5);
  308. xed_immdis_print(&i,buf,1000);
  309. printf("%s\n",buf);
  310. v = 0xff8000;
  311. xed_immdis_zero(&i);
  312. xed_immdis_add_shortest_width_signed(&i,v,7);
  313. xed_immdis_print(&i,buf,1000);
  314. printf("%s\n",buf);
  315. exit(1);
  316. }
  317. #endif
  318. void remove_spaces(string& s) {
  319. string::size_type i,p=0,len = s.size();
  320. for(i=0;i<len;i++)
  321. if (s[i] != ' ')
  322. s[p++]=s[i];
  323. s = s.substr(0,p);
  324. }
  325. void test_argc(int i, int argc) {
  326. if (i+1 >= argc)
  327. xedex_derror("Need more arguments. Use \"xed -help\" for usage.");
  328. }
  329. void late_init(xed_decoded_inst_t* xedd) {
  330. /* add whatever special init you may require here */
  331. /* future expansion */
  332. (void) xedd; /* pacify compiler */
  333. }
  334. int main(int argc, char** argv) {
  335. xed_bool_t sixty_four_bit = false;
  336. xed_bool_t decode_only = true;
  337. char* input_file_name = 0;
  338. string decode_text("");
  339. string encode_text("");
  340. xed_state_t dstate;
  341. xed_bool_t encode = false;
  342. unsigned int ninst = 100*1000*1000; // FIXME: should use maxint...
  343. xed_bool_t decode_encode = false;
  344. int i,j;
  345. unsigned int loop_decode = 0;
  346. xed_bool_t decode_raw = false;
  347. xed_bool_t assemble = false;
  348. char* target_section = 0;
  349. xed_bool_t use_binary_mode = true;
  350. xed_int64_t addr_start = 0;
  351. xed_int64_t addr_end = 0;
  352. xed_int64_t fake_base = 0;
  353. xed_bool_t xml_format =0;
  354. xed_bool_t resync = 0;
  355. int line_numbers = 0;
  356. /* I have this here to test the functionality, if you are happy with
  357. * the XED formatting options, then you do not need to set this or call
  358. * xed_format_set_options() */
  359. xed_format_options_t format_options = {
  360. #if defined(XED_NO_HEX_BEFORE_SYMBOLIC_NAMES)
  361. 0 /* no hex number before symbolic names */
  362. #else
  363. 1 /* hex number before symbolic names -- default */
  364. #endif
  365. , 0 /* normal formatting */
  366. };
  367. xed_state_init(&dstate,
  368. XED_MACHINE_MODE_LEGACY_32,
  369. XED_ADDRESS_WIDTH_32b, /* 2nd parameter ignored */
  370. XED_ADDRESS_WIDTH_32b);
  371. client_verbose = 3;
  372. xed_set_verbosity( client_verbose );
  373. for( i=1; i < argc ; i++ ) {
  374. if (strcmp(argv[i], "-resync") ==0) {
  375. resync = 1;
  376. continue;
  377. }
  378. else if (strcmp(argv[i],"-d")==0) {
  379. test_argc(i,argc);
  380. for(j=i+1; j< argc;j++)
  381. decode_text = decode_text + argv[j];
  382. break; // leave the i=1...argc loop
  383. }
  384. else if (strcmp(argv[i],"-de")==0) {
  385. test_argc(i,argc);
  386. decode_encode = true;
  387. for(j=i+1; j< argc;j++)
  388. decode_text = decode_text + argv[j];
  389. break; // leave the i=1...argc loop
  390. }
  391. else if (strcmp(argv[i],"-i")==0) {
  392. test_argc(i,argc);
  393. input_file_name = argv[i+1];
  394. i++;
  395. }
  396. else if (strcmp(argv[i],"-s")==0) {
  397. test_argc(i,argc);
  398. target_section = argv[i+1];
  399. i++;
  400. }
  401. else if (strcmp(argv[i],"-xml")==0) {
  402. test_argc(i,argc);
  403. format_options.xml_a = 1;
  404. format_options.xml_f = 1;
  405. xml_format = 1;
  406. }
  407. #if defined(XED_DWARF)
  408. else if (strcmp(argv[i],"-line")==0) {
  409. test_argc(i,argc);
  410. line_numbers = 1;
  411. }
  412. #endif
  413. else if (strcmp(argv[i],"-ir")==0) {
  414. test_argc(i,argc);
  415. input_file_name = argv[i+1];
  416. decode_raw = true;
  417. i++;
  418. }
  419. else if (strcmp(argv[i],"-ie")==0) {
  420. test_argc(i,argc);
  421. input_file_name = argv[i+1];
  422. assemble = true;
  423. i++;
  424. }
  425. else if (strcmp(argv[i],"-ide")==0) {
  426. test_argc(i,argc);
  427. input_file_name = argv[i+1];
  428. decode_only = false;
  429. i++;
  430. }
  431. else if (strcmp(argv[i],"-n") ==0) {
  432. test_argc(i,argc);
  433. ninst = XED_STATIC_CAST(unsigned int,xed_atoi_general(argv[i+1],1000));
  434. i++;
  435. }
  436. else if (strcmp(argv[i],"-b") ==0) {
  437. test_argc(i,argc);
  438. fake_base = xed_atoi_general(argv[i+1],1000);
  439. printf("ASSUMED BASE = " XED_FMT_LX "\n",fake_base);
  440. i++;
  441. }
  442. else if (strcmp(argv[i],"-as") == 0 || strcmp(argv[i],"-sa") == 0) {
  443. test_argc(i,argc);
  444. addr_start = XED_STATIC_CAST(xed_int64_t,xed_atoi_general(argv[i+1],1000));
  445. i++;
  446. }
  447. else if (strcmp(argv[i],"-ae") == 0 || strcmp(argv[i],"-ea") == 0) {
  448. test_argc(i,argc);
  449. addr_end = XED_STATIC_CAST(xed_int64_t,xed_atoi_general(argv[i+1],1000));
  450. i++;
  451. }
  452. else if (strcmp(argv[i],"-loop") ==0) {
  453. test_argc(i,argc);
  454. loop_decode = XED_STATIC_CAST(unsigned int,xed_atoi_general(argv[i+1],1000));
  455. i++;
  456. }
  457. else if (strcmp(argv[i],"-v") ==0) {
  458. test_argc(i,argc);
  459. client_verbose = XED_STATIC_CAST(int,xed_atoi_general(argv[i+1],1000));
  460. xed_set_verbosity(client_verbose);
  461. i++;
  462. }
  463. else if (strcmp(argv[i],"-xv") ==0) {
  464. test_argc(i,argc);
  465. unsigned int xed_engine_verbose = XED_STATIC_CAST(unsigned int,xed_atoi_general(argv[i+1],1000));
  466. xed_set_verbosity(xed_engine_verbose);
  467. i++;
  468. }
  469. else if (strcmp(argv[i],"-A")==0) {
  470. att_syntax = true;
  471. xed_syntax = intel_syntax = false;
  472. }
  473. else if (strcmp(argv[i],"-I")==0) {
  474. intel_syntax = true;
  475. xed_syntax = att_syntax = false;
  476. }
  477. else if (strcmp(argv[i],"-X") == 0) { // undocumented
  478. xed_syntax = true;
  479. intel_syntax = true;
  480. att_syntax = true;
  481. }
  482. else if (strcmp(argv[i],"-r")==0) {
  483. sixty_four_bit = false;
  484. dstate.mmode = XED_MACHINE_MODE_REAL_16;
  485. dstate.stack_addr_width = XED_ADDRESS_WIDTH_16b;
  486. use_binary_mode = false;
  487. }
  488. else if (strcmp(argv[i],"-16")==0) {
  489. sixty_four_bit = false;
  490. dstate.mmode = XED_MACHINE_MODE_LEGACY_16;
  491. use_binary_mode = false;
  492. }
  493. else if (strcmp(argv[i],"-32")==0) { // default
  494. sixty_four_bit = false;
  495. dstate.mmode = XED_MACHINE_MODE_LEGACY_32;
  496. use_binary_mode = false;
  497. }
  498. else if (strcmp(argv[i],"-64")==0) {
  499. sixty_four_bit = true;
  500. dstate.mmode = XED_MACHINE_MODE_LONG_64;
  501. use_binary_mode = false;
  502. }
  503. else if (strcmp(argv[i],"-s32")==0) {
  504. dstate.stack_addr_width = XED_ADDRESS_WIDTH_32b;
  505. use_binary_mode = false;
  506. }
  507. else if (strcmp(argv[i],"-s16")==0) {
  508. dstate.stack_addr_width = XED_ADDRESS_WIDTH_16b;
  509. use_binary_mode = false;
  510. }
  511. else if (strcmp(argv[i],"-ti") ==0) {
  512. client_verbose = 5;
  513. xed_set_verbosity(5);
  514. test_immdis();
  515. exit(1);
  516. }
  517. else if (strcmp(argv[i],"-emit") ==0) {
  518. intel_asm_emit = 1;
  519. }
  520. else if (strcmp(argv[i],"-e") ==0) {
  521. encode = true;
  522. test_argc(i,argc);
  523. // merge the rest of the args in to the encode_text string.
  524. for( j = i+1; j< argc; j++ )
  525. encode_text = encode_text + argv[j] + " ";
  526. break; // leave the loop
  527. }
  528. else {
  529. usage(argv[0]);
  530. exit(1);
  531. }
  532. }
  533. if (!encode) {
  534. if (input_file_name == 0 && decode_text == "") {
  535. cerr << "ERROR: required argument(s) were missing" << endl;
  536. usage(argv[0]);
  537. exit(1);
  538. }
  539. }
  540. if (CLIENT_VERBOSE2)
  541. printf("Initializing XED tables...\n");
  542. xed_tables_init();
  543. if (CLIENT_VERBOSE2)
  544. printf("Done initialing XED tables.\n");
  545. xed_format_set_options(format_options);
  546. if (CLIENT_VERBOSE1)
  547. printf("#XED version: [%s]\n", xed_get_version());
  548. xed_decoded_inst_t xedd;
  549. xed_decoded_inst_zero_set_mode(&xedd, &dstate);
  550. late_init(&xedd);
  551. xed_uint_t retval_okay = 1;
  552. unsigned int obytes=0;
  553. if (assemble) {
  554. xed_assemble(&dstate, input_file_name);
  555. }
  556. else if (decode_encode) {
  557. obytes = disas_decode_encode(&dstate, decode_text.c_str(), &xedd, fake_base);
  558. retval_okay = (obytes != 0) ? 1 : 0;
  559. }
  560. else if (encode)
  561. obytes = disas_encode(&dstate, encode_text.c_str());
  562. else if (decode_text != "") {
  563. if (loop_decode) {
  564. unsigned int k;
  565. for(k=0;k<loop_decode;k++) {
  566. retval_okay = disas_decode(&dstate, decode_text.c_str(), &xedd, fake_base);
  567. xed_decoded_inst_zero_set_mode(&xedd, &dstate);
  568. late_init(&xedd);
  569. //xed_decode_traverse_dump_profile();
  570. }
  571. }
  572. else {
  573. remove_spaces(decode_text);
  574. const char* p = decode_text.c_str();
  575. int remaining = static_cast<int>(decode_text.size() / 2); // 2 bytes per nibble
  576. do {
  577. retval_okay = disas_decode(&dstate, p, &xedd, fake_base);
  578. unsigned int len = xed_decoded_inst_get_length(&xedd);
  579. p+=len*2;
  580. remaining -= len;
  581. }
  582. while(retval_okay && remaining > 0);
  583. }
  584. }
  585. else {
  586. xed_decode_file_info_t decode_info;
  587. xed_decode_file_info_init(&decode_info,
  588. input_file_name, dstate, ninst,
  589. decode_only, sixty_four_bit,
  590. target_section, use_binary_mode,
  591. addr_start, addr_end, late_init,
  592. xml_format, fake_base, resync,
  593. line_numbers);
  594. if (xml_format) {
  595. printf("<?xml version=\"1.0\"?>\n");
  596. printf("<XEDDISASM>\n");
  597. printf("<XEDFORMAT>1</XEDFORMAT>\n");
  598. }
  599. if (decode_raw) {
  600. xed_disas_raw(&decode_info);
  601. }
  602. else {
  603. #if defined(XED_MAC_OSX_FILE_READER)
  604. xed_disas_macho(&decode_info);
  605. #elif defined(XED_ELF_READER)
  606. xed_disas_elf(&decode_info);
  607. #elif defined(XED_PECOFF_FILE_READER)
  608. xed_disas_pecoff(&decode_info);
  609. #else
  610. xedex_derror("No PECOFF, ELF or MACHO support compiled in");
  611. #endif
  612. }
  613. }
  614. //xed_decode_traverse_dump_profile();
  615. if (xml_format)
  616. printf("</XEDDISASM>\n");
  617. if (retval_okay==0)
  618. exit(1);
  619. return 0;
  620. (void) obytes;
  621. }
  622. ////////////////////////////////////////////////////////////////////////////