• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014, 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 <regex>
28 
29 #include "custom-disassembler.h"
30 #include "examples.h"
31 
32 using namespace vixl;
33 using namespace vixl::aarch64;
34 
35 #define __ masm->
36 
37 
38 // We override this method to specify how register names should be disassembled.
AppendRegisterNameToOutput(const Instruction * instr,const CPURegister & reg)39 void CustomDisassembler::AppendRegisterNameToOutput(const Instruction* instr,
40                                                     const CPURegister& reg) {
41   USE(instr);
42   if (reg.IsRegister()) {
43     switch (reg.GetCode()) {
44       case 16:
45         AppendToOutput(reg.Is64Bits() ? "ip0" : "wip0");
46         return;
47       case 17:
48         AppendToOutput(reg.Is64Bits() ? "ip1" : "wip1");
49         return;
50       case 30:
51         AppendToOutput(reg.Is64Bits() ? "lr" : "w30");
52         return;
53       case kSPRegInternalCode:
54         AppendToOutput(reg.Is64Bits() ? "x_stack_pointer" : "w_stack_pointer");
55         return;
56       case 31:
57         AppendToOutput(reg.Is64Bits() ? "x_zero_reg" : "w_zero_reg");
58         return;
59       default:
60         // Fall through.
61         break;
62     }
63   }
64   // Print other register names as usual.
65   Disassembler::AppendRegisterNameToOutput(instr, reg);
66 }
67 
68 
FakeLookupTargetDescription(const void * address)69 static const char* FakeLookupTargetDescription(const void* address) {
70   USE(address);
71   // We fake looking up the address.
72   static int i = 0;
73   const char* desc = NULL;
74   if (i == 0) {
75     desc = "label: somewhere";
76   } else if (i == 2) {
77     desc = "label: somewhere else";
78   }
79   i++;
80   return desc;
81 }
82 
83 
84 // We override this method to add a description to addresses that we know about.
85 // In this example we fake looking up a description, but in practice one could
86 // for example use a table mapping addresses to function names.
AppendCodeRelativeCodeAddressToOutput(const Instruction * instr,const void * addr)87 void CustomDisassembler::AppendCodeRelativeCodeAddressToOutput(
88     const Instruction* instr, const void* addr) {
89   USE(instr);
90   // Print the address.
91   int64_t rel_addr = CodeRelativeAddress(addr);
92   if (rel_addr >= 0) {
93     AppendToOutput("(addr 0x%" PRIx64, rel_addr);
94   } else {
95     AppendToOutput("(addr -0x%" PRIx64, -rel_addr);
96   }
97 
98   // If available, print a description of the address.
99   const char* address_desc = FakeLookupTargetDescription(addr);
100   if (address_desc != NULL) {
101     Disassembler::AppendToOutput(" ; %s", address_desc);
102   }
103   AppendToOutput(")");
104 }
105 
106 
107 // We override this method to add a comment to some instructions. Helpers from
108 // the vixl::Instruction class can be used to analyse the instruction being
109 // disasssembled.
Visit(Metadata * metadata,const Instruction * instr)110 void CustomDisassembler::Visit(Metadata* metadata, const Instruction* instr) {
111   vixl::aarch64::Disassembler::Visit(metadata, instr);
112   const std::string& form = (*metadata)["form"];
113 
114   // Match the forms for 32/64-bit add/subtract with shift, with optional flag
115   // setting.
116   if (std::regex_match(form,  // NOLINT: avoid clang-tidy-4.0 errors.
117                        std::regex("(?:add|sub)s?_(?:32|64)_addsub_shift"))) {
118     if (instr->GetRd() == 10) {
119       AppendToOutput(" // add/sub to x10");
120     }
121   }
122   ProcessOutput(instr);
123 }
124 
125 
GenerateCustomDisassemblerTestCode(MacroAssembler * masm)126 void GenerateCustomDisassemblerTestCode(MacroAssembler* masm) {
127   // Generate some code to illustrate how the modified disassembler changes the
128   // disassembly output.
129   Label begin, end;
130   __ Bind(&begin);
131   __ Add(x10, x16, x17);
132   __ Cbz(x10, &end);
133   __ Add(x11, ip0, ip1);
134   __ Add(w5, w6, w30);
135   __ Tbz(x10, 2, &begin);
136   __ Tbnz(x10, 3, &begin);
137   __ Br(x30);
138   __ Br(lr);
139   __ Fadd(d30, d16, d17);
140   __ Push(xzr, xzr);
141   __ Pop(x16, x20);
142   __ Bind(&end);
143 }
144 
145 
TestCustomDisassembler()146 void TestCustomDisassembler() {
147   MacroAssembler masm;
148 
149   // Generate the code.
150   Label code_start, code_end;
151   masm.Bind(&code_start);
152   GenerateCustomDisassemblerTestCode(&masm);
153   masm.Bind(&code_end);
154   masm.FinalizeCode();
155   Instruction* instr_start = masm.GetLabelAddress<Instruction*>(&code_start);
156   Instruction* instr_end = masm.GetLabelAddress<Instruction*>(&code_end);
157 
158   // Instantiate a standard disassembler, our custom disassembler, and register
159   // them with a decoder.
160   Decoder decoder;
161   Disassembler disasm;
162   CustomDisassembler custom_disasm;
163   decoder.AppendVisitor(&disasm);
164   decoder.AppendVisitor(&custom_disasm);
165 
166   // In our custom disassembler, disassemble as if the base address was -0x8.
167   // Note that this can also be achieved with
168   //   custom_disasm.MapCodeAddress(0x0, instr_start + 2 * kInstructionSize);
169   // Users may generally want to map the start address to 0x0. Mapping to a
170   // negative offset can be used to focus on the section of the
171   // disassembly at address 0x0.
172   custom_disasm.MapCodeAddress(-0x8, instr_start);
173 
174   // Iterate through the instructions to show the difference in the disassembly.
175   Instruction* instr;
176   for (instr = instr_start; instr < instr_end; instr += kInstructionSize) {
177     decoder.Decode(instr);
178     printf("\n");
179     printf("VIXL disasm\t %p:\t%s\n",
180            reinterpret_cast<void*>(instr),
181            disasm.GetOutput());
182     int64_t rel_addr =
183         custom_disasm.CodeRelativeAddress(reinterpret_cast<void*>(instr));
184     char rel_addr_sign_char = ' ';
185     if (rel_addr < 0) {
186       rel_addr_sign_char = '-';
187       rel_addr = -rel_addr;
188     }
189     printf("custom disasm\t%c0x%" PRIx64 ":\t%s\n",
190            rel_addr_sign_char,
191            rel_addr,
192            custom_disasm.GetOutput());
193   }
194 }
195 
196 
197 #ifndef TEST_EXAMPLES
main()198 int main() {
199   TestCustomDisassembler();
200   return 0;
201 }
202 #endif
203