Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- DisassemblerLLVM.cpp ------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "DisassemblerLLVM.h" |
| 11 | |
| 12 | #include "llvm-c/EnhancedDisassembly.h" |
| 13 | |
| 14 | #include "lldb/Core/Address.h" |
| 15 | #include "lldb/Core/DataExtractor.h" |
| 16 | #include "lldb/Core/Disassembler.h" |
| 17 | #include "lldb/Core/Module.h" |
| 18 | #include "lldb/Core/PluginManager.h" |
| 19 | #include "lldb/Core/Stream.h" |
| 20 | #include "lldb/Core/StreamString.h" |
| 21 | #include "lldb/Symbol/SymbolContext.h" |
| 22 | |
| 23 | #include "lldb/Target/ExecutionContext.h" |
| 24 | #include "lldb/Target/Process.h" |
| 25 | #include "lldb/Target/RegisterContext.h" |
| 26 | #include "lldb/Target/Target.h" |
| 27 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 28 | #include <assert.h> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace lldb; |
| 31 | using namespace lldb_private; |
| 32 | |
| 33 | |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 34 | static int |
Greg Clayton | 7bc3908 | 2011-03-24 23:53:38 +0000 | [diff] [blame] | 35 | DataExtractorByteReader (uint8_t *byte, uint64_t address, void *arg) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | { |
| 37 | DataExtractor &extractor = *((DataExtractor *)arg); |
| 38 | |
| 39 | if (extractor.ValidOffset(address)) |
| 40 | { |
| 41 | *byte = *(extractor.GetDataStart() + address); |
| 42 | return 0; |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | return -1; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | namespace { |
| 51 | struct RegisterReaderArg { |
| 52 | const lldb::addr_t instructionPointer; |
| 53 | const EDDisassemblerRef disassembler; |
| 54 | |
| 55 | RegisterReaderArg(lldb::addr_t ip, |
| 56 | EDDisassemblerRef dis) : |
| 57 | instructionPointer(ip), |
| 58 | disassembler(dis) |
| 59 | { |
| 60 | } |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | static int IPRegisterReader(uint64_t *value, unsigned regID, void* arg) |
| 65 | { |
| 66 | uint64_t instructionPointer = ((RegisterReaderArg*)arg)->instructionPointer; |
| 67 | EDDisassemblerRef disassembler = ((RegisterReaderArg*)arg)->disassembler; |
| 68 | |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 69 | if (EDRegisterIsProgramCounter(disassembler, regID)) { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | *value = instructionPointer; |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | return -1; |
| 75 | } |
| 76 | |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 77 | InstructionLLVM::InstructionLLVM (const Address &addr, |
| 78 | AddressClass addr_class, |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 79 | EDDisassemblerRef disassembler, |
Johnny Chen | 80ab18e | 2011-05-12 22:25:53 +0000 | [diff] [blame] | 80 | llvm::Triple::ArchType arch_type) : |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 81 | Instruction (addr, addr_class), |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 82 | m_disassembler (disassembler), |
Johnny Chen | 80ab18e | 2011-05-12 22:25:53 +0000 | [diff] [blame] | 83 | m_arch_type (arch_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | { |
| 85 | } |
| 86 | |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 87 | InstructionLLVM::~InstructionLLVM() |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 88 | { |
| 89 | } |
| 90 | |
| 91 | static void |
| 92 | PadString(Stream *s, const std::string &str, size_t width) |
| 93 | { |
| 94 | int diff = width - str.length(); |
| 95 | |
| 96 | if (diff > 0) |
| 97 | s->Printf("%s%*.*s", str.c_str(), diff, diff, ""); |
| 98 | else |
| 99 | s->Printf("%s ", str.c_str()); |
| 100 | } |
Johnny Chen | d254eb9 | 2011-05-23 23:29:23 +0000 | [diff] [blame] | 101 | static void |
| 102 | AddSymbolicInfo(const ExecutionContext *exe_ctx, ExecutionContextScope *exe_scope, |
| 103 | StreamString &comment, uint64_t operand_value, const Address &inst_addr) |
| 104 | { |
| 105 | Address so_addr; |
| 106 | if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty()) |
| 107 | { |
| 108 | if (exe_ctx->target->GetSectionLoadList().ResolveLoadAddress(operand_value, so_addr)) |
| 109 | so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | Module *module = inst_addr.GetModule(); |
| 114 | if (module) |
| 115 | { |
| 116 | if (module->ResolveFileAddress(operand_value, so_addr)) |
| 117 | so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset); |
| 118 | } |
| 119 | } |
| 120 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 121 | |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 122 | #include "llvm/ADT/StringRef.h" |
Johnny Chen | d254eb9 | 2011-05-23 23:29:23 +0000 | [diff] [blame] | 123 | static inline void StripSpaces(llvm::StringRef &Str) |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 124 | { |
| 125 | while (!Str.empty() && isspace(Str[0])) |
| 126 | Str = Str.substr(1); |
| 127 | while (!Str.empty() && isspace(Str.back())) |
| 128 | Str = Str.substr(0, Str.size()-1); |
| 129 | } |
Johnny Chen | d254eb9 | 2011-05-23 23:29:23 +0000 | [diff] [blame] | 130 | static inline void RStrip(llvm::StringRef &Str, char c) |
| 131 | { |
| 132 | if (!Str.empty() && Str.back() == c) |
| 133 | Str = Str.substr(0, Str.size()-1); |
| 134 | } |
Johnny Chen | 3930cbe | 2011-05-24 20:36:40 +0000 | [diff] [blame] | 135 | // Aligns the raw disassembly (passed as 'str') with the rest of edis'ed disassembly output. |
| 136 | // This is called from non-raw mode when edis of the current m_inst fails for some reason. |
Johnny Chen | 84d42e8 | 2011-05-21 00:55:57 +0000 | [diff] [blame] | 137 | static void |
Johnny Chen | d17f801 | 2011-05-23 18:00:40 +0000 | [diff] [blame] | 138 | Align(Stream *s, const char *str, size_t opcodeColWidth, size_t operandColWidth) |
Johnny Chen | 84d42e8 | 2011-05-21 00:55:57 +0000 | [diff] [blame] | 139 | { |
| 140 | llvm::StringRef raw_disasm(str); |
| 141 | StripSpaces(raw_disasm); |
Johnny Chen | d17f801 | 2011-05-23 18:00:40 +0000 | [diff] [blame] | 142 | // Split the raw disassembly into opcode and operands. |
| 143 | std::pair<llvm::StringRef, llvm::StringRef> p = raw_disasm.split('\t'); |
| 144 | PadString(s, p.first, opcodeColWidth); |
| 145 | if (!p.second.empty()) |
| 146 | PadString(s, p.second, operandColWidth); |
Johnny Chen | 84d42e8 | 2011-05-21 00:55:57 +0000 | [diff] [blame] | 147 | } |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 148 | |
Johnny Chen | 6678676 | 2011-08-03 04:50:37 +0000 | [diff] [blame] | 149 | #define AlignPC(pc_val) (pc_val & 0xFFFFFFFC) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 150 | void |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 151 | InstructionLLVM::Dump |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 152 | ( |
| 153 | Stream *s, |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 154 | uint32_t max_opcode_byte_size, |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 155 | bool show_address, |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 156 | bool show_bytes, |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 157 | const lldb_private::ExecutionContext* exe_ctx, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 158 | bool raw |
| 159 | ) |
| 160 | { |
| 161 | const size_t opcodeColumnWidth = 7; |
| 162 | const size_t operandColumnWidth = 25; |
| 163 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 164 | ExecutionContextScope *exe_scope = NULL; |
| 165 | if (exe_ctx) |
| 166 | exe_scope = exe_ctx->GetBestExecutionContextScope(); |
| 167 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | // If we have an address, print it out |
Sean Callanan | 91557b0 | 2010-11-10 01:38:28 +0000 | [diff] [blame] | 169 | if (GetAddress().IsValid() && show_address) |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 170 | { |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 171 | if (GetAddress().Dump (s, |
| 172 | exe_scope, |
| 173 | Address::DumpStyleLoadAddress, |
| 174 | Address::DumpStyleModuleWithFileAddress, |
| 175 | 0)) |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 176 | s->PutCString(": "); |
| 177 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 178 | |
| 179 | // If we are supposed to show bytes, "bytes" will be non-NULL. |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 180 | if (show_bytes) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 181 | { |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 182 | if (m_opcode.GetType() == Opcode::eTypeBytes) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 183 | { |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 184 | // x86_64 and i386 are the only ones that use bytes right now so |
| 185 | // pad out the byte dump to be able to always show 15 bytes (3 chars each) |
| 186 | // plus a space |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 187 | if (max_opcode_byte_size > 0) |
| 188 | m_opcode.Dump (s, max_opcode_byte_size * 3 + 1); |
| 189 | else |
| 190 | m_opcode.Dump (s, 15 * 3 + 1); |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 191 | } |
| 192 | else |
| 193 | { |
| 194 | // Else, we have ARM which can show up to a uint32_t 0x00000000 (10 spaces) |
| 195 | // plus two for padding... |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 196 | if (max_opcode_byte_size > 0) |
| 197 | m_opcode.Dump (s, max_opcode_byte_size * 3 + 1); |
| 198 | else |
| 199 | m_opcode.Dump (s, 12); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 203 | int numTokens = -1; |
| 204 | |
Johnny Chen | 80ab18e | 2011-05-12 22:25:53 +0000 | [diff] [blame] | 205 | // FIXME!!! |
| 206 | /* Remove the following section of code related to force_raw .... */ |
Johnny Chen | d17f801 | 2011-05-23 18:00:40 +0000 | [diff] [blame] | 207 | /* |
Johnny Chen | 80ab18e | 2011-05-12 22:25:53 +0000 | [diff] [blame] | 208 | bool force_raw = m_arch_type == llvm::Triple::arm || |
| 209 | m_arch_type == llvm::Triple::thumb; |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 210 | if (!raw) |
Johnny Chen | 80ab18e | 2011-05-12 22:25:53 +0000 | [diff] [blame] | 211 | raw = force_raw; |
Johnny Chen | d17f801 | 2011-05-23 18:00:40 +0000 | [diff] [blame] | 212 | */ |
Johnny Chen | 80ab18e | 2011-05-12 22:25:53 +0000 | [diff] [blame] | 213 | /* .... when we fix the edis for arm/thumb. */ |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 214 | |
Johnny Chen | 611b7e9 | 2011-08-19 17:31:59 +0000 | [diff] [blame] | 215 | if (!raw) |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 216 | numTokens = EDNumTokens(m_inst); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 217 | |
| 218 | int currentOpIndex = -1; |
| 219 | |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 220 | bool printTokenized = false; |
| 221 | |
| 222 | if (numTokens != -1 && !raw) |
Sean Callanan | 8541f2f | 2010-07-23 02:19:15 +0000 | [diff] [blame] | 223 | { |
| 224 | addr_t base_addr = LLDB_INVALID_ADDRESS; |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 225 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 226 | if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty()) |
| 227 | base_addr = GetAddress().GetLoadAddress (exe_ctx->target); |
Sean Callanan | 8541f2f | 2010-07-23 02:19:15 +0000 | [diff] [blame] | 228 | if (base_addr == LLDB_INVALID_ADDRESS) |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 229 | base_addr = GetAddress().GetFileAddress (); |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 230 | |
Johnny Chen | 80ab18e | 2011-05-12 22:25:53 +0000 | [diff] [blame] | 231 | lldb::addr_t PC = base_addr + EDInstByteSize(m_inst); |
| 232 | |
| 233 | // When executing an ARM instruction, PC reads as the address of the |
| 234 | // current instruction plus 8. And for Thumb, it is plus 4. |
| 235 | if (m_arch_type == llvm::Triple::arm) |
| 236 | PC = base_addr + 8; |
| 237 | else if (m_arch_type == llvm::Triple::thumb) |
| 238 | PC = base_addr + 4; |
| 239 | |
| 240 | RegisterReaderArg rra(PC, m_disassembler); |
Johnny Chen | c5272bf | 2011-05-12 18:48:11 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 242 | printTokenized = true; |
| 243 | |
| 244 | // Handle the opcode column. |
| 245 | |
| 246 | StreamString opcode; |
| 247 | |
| 248 | int tokenIndex = 0; |
| 249 | |
| 250 | EDTokenRef token; |
| 251 | const char *tokenStr; |
| 252 | |
Johnny Chen | ff8fea6 | 2011-05-18 22:48:41 +0000 | [diff] [blame] | 253 | if (EDGetToken(&token, m_inst, tokenIndex)) // 0 on success |
| 254 | printTokenized = false; |
| 255 | else if (!EDTokenIsOpcode(token)) |
| 256 | printTokenized = false; |
| 257 | else if (EDGetTokenString(&tokenStr, token)) // 0 on success |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 258 | printTokenized = false; |
| 259 | |
Johnny Chen | ff8fea6 | 2011-05-18 22:48:41 +0000 | [diff] [blame] | 260 | if (printTokenized) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 261 | { |
Johnny Chen | ff8fea6 | 2011-05-18 22:48:41 +0000 | [diff] [blame] | 262 | // Put the token string into our opcode string |
| 263 | opcode.PutCString(tokenStr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 264 | |
Johnny Chen | ff8fea6 | 2011-05-18 22:48:41 +0000 | [diff] [blame] | 265 | // If anything follows, it probably starts with some whitespace. Skip it. |
| 266 | if (++tokenIndex < numTokens) |
| 267 | { |
| 268 | if (EDGetToken(&token, m_inst, tokenIndex)) // 0 on success |
| 269 | printTokenized = false; |
| 270 | else if (!EDTokenIsWhitespace(token)) |
| 271 | printTokenized = false; |
| 272 | } |
| 273 | |
| 274 | ++tokenIndex; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 277 | // Handle the operands and the comment. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 278 | StreamString operands; |
| 279 | StreamString comment; |
| 280 | |
| 281 | if (printTokenized) |
| 282 | { |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 283 | bool show_token = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 284 | |
| 285 | for (; tokenIndex < numTokens; ++tokenIndex) |
| 286 | { |
| 287 | if (EDGetToken(&token, m_inst, tokenIndex)) |
| 288 | return; |
| 289 | |
Johnny Chen | 6d61ebf | 2011-05-18 22:08:52 +0000 | [diff] [blame] | 290 | int operandIndex = EDOperandIndexForToken(token); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 291 | |
Johnny Chen | 6d61ebf | 2011-05-18 22:08:52 +0000 | [diff] [blame] | 292 | if (operandIndex >= 0) |
| 293 | { |
| 294 | if (operandIndex != currentOpIndex) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 295 | { |
Johnny Chen | 6d61ebf | 2011-05-18 22:08:52 +0000 | [diff] [blame] | 296 | show_token = true; |
| 297 | |
| 298 | currentOpIndex = operandIndex; |
| 299 | EDOperandRef operand; |
| 300 | |
| 301 | if (!EDGetOperand(&operand, m_inst, currentOpIndex)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 302 | { |
Johnny Chen | 6d61ebf | 2011-05-18 22:08:52 +0000 | [diff] [blame] | 303 | if (EDOperandIsMemory(operand)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 304 | { |
Johnny Chen | 6d61ebf | 2011-05-18 22:08:52 +0000 | [diff] [blame] | 305 | uint64_t operand_value; |
| 306 | |
| 307 | if (!EDEvaluateOperand(&operand_value, operand, IPRegisterReader, &rra)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 308 | { |
Johnny Chen | 6d61ebf | 2011-05-18 22:08:52 +0000 | [diff] [blame] | 309 | if (EDInstIsBranch(m_inst)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 310 | { |
Johnny Chen | 6d61ebf | 2011-05-18 22:08:52 +0000 | [diff] [blame] | 311 | operands.Printf("0x%llx ", operand_value); |
| 312 | show_token = false; |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | // Put the address value into the comment |
| 317 | comment.Printf("0x%llx ", operand_value); |
| 318 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 319 | |
Johnny Chen | d254eb9 | 2011-05-23 23:29:23 +0000 | [diff] [blame] | 320 | AddSymbolicInfo(exe_ctx, exe_scope, comment, operand_value, GetAddress()); |
Johnny Chen | 6d61ebf | 2011-05-18 22:08:52 +0000 | [diff] [blame] | 321 | } // EDEvaluateOperand |
| 322 | } // EDOperandIsMemory |
| 323 | } // EDGetOperand |
| 324 | } // operandIndex != currentOpIndex |
| 325 | } // operandIndex >= 0 |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 326 | |
| 327 | if (show_token) |
| 328 | { |
Enrico Granata | 4c3fb4b | 2011-07-19 18:03:25 +0000 | [diff] [blame] | 329 | if (EDGetTokenString(&tokenStr, token)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 330 | { |
| 331 | printTokenized = false; |
| 332 | break; |
| 333 | } |
| 334 | |
| 335 | operands.PutCString(tokenStr); |
| 336 | } |
| 337 | } // for (tokenIndex) |
| 338 | |
Johnny Chen | de5cc8c | 2011-05-20 17:27:37 +0000 | [diff] [blame] | 339 | // FIXME!!! |
| 340 | // Workaround for llvm::tB's operands not properly parsed by ARMAsmParser. |
| 341 | if (m_arch_type == llvm::Triple::thumb && opcode.GetString() == "b") { |
| 342 | const char *inst_str; |
Peter Collingbourne | d77c039 | 2011-05-20 22:42:59 +0000 | [diff] [blame] | 343 | const char *pos = NULL; |
Johnny Chen | c298a98 | 2011-05-23 19:41:31 +0000 | [diff] [blame] | 344 | operands.Clear(); comment.Clear(); |
Johnny Chen | de5cc8c | 2011-05-20 17:27:37 +0000 | [diff] [blame] | 345 | if (EDGetInstString(&inst_str, m_inst) == 0 && (pos = strstr(inst_str, "#")) != NULL) { |
| 346 | uint64_t operand_value = PC + atoi(++pos); |
Johnny Chen | d254eb9 | 2011-05-23 23:29:23 +0000 | [diff] [blame] | 347 | // Put the address value into the operands. |
Johnny Chen | de5cc8c | 2011-05-20 17:27:37 +0000 | [diff] [blame] | 348 | operands.Printf("0x%llx ", operand_value); |
Johnny Chen | d254eb9 | 2011-05-23 23:29:23 +0000 | [diff] [blame] | 349 | AddSymbolicInfo(exe_ctx, exe_scope, comment, operand_value, GetAddress()); |
Johnny Chen | de5cc8c | 2011-05-20 17:27:37 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
Johnny Chen | c298a98 | 2011-05-23 19:41:31 +0000 | [diff] [blame] | 352 | // Yet more workaround for "bl #..." and "blx #...". |
| 353 | if ((m_arch_type == llvm::Triple::arm || m_arch_type == llvm::Triple::thumb) && |
| 354 | (opcode.GetString() == "bl" || opcode.GetString() == "blx")) { |
| 355 | const char *inst_str; |
| 356 | const char *pos = NULL; |
| 357 | operands.Clear(); comment.Clear(); |
| 358 | if (EDGetInstString(&inst_str, m_inst) == 0 && (pos = strstr(inst_str, "#")) != NULL) { |
Johnny Chen | 6678676 | 2011-08-03 04:50:37 +0000 | [diff] [blame] | 359 | if (m_arch_type == llvm::Triple::thumb && opcode.GetString() == "blx") { |
| 360 | // A8.6.23 BLX (immediate) |
| 361 | // Target Address = Align(PC,4) + offset value |
| 362 | PC = AlignPC(PC); |
| 363 | } |
Johnny Chen | c298a98 | 2011-05-23 19:41:31 +0000 | [diff] [blame] | 364 | uint64_t operand_value = PC + atoi(++pos); |
Johnny Chen | d254eb9 | 2011-05-23 23:29:23 +0000 | [diff] [blame] | 365 | // Put the address value into the comment. |
Johnny Chen | c298a98 | 2011-05-23 19:41:31 +0000 | [diff] [blame] | 366 | comment.Printf("0x%llx ", operand_value); |
Johnny Chen | d254eb9 | 2011-05-23 23:29:23 +0000 | [diff] [blame] | 367 | // And the original token string into the operands. |
| 368 | llvm::StringRef Str(pos - 1); |
| 369 | RStrip(Str, '\n'); |
| 370 | operands.PutCString(Str.str().c_str()); |
| 371 | AddSymbolicInfo(exe_ctx, exe_scope, comment, operand_value, GetAddress()); |
Johnny Chen | c298a98 | 2011-05-23 19:41:31 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
Johnny Chen | de5cc8c | 2011-05-20 17:27:37 +0000 | [diff] [blame] | 374 | // END of workaround. |
| 375 | |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 376 | // If both operands and comment are empty, we will just print out |
| 377 | // the raw disassembly. |
| 378 | if (operands.GetString().empty() && comment.GetString().empty()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 379 | { |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 380 | const char *str; |
| 381 | |
| 382 | if (EDGetInstString(&str, m_inst)) |
| 383 | return; |
Johnny Chen | d17f801 | 2011-05-23 18:00:40 +0000 | [diff] [blame] | 384 | Align(s, str, opcodeColumnWidth, operandColumnWidth); |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 385 | } |
| 386 | else |
| 387 | { |
| 388 | PadString(s, opcode.GetString(), opcodeColumnWidth); |
| 389 | |
| 390 | if (comment.GetString().empty()) |
| 391 | s->PutCString(operands.GetString().c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 392 | else |
| 393 | { |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 394 | PadString(s, operands.GetString(), operandColumnWidth); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 395 | |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 396 | s->PutCString("; "); |
| 397 | s->PutCString(comment.GetString().c_str()); |
| 398 | } // else (comment.GetString().empty()) |
| 399 | } // else (operands.GetString().empty() && comment.GetString().empty()) |
| 400 | } // printTokenized |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 401 | } // numTokens != -1 |
| 402 | |
| 403 | if (!printTokenized) |
| 404 | { |
| 405 | const char *str; |
| 406 | |
Johnny Chen | 51ff248 | 2011-05-19 01:05:37 +0000 | [diff] [blame] | 407 | if (EDGetInstString(&str, m_inst)) // 0 on success |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 408 | return; |
Johnny Chen | 08251ef | 2011-05-21 00:44:42 +0000 | [diff] [blame] | 409 | if (raw) |
| 410 | s->Write(str, strlen(str) - 1); |
| 411 | else |
| 412 | { |
| 413 | // EDis fails to parse the tokens of this inst. Need to align this |
Johnny Chen | 84d42e8 | 2011-05-21 00:55:57 +0000 | [diff] [blame] | 414 | // raw disassembly's opcode with the rest of output. |
Johnny Chen | d17f801 | 2011-05-23 18:00:40 +0000 | [diff] [blame] | 415 | Align(s, str, opcodeColumnWidth, operandColumnWidth); |
Johnny Chen | 08251ef | 2011-05-21 00:44:42 +0000 | [diff] [blame] | 416 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
| 420 | bool |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 421 | InstructionLLVM::DoesBranch() const |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 422 | { |
| 423 | return EDInstIsBranch(m_inst); |
| 424 | } |
| 425 | |
| 426 | size_t |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 427 | InstructionLLVM::Decode (const Disassembler &disassembler, |
| 428 | const lldb_private::DataExtractor &data, |
| 429 | uint32_t data_offset) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 430 | { |
| 431 | if (EDCreateInsts(&m_inst, 1, m_disassembler, DataExtractorByteReader, data_offset, (void*)(&data))) |
Greg Clayton | 7bc3908 | 2011-03-24 23:53:38 +0000 | [diff] [blame] | 432 | { |
| 433 | const int byte_size = EDInstByteSize(m_inst); |
| 434 | uint32_t offset = data_offset; |
| 435 | // Make a copy of the opcode in m_opcode |
| 436 | switch (disassembler.GetArchitecture().GetMachine()) |
| 437 | { |
| 438 | case llvm::Triple::x86: |
| 439 | case llvm::Triple::x86_64: |
| 440 | m_opcode.SetOpcodeBytes (data.PeekData (data_offset, byte_size), byte_size); |
| 441 | break; |
| 442 | |
| 443 | case llvm::Triple::arm: |
Greg Clayton | 7bc3908 | 2011-03-24 23:53:38 +0000 | [diff] [blame] | 444 | case llvm::Triple::thumb: |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 445 | switch (byte_size) |
| 446 | { |
| 447 | case 2: |
| 448 | m_opcode.SetOpcode16 (data.GetU16 (&offset)); |
| 449 | break; |
| 450 | |
| 451 | case 4: |
Caroline Tice | 6b8d3b5 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 452 | { |
| 453 | if (GetAddressClass() == eAddressClassCodeAlternateISA) |
| 454 | { |
| 455 | // If it is a 32-bit THUMB instruction, we need to swap the upper & lower halves. |
| 456 | uint32_t orig_bytes = data.GetU32 (&offset); |
| 457 | uint16_t upper_bits = (orig_bytes >> 16) & ((1u << 16) - 1); |
| 458 | uint16_t lower_bits = orig_bytes & ((1u << 16) - 1); |
| 459 | uint32_t swapped = (lower_bits << 16) | upper_bits; |
| 460 | m_opcode.SetOpcode32 (swapped); |
| 461 | } |
| 462 | else |
| 463 | m_opcode.SetOpcode32 (data.GetU32 (&offset)); |
| 464 | } |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 465 | break; |
| 466 | |
| 467 | default: |
| 468 | assert (!"Invalid ARM opcode size"); |
| 469 | break; |
| 470 | } |
Greg Clayton | 7bc3908 | 2011-03-24 23:53:38 +0000 | [diff] [blame] | 471 | break; |
| 472 | |
| 473 | default: |
| 474 | assert (!"This shouldn't happen since we control the architecture we allow DisassemblerLLVM to be created for"); |
| 475 | break; |
| 476 | } |
| 477 | return byte_size; |
| 478 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 479 | else |
| 480 | return 0; |
| 481 | } |
| 482 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 483 | static inline EDAssemblySyntax_t |
Greg Clayton | cf01505 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 484 | SyntaxForArchSpec (const ArchSpec &arch) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 485 | { |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 486 | switch (arch.GetMachine ()) |
Greg Clayton | 5e4f4a2 | 2011-02-16 00:00:43 +0000 | [diff] [blame] | 487 | { |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 488 | case llvm::Triple::x86: |
| 489 | case llvm::Triple::x86_64: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 490 | return kEDAssemblySyntaxX86ATT; |
Sean Callanan | d151c8a | 2011-03-09 01:02:51 +0000 | [diff] [blame] | 491 | case llvm::Triple::arm: |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 492 | case llvm::Triple::thumb: |
Sean Callanan | d151c8a | 2011-03-09 01:02:51 +0000 | [diff] [blame] | 493 | return kEDAssemblySyntaxARMUAL; |
Greg Clayton | 5e4f4a2 | 2011-02-16 00:00:43 +0000 | [diff] [blame] | 494 | default: |
| 495 | break; |
| 496 | } |
Greg Clayton | cf01505 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 497 | return (EDAssemblySyntax_t)0; // default |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | Disassembler * |
| 501 | DisassemblerLLVM::CreateInstance(const ArchSpec &arch) |
| 502 | { |
Greg Clayton | 5e4f4a2 | 2011-02-16 00:00:43 +0000 | [diff] [blame] | 503 | std::auto_ptr<DisassemblerLLVM> disasm_ap (new DisassemblerLLVM(arch)); |
| 504 | |
Caroline Tice | 080bf61 | 2011-04-05 18:46:00 +0000 | [diff] [blame] | 505 | if (disasm_ap.get() && disasm_ap->IsValid()) |
Greg Clayton | 5e4f4a2 | 2011-02-16 00:00:43 +0000 | [diff] [blame] | 506 | return disasm_ap.release(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 507 | |
Greg Clayton | cf01505 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 508 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | DisassemblerLLVM::DisassemblerLLVM(const ArchSpec &arch) : |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 512 | Disassembler (arch), |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 513 | m_disassembler (NULL), |
| 514 | m_disassembler_thumb (NULL) // For ARM only |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 515 | { |
Greg Clayton | 5e4f4a2 | 2011-02-16 00:00:43 +0000 | [diff] [blame] | 516 | const std::string &arch_triple = arch.GetTriple().str(); |
| 517 | if (!arch_triple.empty()) |
Greg Clayton | cf01505 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 518 | { |
Greg Clayton | 5e4f4a2 | 2011-02-16 00:00:43 +0000 | [diff] [blame] | 519 | if (EDGetDisassembler(&m_disassembler, arch_triple.c_str(), SyntaxForArchSpec (arch))) |
| 520 | m_disassembler = NULL; |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 521 | llvm::Triple::ArchType llvm_arch = arch.GetTriple().getArch(); |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 522 | // Don't have the lldb::Triple::thumb architecture here. If someone specifies |
| 523 | // "thumb" as the architecture, we want a thumb only disassembler. But if any |
| 524 | // architecture starting with "arm" if specified, we want to auto detect the |
| 525 | // arm/thumb code automatically using the AddressClass from section offset |
| 526 | // addresses. |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 527 | if (llvm_arch == llvm::Triple::arm) |
| 528 | { |
| 529 | if (EDGetDisassembler(&m_disassembler_thumb, "thumb-apple-darwin", kEDAssemblySyntaxARMUAL)) |
| 530 | m_disassembler_thumb = NULL; |
| 531 | } |
Greg Clayton | cf01505 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 532 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | DisassemblerLLVM::~DisassemblerLLVM() |
| 536 | { |
| 537 | } |
| 538 | |
| 539 | size_t |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 540 | DisassemblerLLVM::DecodeInstructions |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 541 | ( |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 542 | const Address &base_addr, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 543 | const DataExtractor& data, |
| 544 | uint32_t data_offset, |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 545 | uint32_t num_instructions, |
| 546 | bool append |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 547 | ) |
| 548 | { |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 549 | if (m_disassembler == NULL) |
| 550 | return 0; |
| 551 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 552 | size_t total_inst_byte_size = 0; |
| 553 | |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 554 | if (!append) |
| 555 | m_instruction_list.Clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 556 | |
| 557 | while (data.ValidOffset(data_offset) && num_instructions) |
| 558 | { |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 559 | Address inst_addr (base_addr); |
| 560 | inst_addr.Slide(data_offset); |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 561 | |
| 562 | bool use_thumb = false; |
| 563 | // If we have a thumb disassembler, then we have an ARM architecture |
| 564 | // so we need to check what the instruction address class is to make |
| 565 | // sure we shouldn't be disassembling as thumb... |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 566 | AddressClass inst_address_class = eAddressClassInvalid; |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 567 | if (m_disassembler_thumb) |
| 568 | { |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 569 | inst_address_class = inst_addr.GetAddressClass (); |
| 570 | if (inst_address_class == eAddressClassCodeAlternateISA) |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 571 | use_thumb = true; |
| 572 | } |
Johnny Chen | 80ab18e | 2011-05-12 22:25:53 +0000 | [diff] [blame] | 573 | |
Greg Clayton | 7bc3908 | 2011-03-24 23:53:38 +0000 | [diff] [blame] | 574 | InstructionSP inst_sp (new InstructionLLVM (inst_addr, |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 575 | inst_address_class, |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 576 | use_thumb ? m_disassembler_thumb : m_disassembler, |
Johnny Chen | 1608c87 | 2011-05-18 18:22:16 +0000 | [diff] [blame] | 577 | use_thumb ? llvm::Triple::thumb : m_arch.GetMachine())); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 578 | |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 579 | size_t inst_byte_size = inst_sp->Decode (*this, data, data_offset); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 580 | |
| 581 | if (inst_byte_size == 0) |
| 582 | break; |
| 583 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 584 | m_instruction_list.Append (inst_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 585 | |
| 586 | total_inst_byte_size += inst_byte_size; |
| 587 | data_offset += inst_byte_size; |
| 588 | num_instructions--; |
| 589 | } |
| 590 | |
| 591 | return total_inst_byte_size; |
| 592 | } |
| 593 | |
| 594 | void |
| 595 | DisassemblerLLVM::Initialize() |
| 596 | { |
| 597 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 598 | GetPluginDescriptionStatic(), |
| 599 | CreateInstance); |
| 600 | } |
| 601 | |
| 602 | void |
| 603 | DisassemblerLLVM::Terminate() |
| 604 | { |
| 605 | PluginManager::UnregisterPlugin (CreateInstance); |
| 606 | } |
| 607 | |
| 608 | |
| 609 | const char * |
| 610 | DisassemblerLLVM::GetPluginNameStatic() |
| 611 | { |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 612 | return "llvm"; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | const char * |
| 616 | DisassemblerLLVM::GetPluginDescriptionStatic() |
| 617 | { |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 618 | return "Disassembler that uses LLVM opcode tables to disassemble i386, x86_64 and ARM."; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | //------------------------------------------------------------------ |
| 622 | // PluginInterface protocol |
| 623 | //------------------------------------------------------------------ |
| 624 | const char * |
| 625 | DisassemblerLLVM::GetPluginName() |
| 626 | { |
| 627 | return "DisassemblerLLVM"; |
| 628 | } |
| 629 | |
| 630 | const char * |
| 631 | DisassemblerLLVM::GetShortPluginName() |
| 632 | { |
| 633 | return GetPluginNameStatic(); |
| 634 | } |
| 635 | |
| 636 | uint32_t |
| 637 | DisassemblerLLVM::GetPluginVersion() |
| 638 | { |
| 639 | return 1; |
| 640 | } |
| 641 | |