main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (C) 2017 bzt (bztsrc@gitlab)
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. * @brief Example code on how to use the Universal Disassembler Function Library
  25. */
  26. #include <stddef.h>
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "aarch64.h"
  32. //#include "z80.h"
  33. //#include "java.h"
  34. //#include "x86_64.h"
  35. // required if header generated with -i
  36. uint8_t sys_fault=0;
  37. uint64_t dbg_label, buf_reloc=0;
  38. int main(int argc, char **argv)
  39. {
  40. FILE *f;
  41. long int s;
  42. uint64_t ptr, end;
  43. char *data, str[1024];
  44. if(argc<2) {
  45. fprintf(stderr, "Universal Disassembler by bzt 2017\n\nUsage: %s <raw AArch64 binary>\n", argv[0]);
  46. exit(1);
  47. }
  48. // read input
  49. f=fopen(argv[1], "r");
  50. if(f){
  51. fseek(f, 0L, SEEK_END);
  52. s=ftell(f);
  53. fseek(f, 0L, SEEK_SET);
  54. data=(char*)malloc(s+1);
  55. if(data==NULL) {
  56. fprintf(stderr, "Unable to allocate %ld memory\n", s+1);
  57. exit(2);
  58. }
  59. fread(data, s, 1, f);
  60. fclose(f);
  61. } else {
  62. fprintf(stderr, "File not found %s\n", argv[1]);
  63. exit(1);
  64. }
  65. ptr = buf_reloc = (uint64_t)data;
  66. end = ptr + s;
  67. // disassemble instructions
  68. while (ptr<end && ptr!=0) {
  69. // print out address and instruction in hex
  70. #ifdef disasm_integration
  71. // if header generated with "-i", addresses will be relative to buffer
  72. printf("%08x: %08x ", (int)(ptr - buf_reloc), *((uint32_t *)ptr));
  73. #else
  74. printf("%08x: %08x ", (int)ptr, *((uint32_t *)ptr));
  75. #endif
  76. // now do the thing, call the library
  77. #ifdef disasm_snprintf
  78. // if header generated with "-n", there's an additional buffer size argument
  79. ptr = disasm(ptr, (char*)&str, sizeof(str));
  80. // print the disassembled instruction buffer
  81. if (ptr!=0)
  82. printf("%s\n", str);
  83. else
  84. printf("BUFFER TOO SMALL\n");
  85. #else
  86. // print the disassembled instruction buffer
  87. ptr = disasm(ptr, (char*)&str);
  88. printf("%s\n", str);
  89. #endif
  90. }
  91. }