1 // Copyright 2020, VIXL authors
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 // * Neither the name of ARM Limited nor the names of its contributors may be
13 // used to endorse or promote products derived from this software without
14 // specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (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
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "code-buffer-vixl.h"
33
34 #include "aarch64/decoder-aarch64.h"
35 #include "aarch64/disasm-aarch64.h"
36
37 // This example is interactive, and isn't tested systematically.
38 #ifndef TEST_EXAMPLES
39
40 using namespace vixl;
41 using namespace vixl::aarch64;
42
PrintUsage(char const * name)43 void PrintUsage(char const* name) {
44 printf("Usage: %s [OPTION]... <INSTRUCTION>...\n", name);
45 printf("\n");
46 printf("Disassemble ad-hoc A64 instructions.\n");
47 printf("\n");
48 printf(
49 "Options:\n"
50 " --start-at <address>\n"
51 " Start disassembling from <address> Any signed 64-bit value\n"
52 " accepted by strtoll can be specified. The address is printed\n"
53 " alongside each instruction, and it is also used to decode\n"
54 " PC-relative offsets.\n"
55 "\n"
56 " Defaults to 0.\n"
57 "\n");
58 printf(
59 "<instruction>\n"
60 " A hexadecimal representation of an A64 instruction. The leading '0x'\n"
61 " (or '0X') is optional.\n"
62 "\n"
63 " Multiple instructions can be provided; they will be disassembled as\n"
64 " if they were read sequentially from memory.\n"
65 "\n");
66 printf("Examples:\n");
67 printf(" $ %s d2824685\n", name);
68 printf(" 0x0000000000000000: d2824685 movz x5, #0x1234\n");
69 printf("\n");
70 printf(" $ %s --start-at -4 0x10fffe85 0xd61f00a0\n", name);
71 printf(" -0x0000000000000004: 10fffe85 adr x5, #-0x30 (addr -0x34)\n");
72 printf(" 0x0000000000000000: d61f00a0 br x5\n");
73 }
74
ParseInstr(char const * arg)75 Instr ParseInstr(char const* arg) {
76 // TODO: Error handling for out-of-range inputs.
77 return (Instr)strtoul(arg, NULL, 16);
78 }
79
ParseInt64(char const * arg)80 int64_t ParseInt64(char const* arg) {
81 // TODO: Error handling for out-of-range inputs.
82 return (int64_t)strtoll(arg, NULL, 0);
83 }
84
main(int argc,char * argv[])85 int main(int argc, char* argv[]) {
86 for (int i = 1; i < argc; i++) {
87 char const* arg = argv[i];
88 if ((strcmp(arg, "--help") == 0) || (strcmp(arg, "-h") == 0)) {
89 PrintUsage(argv[0]);
90 return 0;
91 }
92 }
93
94 // Assume an address of 0, unless otherwise specified.
95 int64_t start_address = 0;
96 // Allocate space for one instruction per argument.
97 CodeBuffer buffer((argc - 1) * kInstructionSize);
98
99 bool expect_start_at = false;
100 for (int i = 1; i < argc; i++) {
101 char* arg = argv[i];
102 if (expect_start_at) {
103 start_address = ParseInt64(arg);
104 expect_start_at = false;
105 } else if (strcmp(arg, "--start-at") == 0) {
106 expect_start_at = true;
107 } else {
108 // Assume that everything else is an instruction.
109 buffer.Emit(ParseInstr(arg));
110 }
111 }
112 buffer.SetClean();
113
114 if (expect_start_at) {
115 printf("No address given. Use: --start-at <address>\n");
116 return 1;
117 }
118
119 if (buffer.GetSizeInBytes() == 0) {
120 printf("Nothing to disassemble.\n");
121 return 0;
122 }
123
124 // Disassemble the buffer.
125 const Instruction* start = buffer.GetStartAddress<Instruction*>();
126 const Instruction* end = buffer.GetEndAddress<Instruction*>();
127 vixl::aarch64::PrintDisassembler disasm(stdout);
128 disasm.PrintSignedAddresses(true);
129 disasm.MapCodeAddress(start_address, start);
130 disasm.DisassembleBuffer(start, end);
131
132 return 0;
133 }
134
135 #endif // TEST_EXAMPLES
136