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