xref: /aosp_15_r20/external/vixl/examples/aarch64/disasm.cc (revision f5c631da2f1efdd72b5fd1e20510e4042af13d77)
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 #include "aarch64/decoder-aarch64.h"
34 #include "aarch64/disasm-aarch64.h"
35 
36 // This example is interactive, and isn't tested systematically.
37 #ifndef TEST_EXAMPLES
38 
39 using namespace vixl;
40 using namespace vixl::aarch64;
41 
PrintUsage(char const * name)42 void PrintUsage(char const* name) {
43   printf("Usage: %s [OPTION]... <INSTRUCTION>...\n", name);
44   printf("\n");
45   printf("Disassemble ad-hoc A64 instructions.\n");
46   printf("\n");
47   printf(
48       "Options:\n"
49       "  --start-at <address>\n"
50       "    Start disassembling from <address> Any signed 64-bit value\n"
51       "    accepted by strtoll can be specified. The address is printed\n"
52       "    alongside each instruction, and it is also used to decode\n"
53       "    PC-relative offsets.\n"
54       "\n"
55       "    Defaults to 0.\n"
56       "\n");
57   printf(
58       "<instruction>\n"
59       "  A hexadecimal representation of an A64 instruction. The leading '0x'\n"
60       "  (or '0X') is optional.\n"
61       "\n"
62       "  Multiple instructions can be provided; they will be disassembled as\n"
63       "  if they were read sequentially from memory.\n"
64       "\n");
65   printf("Examples:\n");
66   printf("  $ %s d2824685\n", name);
67   printf("   0x0000000000000000:  d2824685  movz x5, #0x1234\n");
68   printf("\n");
69   printf("  $ %s --start-at -4 0x10fffe85 0xd61f00a0\n", name);
70   printf("  -0x0000000000000004:  10fffe85  adr x5, #-0x30 (addr -0x34)\n");
71   printf("   0x0000000000000000:  d61f00a0  br x5\n");
72 }
73 
ParseInstr(char const * arg)74 Instr ParseInstr(char const* arg) {
75   // TODO: Error handling for out-of-range inputs.
76   return (Instr)strtoul(arg, NULL, 16);
77 }
78 
ParseInt64(char const * arg)79 int64_t ParseInt64(char const* arg) {
80   // TODO: Error handling for out-of-range inputs.
81   return (int64_t)strtoll(arg, NULL, 0);
82 }
83 
main(int argc,char * argv[])84 int main(int argc, char* argv[]) {
85   for (int i = 1; i < argc; i++) {
86     char const* arg = argv[i];
87     if ((strcmp(arg, "--help") == 0) || (strcmp(arg, "-h") == 0)) {
88       PrintUsage(argv[0]);
89       return 0;
90     }
91   }
92 
93   // Assume an address of 0, unless otherwise specified.
94   int64_t start_address = 0;
95   // Allocate space for one instruction per argument.
96   CodeBuffer buffer((argc - 1) * kInstructionSize);
97 
98   bool expect_start_at = false;
99   for (int i = 1; i < argc; i++) {
100     char* arg = argv[i];
101     if (expect_start_at) {
102       start_address = ParseInt64(arg);
103       expect_start_at = false;
104     } else if (strcmp(arg, "--start-at") == 0) {
105       expect_start_at = true;
106     } else {
107       // Assume that everything else is an instruction.
108       buffer.Emit(ParseInstr(arg));
109     }
110   }
111   buffer.SetClean();
112 
113   if (expect_start_at) {
114     printf("No address given. Use: --start-at <address>\n");
115     return 1;
116   }
117 
118   if (buffer.GetSizeInBytes() == 0) {
119     printf("Nothing to disassemble.\n");
120     return 0;
121   }
122 
123   // Disassemble the buffer.
124   const Instruction* start = buffer.GetStartAddress<Instruction*>();
125   const Instruction* end = buffer.GetEndAddress<Instruction*>();
126   vixl::aarch64::PrintDisassembler disasm(stdout);
127   disasm.PrintSignedAddresses(true);
128   disasm.MapCodeAddress(start_address, start);
129   disasm.DisassembleBuffer(start, end);
130 
131   return 0;
132 }
133 
134 #endif  // TEST_EXAMPLES
135