blob: c2f84ecc98d1d2f3f1fc42fc6187efe745393d85 [file] [log] [blame]
Sean Callanan95e5c632012-02-17 00:53:45 +00001//===-- DisassemblerLLVMC.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 "DisassemblerLLVMC.h"
11
12#include "llvm-c/Disassembler.h"
Ashok Thirumurthi78059352013-05-24 15:55:54 +000013#include "llvm/ADT/OwningPtr.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000014#include "llvm/MC/MCAsmInfo.h"
15#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCDisassembler.h"
17#include "llvm/MC/MCInst.h"
18#include "llvm/MC/MCInstPrinter.h"
19#include "llvm/MC/MCInstrInfo.h"
20#include "llvm/MC/MCRegisterInfo.h"
Ashok Thirumurthi78059352013-05-24 15:55:54 +000021#include "llvm/MC/MCRelocationInfo.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000022#include "llvm/MC/MCSubtargetInfo.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/MemoryObject.h"
25#include "llvm/Support/TargetRegistry.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000026#include "llvm/Support/TargetSelect.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000027#include "llvm/ADT/SmallString.h"
28
Sean Callanan95e5c632012-02-17 00:53:45 +000029
30#include "lldb/Core/Address.h"
31#include "lldb/Core/DataExtractor.h"
Greg Clayton1f746072012-08-29 21:13:06 +000032#include "lldb/Core/Module.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000033#include "lldb/Core/Stream.h"
34#include "lldb/Symbol/SymbolContext.h"
35#include "lldb/Target/ExecutionContext.h"
36#include "lldb/Target/Process.h"
37#include "lldb/Target/RegisterContext.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000038#include "lldb/Target/SectionLoadList.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000039#include "lldb/Target/Target.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000040#include "lldb/Target/StackFrame.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000041
Virgile Bellob2f1fb22013-08-23 12:44:05 +000042#include "lldb/Core/RegularExpression.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000043
44using namespace lldb;
45using namespace lldb_private;
46
47class InstructionLLVMC : public lldb_private::Instruction
48{
49public:
50 InstructionLLVMC (DisassemblerLLVMC &disasm,
51 const lldb_private::Address &address,
Greg Claytonc8e0c242012-04-13 00:07:34 +000052 AddressClass addr_class) :
Greg Clayton3faf47c2013-03-28 23:42:53 +000053 Instruction (address, addr_class),
54 m_disasm_sp (disasm.shared_from_this()),
55 m_does_branch (eLazyBoolCalculate),
56 m_is_valid (false),
57 m_using_file_addr (false)
Sean Callanan95e5c632012-02-17 00:53:45 +000058 {
59 }
60
61 virtual
62 ~InstructionLLVMC ()
63 {
64 }
65
Sean Callanan95e5c632012-02-17 00:53:45 +000066 virtual bool
Jim Ingham32ce20c2013-03-13 01:55:16 +000067 DoesBranch ()
Sean Callanan95e5c632012-02-17 00:53:45 +000068 {
Jim Ingham32ce20c2013-03-13 01:55:16 +000069 if (m_does_branch == eLazyBoolCalculate)
70 {
Greg Clayton3faf47c2013-03-28 23:42:53 +000071 GetDisassemblerLLVMC().Lock(this, NULL);
Jim Ingham32ce20c2013-03-13 01:55:16 +000072 DataExtractor data;
73 if (m_opcode.GetData(data))
74 {
75 bool is_alternate_isa;
76 lldb::addr_t pc = m_address.GetFileAddress();
77
78 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr = GetDisasmToUse (is_alternate_isa);
Greg Clayton3faf47c2013-03-28 23:42:53 +000079 const uint8_t *opcode_data = data.GetDataStart();
Jim Ingham32ce20c2013-03-13 01:55:16 +000080 const size_t opcode_data_len = data.GetByteSize();
81 llvm::MCInst inst;
Greg Clayton3faf47c2013-03-28 23:42:53 +000082 const size_t inst_size = mc_disasm_ptr->GetMCInst (opcode_data,
83 opcode_data_len,
84 pc,
85 inst);
Jim Ingham32ce20c2013-03-13 01:55:16 +000086 // Be conservative, if we didn't understand the instruction, say it might branch...
87 if (inst_size == 0)
88 m_does_branch = eLazyBoolYes;
89 else
90 {
Greg Clayton3faf47c2013-03-28 23:42:53 +000091 const bool can_branch = mc_disasm_ptr->CanBranch(inst);
Jim Ingham32ce20c2013-03-13 01:55:16 +000092 if (can_branch)
93 m_does_branch = eLazyBoolYes;
94 else
95 m_does_branch = eLazyBoolNo;
96 }
97 }
Greg Clayton3faf47c2013-03-28 23:42:53 +000098 GetDisassemblerLLVMC().Unlock();
Jim Ingham32ce20c2013-03-13 01:55:16 +000099 }
Sean Callanan7725a462012-03-02 23:22:53 +0000100 return m_does_branch == eLazyBoolYes;
Sean Callanan95e5c632012-02-17 00:53:45 +0000101 }
102
Jim Ingham32ce20c2013-03-13 01:55:16 +0000103 DisassemblerLLVMC::LLVMCDisassembler *
104 GetDisasmToUse (bool &is_alternate_isa)
105 {
Jim Ingham32ce20c2013-03-13 01:55:16 +0000106 is_alternate_isa = false;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000107 DisassemblerLLVMC &llvm_disasm = GetDisassemblerLLVMC();
108 if (llvm_disasm.m_alternate_disasm_ap.get() != NULL)
Jim Ingham32ce20c2013-03-13 01:55:16 +0000109 {
110 const AddressClass address_class = GetAddressClass ();
111
112 if (address_class == eAddressClassCodeAlternateISA)
113 {
Jim Ingham32ce20c2013-03-13 01:55:16 +0000114 is_alternate_isa = true;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000115 return llvm_disasm.m_alternate_disasm_ap.get();
Jim Ingham32ce20c2013-03-13 01:55:16 +0000116 }
117 }
Greg Clayton3faf47c2013-03-28 23:42:53 +0000118 return llvm_disasm.m_disasm_ap.get();
Jim Ingham32ce20c2013-03-13 01:55:16 +0000119 }
120
Sean Callanan95e5c632012-02-17 00:53:45 +0000121 virtual size_t
122 Decode (const lldb_private::Disassembler &disassembler,
123 const lldb_private::DataExtractor &data,
Greg Claytonc7bece562013-01-25 18:06:21 +0000124 lldb::offset_t data_offset)
Sean Callanan95e5c632012-02-17 00:53:45 +0000125 {
Greg Claytonba812f42012-05-10 02:52:23 +0000126 // All we have to do is read the opcode which can be easy for some
Michael Sartaincc791bb2013-07-11 16:40:56 +0000127 // architectures
Greg Claytonba812f42012-05-10 02:52:23 +0000128 bool got_op = false;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000129 DisassemblerLLVMC &llvm_disasm = GetDisassemblerLLVMC();
130 const ArchSpec &arch = llvm_disasm.GetArchitecture();
Ed Maste90359962013-12-09 19:45:33 +0000131 const lldb::ByteOrder byte_order = data.GetByteOrder();
Sean Callanan95e5c632012-02-17 00:53:45 +0000132
Greg Claytonba812f42012-05-10 02:52:23 +0000133 const uint32_t min_op_byte_size = arch.GetMinimumOpcodeByteSize();
134 const uint32_t max_op_byte_size = arch.GetMaximumOpcodeByteSize();
135 if (min_op_byte_size == max_op_byte_size)
136 {
137 // Fixed size instructions, just read that amount of data.
138 if (!data.ValidOffsetForDataOfSize(data_offset, min_op_byte_size))
139 return false;
Ed Maste90359962013-12-09 19:45:33 +0000140
Greg Claytonba812f42012-05-10 02:52:23 +0000141 switch (min_op_byte_size)
142 {
143 case 1:
Ed Maste90359962013-12-09 19:45:33 +0000144 m_opcode.SetOpcode8 (data.GetU8 (&data_offset), byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000145 got_op = true;
146 break;
147
148 case 2:
Ed Maste90359962013-12-09 19:45:33 +0000149 m_opcode.SetOpcode16 (data.GetU16 (&data_offset), byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000150 got_op = true;
151 break;
152
153 case 4:
Ed Maste90359962013-12-09 19:45:33 +0000154 m_opcode.SetOpcode32 (data.GetU32 (&data_offset), byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000155 got_op = true;
156 break;
157
158 case 8:
Ed Maste90359962013-12-09 19:45:33 +0000159 m_opcode.SetOpcode64 (data.GetU64 (&data_offset), byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000160 got_op = true;
161 break;
162
163 default:
164 m_opcode.SetOpcodeBytes(data.PeekData(data_offset, min_op_byte_size), min_op_byte_size);
165 got_op = true;
166 break;
167 }
168 }
169 if (!got_op)
170 {
Jim Ingham32ce20c2013-03-13 01:55:16 +0000171 bool is_alternate_isa = false;
172 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr = GetDisasmToUse (is_alternate_isa);
Greg Claytonba812f42012-05-10 02:52:23 +0000173
Greg Claytonba812f42012-05-10 02:52:23 +0000174 const llvm::Triple::ArchType machine = arch.GetMachine();
175 if (machine == llvm::Triple::arm || machine == llvm::Triple::thumb)
176 {
Jim Ingham32ce20c2013-03-13 01:55:16 +0000177 if (machine == llvm::Triple::thumb || is_alternate_isa)
Greg Claytonba812f42012-05-10 02:52:23 +0000178 {
Greg Clayton79101b52012-08-07 01:29:29 +0000179 uint32_t thumb_opcode = data.GetU16(&data_offset);
Greg Claytonba812f42012-05-10 02:52:23 +0000180 if ((thumb_opcode & 0xe000) != 0xe000 || ((thumb_opcode & 0x1800u) == 0))
181 {
Ed Maste90359962013-12-09 19:45:33 +0000182 m_opcode.SetOpcode16 (thumb_opcode, byte_order);
Sean Callanan5c97c2f2012-08-06 23:42:52 +0000183 m_is_valid = true;
Greg Claytonba812f42012-05-10 02:52:23 +0000184 }
185 else
186 {
Greg Clayton79101b52012-08-07 01:29:29 +0000187 thumb_opcode <<= 16;
188 thumb_opcode |= data.GetU16(&data_offset);
Ed Maste90359962013-12-09 19:45:33 +0000189 m_opcode.SetOpcode16_2 (thumb_opcode, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000190 m_is_valid = true;
191 }
192 }
193 else
194 {
Ed Maste90359962013-12-09 19:45:33 +0000195 m_opcode.SetOpcode32 (data.GetU32(&data_offset), byte_order);
Sean Callanan5c97c2f2012-08-06 23:42:52 +0000196 m_is_valid = true;
Greg Claytonba812f42012-05-10 02:52:23 +0000197 }
198 }
199 else
200 {
201 // The opcode isn't evenly sized, so we need to actually use the llvm
202 // disassembler to parse it and get the size.
Greg Claytonba812f42012-05-10 02:52:23 +0000203 uint8_t *opcode_data = const_cast<uint8_t *>(data.PeekData (data_offset, 1));
Greg Clayton3faf47c2013-03-28 23:42:53 +0000204 const size_t opcode_data_len = data.BytesLeft(data_offset);
Greg Claytonba812f42012-05-10 02:52:23 +0000205 const addr_t pc = m_address.GetFileAddress();
Jim Ingham0f063ba2013-03-02 00:26:47 +0000206 llvm::MCInst inst;
207
Greg Clayton3faf47c2013-03-28 23:42:53 +0000208 llvm_disasm.Lock(this, NULL);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000209 const size_t inst_size = mc_disasm_ptr->GetMCInst(opcode_data,
Greg Clayton3faf47c2013-03-28 23:42:53 +0000210 opcode_data_len,
211 pc,
212 inst);
213 llvm_disasm.Unlock();
Greg Claytonba812f42012-05-10 02:52:23 +0000214 if (inst_size == 0)
215 m_opcode.Clear();
216 else
217 {
218 m_opcode.SetOpcodeBytes(opcode_data, inst_size);
219 m_is_valid = true;
220 }
221 }
222 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000223 return m_opcode.GetByteSize();
224 }
225
226 void
Greg Claytonba812f42012-05-10 02:52:23 +0000227 AppendComment (std::string &description)
Sean Callanan95e5c632012-02-17 00:53:45 +0000228 {
Greg Claytonba812f42012-05-10 02:52:23 +0000229 if (m_comment.empty())
230 m_comment.swap (description);
Sean Callanan95e5c632012-02-17 00:53:45 +0000231 else
Greg Claytonba812f42012-05-10 02:52:23 +0000232 {
233 m_comment.append(", ");
234 m_comment.append(description);
235 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000236 }
237
238 virtual void
Greg Claytonba812f42012-05-10 02:52:23 +0000239 CalculateMnemonicOperandsAndComment (const lldb_private::ExecutionContext *exe_ctx)
Sean Callanan95e5c632012-02-17 00:53:45 +0000240 {
Greg Claytonba812f42012-05-10 02:52:23 +0000241 DataExtractor data;
242 const AddressClass address_class = GetAddressClass ();
243
Sean Callanancd4ae1a2012-08-07 01:44:58 +0000244 if (m_opcode.GetData(data))
Greg Claytonba812f42012-05-10 02:52:23 +0000245 {
246 char out_string[512];
247
Greg Clayton3faf47c2013-03-28 23:42:53 +0000248 DisassemblerLLVMC &llvm_disasm = GetDisassemblerLLVMC();
249
Jim Ingham0f063ba2013-03-02 00:26:47 +0000250 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr;
Greg Claytonba812f42012-05-10 02:52:23 +0000251
252 if (address_class == eAddressClassCodeAlternateISA)
Greg Clayton3faf47c2013-03-28 23:42:53 +0000253 mc_disasm_ptr = llvm_disasm.m_alternate_disasm_ap.get();
Greg Claytonba812f42012-05-10 02:52:23 +0000254 else
Greg Clayton3faf47c2013-03-28 23:42:53 +0000255 mc_disasm_ptr = llvm_disasm.m_disasm_ap.get();
Greg Claytonba812f42012-05-10 02:52:23 +0000256
Greg Clayton3faf47c2013-03-28 23:42:53 +0000257 lldb::addr_t pc = m_address.GetFileAddress();
258 m_using_file_addr = true;
Greg Claytonba812f42012-05-10 02:52:23 +0000259
Greg Clayton3faf47c2013-03-28 23:42:53 +0000260 const bool data_from_file = GetDisassemblerLLVMC().m_data_from_file;
Daniel Malead79ae052013-08-07 21:54:09 +0000261 bool use_hex_immediates = true;
262 Disassembler::HexImmediateStyle hex_style = Disassembler::eHexStyleC;
263
264 if (exe_ctx)
Greg Claytonba812f42012-05-10 02:52:23 +0000265 {
Daniel Malead79ae052013-08-07 21:54:09 +0000266 Target *target = exe_ctx->GetTargetPtr();
267 if (target)
Greg Clayton3faf47c2013-03-28 23:42:53 +0000268 {
Daniel Malead79ae052013-08-07 21:54:09 +0000269 use_hex_immediates = target->GetUseHexImmediates();
270 hex_style = target->GetHexImmediateStyle();
271
272 if (!data_from_file)
Greg Clayton3faf47c2013-03-28 23:42:53 +0000273 {
274 const lldb::addr_t load_addr = m_address.GetLoadAddress(target);
275 if (load_addr != LLDB_INVALID_ADDRESS)
276 {
277 pc = load_addr;
278 m_using_file_addr = false;
279 }
280 }
281 }
Greg Claytonba812f42012-05-10 02:52:23 +0000282 }
283
Greg Clayton3faf47c2013-03-28 23:42:53 +0000284 llvm_disasm.Lock(this, exe_ctx);
Greg Claytonba812f42012-05-10 02:52:23 +0000285
Greg Clayton3faf47c2013-03-28 23:42:53 +0000286 const uint8_t *opcode_data = data.GetDataStart();
Greg Claytonba812f42012-05-10 02:52:23 +0000287 const size_t opcode_data_len = data.GetByteSize();
Jim Ingham0f063ba2013-03-02 00:26:47 +0000288 llvm::MCInst inst;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000289 size_t inst_size = mc_disasm_ptr->GetMCInst (opcode_data,
290 opcode_data_len,
291 pc,
292 inst);
Daniel Malead79ae052013-08-07 21:54:09 +0000293
Jim Ingham0f063ba2013-03-02 00:26:47 +0000294 if (inst_size > 0)
Daniel Malead79ae052013-08-07 21:54:09 +0000295 {
296 mc_disasm_ptr->SetStyle(use_hex_immediates, hex_style);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000297 mc_disasm_ptr->PrintMCInst(inst, out_string, sizeof(out_string));
Daniel Malead79ae052013-08-07 21:54:09 +0000298 }
299
Greg Clayton3faf47c2013-03-28 23:42:53 +0000300 llvm_disasm.Unlock();
Greg Claytonba812f42012-05-10 02:52:23 +0000301
302 if (inst_size == 0)
303 {
304 m_comment.assign ("unknown opcode");
305 inst_size = m_opcode.GetByteSize();
306 StreamString mnemonic_strm;
Greg Claytonc7bece562013-01-25 18:06:21 +0000307 lldb::offset_t offset = 0;
Ed Maste90359962013-12-09 19:45:33 +0000308 lldb::ByteOrder byte_order = data.GetByteOrder();
Greg Claytonba812f42012-05-10 02:52:23 +0000309 switch (inst_size)
310 {
311 case 1:
312 {
313 const uint8_t uval8 = data.GetU8 (&offset);
Ed Maste90359962013-12-09 19:45:33 +0000314 m_opcode.SetOpcode8 (uval8, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000315 m_opcode_name.assign (".byte");
316 mnemonic_strm.Printf("0x%2.2x", uval8);
317 }
318 break;
319 case 2:
320 {
321 const uint16_t uval16 = data.GetU16(&offset);
Ed Maste90359962013-12-09 19:45:33 +0000322 m_opcode.SetOpcode16(uval16, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000323 m_opcode_name.assign (".short");
324 mnemonic_strm.Printf("0x%4.4x", uval16);
325 }
326 break;
327 case 4:
328 {
329 const uint32_t uval32 = data.GetU32(&offset);
Ed Maste90359962013-12-09 19:45:33 +0000330 m_opcode.SetOpcode32(uval32, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000331 m_opcode_name.assign (".long");
332 mnemonic_strm.Printf("0x%8.8x", uval32);
333 }
334 break;
335 case 8:
336 {
337 const uint64_t uval64 = data.GetU64(&offset);
Ed Maste90359962013-12-09 19:45:33 +0000338 m_opcode.SetOpcode64(uval64, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000339 m_opcode_name.assign (".quad");
Daniel Malead01b2952012-11-29 21:49:15 +0000340 mnemonic_strm.Printf("0x%16.16" PRIx64, uval64);
Greg Claytonba812f42012-05-10 02:52:23 +0000341 }
342 break;
343 default:
344 if (inst_size == 0)
345 return;
346 else
347 {
348 const uint8_t *bytes = data.PeekData(offset, inst_size);
349 if (bytes == NULL)
350 return;
351 m_opcode_name.assign (".byte");
352 m_opcode.SetOpcodeBytes(bytes, inst_size);
353 mnemonic_strm.Printf("0x%2.2x", bytes[0]);
354 for (uint32_t i=1; i<inst_size; ++i)
355 mnemonic_strm.Printf(" 0x%2.2x", bytes[i]);
356 }
357 break;
358 }
Jim Ingham0f063ba2013-03-02 00:26:47 +0000359 m_mnemonics.swap(mnemonic_strm.GetString());
Greg Claytonba812f42012-05-10 02:52:23 +0000360 return;
361 }
362 else
363 {
364 if (m_does_branch == eLazyBoolCalculate)
365 {
Greg Clayton3faf47c2013-03-28 23:42:53 +0000366 const bool can_branch = mc_disasm_ptr->CanBranch(inst);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000367 if (can_branch)
Greg Claytonba812f42012-05-10 02:52:23 +0000368 m_does_branch = eLazyBoolYes;
369 else
370 m_does_branch = eLazyBoolNo;
Jim Ingham0f063ba2013-03-02 00:26:47 +0000371
Greg Claytonba812f42012-05-10 02:52:23 +0000372 }
373 }
374
Greg Claytondf96dd72013-08-27 19:53:47 +0000375 static RegularExpression s_regex("[ \t]*([^ ^\t]+)[ \t]*([^ ^\t].*)?", REG_EXTENDED);
Greg Claytonba812f42012-05-10 02:52:23 +0000376
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000377 RegularExpression::Match matches(3);
Greg Claytonba812f42012-05-10 02:52:23 +0000378
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000379 if (s_regex.Execute(out_string, &matches))
Greg Claytonba812f42012-05-10 02:52:23 +0000380 {
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000381 matches.GetMatchAtIndex(out_string, 1, m_opcode_name);
382 matches.GetMatchAtIndex(out_string, 2, m_mnemonics);
Greg Claytonba812f42012-05-10 02:52:23 +0000383 }
384 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000385 }
386
387 bool
Greg Clayton3faf47c2013-03-28 23:42:53 +0000388 IsValid () const
Sean Callanan95e5c632012-02-17 00:53:45 +0000389 {
390 return m_is_valid;
391 }
392
Greg Clayton3faf47c2013-03-28 23:42:53 +0000393 bool
394 UsingFileAddress() const
395 {
396 return m_using_file_addr;
397 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000398 size_t
Greg Clayton3faf47c2013-03-28 23:42:53 +0000399 GetByteSize () const
Sean Callanan95e5c632012-02-17 00:53:45 +0000400 {
401 return m_opcode.GetByteSize();
402 }
Greg Clayton3faf47c2013-03-28 23:42:53 +0000403
404 DisassemblerLLVMC &
405 GetDisassemblerLLVMC ()
406 {
407 return *(DisassemblerLLVMC *)m_disasm_sp.get();
408 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000409protected:
Sean Callanan95e5c632012-02-17 00:53:45 +0000410
Sean Callanan7e6d4e52012-08-01 18:50:59 +0000411 DisassemblerSP m_disasm_sp; // for ownership
Sean Callanan7725a462012-03-02 23:22:53 +0000412 LazyBool m_does_branch;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000413 bool m_is_valid;
414 bool m_using_file_addr;
Sean Callanan95e5c632012-02-17 00:53:45 +0000415};
416
Greg Claytondf96dd72013-08-27 19:53:47 +0000417
Sean Callanan95e5c632012-02-17 00:53:45 +0000418
Jim Ingham0f063ba2013-03-02 00:26:47 +0000419DisassemblerLLVMC::LLVMCDisassembler::LLVMCDisassembler (const char *triple, unsigned flavor, DisassemblerLLVMC &owner):
420 m_is_valid(true)
421{
422 std::string Error;
423 const llvm::Target *curr_target = llvm::TargetRegistry::lookupTarget(triple, Error);
424 if (!curr_target)
425 {
426 m_is_valid = false;
427 return;
428 }
429
430 m_instr_info_ap.reset(curr_target->createMCInstrInfo());
431 m_reg_info_ap.reset (curr_target->createMCRegInfo(triple));
432
433 std::string features_str;
434
435 m_subtarget_info_ap.reset(curr_target->createMCSubtargetInfo(triple, "",
436 features_str));
437
Sylvestre Ledru7a89d6f2013-05-13 13:41:13 +0000438 m_asm_info_ap.reset(curr_target->createMCAsmInfo(*curr_target->createMCRegInfo(triple), triple));
439
Jim Ingham0f063ba2013-03-02 00:26:47 +0000440 if (m_instr_info_ap.get() == NULL || m_reg_info_ap.get() == NULL || m_subtarget_info_ap.get() == NULL || m_asm_info_ap.get() == NULL)
441 {
Matt Kopec787d1622013-03-12 17:45:38 +0000442 m_is_valid = false;
Jim Ingham0f063ba2013-03-02 00:26:47 +0000443 return;
444 }
445
Bill Wendlingd63d0a82013-06-18 07:50:43 +0000446 m_context_ap.reset(new llvm::MCContext(m_asm_info_ap.get(), m_reg_info_ap.get(), 0));
Jim Ingham0f063ba2013-03-02 00:26:47 +0000447
448 m_disasm_ap.reset(curr_target->createMCDisassembler(*m_subtarget_info_ap.get()));
Ashok Thirumurthi78059352013-05-24 15:55:54 +0000449 if (m_disasm_ap.get() && m_context_ap.get())
Jim Ingham0f063ba2013-03-02 00:26:47 +0000450 {
Ashok Thirumurthi78059352013-05-24 15:55:54 +0000451 llvm::OwningPtr<llvm::MCRelocationInfo> RelInfo(curr_target->createMCRelocationInfo(triple, *m_context_ap.get()));
452 if (!RelInfo)
453 {
454 m_is_valid = false;
455 return;
456 }
Jim Ingham0f063ba2013-03-02 00:26:47 +0000457 m_disasm_ap->setupForSymbolicDisassembly(NULL,
Ashok Thirumurthi78059352013-05-24 15:55:54 +0000458 DisassemblerLLVMC::SymbolLookupCallback,
459 (void *) &owner,
460 m_context_ap.get(),
461 RelInfo);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000462
463 unsigned asm_printer_variant;
464 if (flavor == ~0U)
465 asm_printer_variant = m_asm_info_ap->getAssemblerDialect();
466 else
467 {
468 asm_printer_variant = flavor;
469 }
470
471 m_instr_printer_ap.reset(curr_target->createMCInstPrinter(asm_printer_variant,
472 *m_asm_info_ap.get(),
473 *m_instr_info_ap.get(),
474 *m_reg_info_ap.get(),
475 *m_subtarget_info_ap.get()));
476 if (m_instr_printer_ap.get() == NULL)
477 {
478 m_disasm_ap.reset();
479 m_is_valid = false;
480 }
481 }
482 else
483 m_is_valid = false;
484}
485
Greg Claytone01e07b2013-04-18 18:10:51 +0000486DisassemblerLLVMC::LLVMCDisassembler::~LLVMCDisassembler()
487{
488}
489
Jim Ingham0f063ba2013-03-02 00:26:47 +0000490namespace {
491 // This is the memory object we use in GetInstruction.
492 class LLDBDisasmMemoryObject : public llvm::MemoryObject {
Greg Clayton3faf47c2013-03-28 23:42:53 +0000493 const uint8_t *m_bytes;
Jim Ingham0f063ba2013-03-02 00:26:47 +0000494 uint64_t m_size;
495 uint64_t m_base_PC;
496 public:
Greg Clayton3faf47c2013-03-28 23:42:53 +0000497 LLDBDisasmMemoryObject(const uint8_t *bytes, uint64_t size, uint64_t basePC) :
Jim Ingham0f063ba2013-03-02 00:26:47 +0000498 m_bytes(bytes), m_size(size), m_base_PC(basePC) {}
499
500 uint64_t getBase() const { return m_base_PC; }
501 uint64_t getExtent() const { return m_size; }
502
503 int readByte(uint64_t addr, uint8_t *byte) const {
504 if (addr - m_base_PC >= m_size)
505 return -1;
506 *byte = m_bytes[addr - m_base_PC];
507 return 0;
508 }
509 };
510} // End Anonymous Namespace
511
512uint64_t
Greg Clayton3faf47c2013-03-28 23:42:53 +0000513DisassemblerLLVMC::LLVMCDisassembler::GetMCInst (const uint8_t *opcode_data,
514 size_t opcode_data_len,
515 lldb::addr_t pc,
516 llvm::MCInst &mc_inst)
Jim Ingham0f063ba2013-03-02 00:26:47 +0000517{
518 LLDBDisasmMemoryObject memory_object (opcode_data, opcode_data_len, pc);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000519 llvm::MCDisassembler::DecodeStatus status;
520
521 uint64_t new_inst_size;
522 status = m_disasm_ap->getInstruction(mc_inst,
523 new_inst_size,
524 memory_object,
525 pc,
526 llvm::nulls(),
527 llvm::nulls());
528 if (status == llvm::MCDisassembler::Success)
529 return new_inst_size;
530 else
531 return 0;
532}
533
534uint64_t
Greg Clayton3faf47c2013-03-28 23:42:53 +0000535DisassemblerLLVMC::LLVMCDisassembler::PrintMCInst (llvm::MCInst &mc_inst,
536 char *dst,
537 size_t dst_len)
Jim Ingham0f063ba2013-03-02 00:26:47 +0000538{
539 llvm::StringRef unused_annotations;
540 llvm::SmallString<64> inst_string;
541 llvm::raw_svector_ostream inst_stream(inst_string);
542 m_instr_printer_ap->printInst (&mc_inst, inst_stream, unused_annotations);
543 inst_stream.flush();
Greg Clayton3faf47c2013-03-28 23:42:53 +0000544 const size_t output_size = std::min(dst_len - 1, inst_string.size());
545 std::memcpy(dst, inst_string.data(), output_size);
546 dst[output_size] = '\0';
Jim Ingham0f063ba2013-03-02 00:26:47 +0000547
548 return output_size;
549}
550
Daniel Malead79ae052013-08-07 21:54:09 +0000551void
552DisassemblerLLVMC::LLVMCDisassembler::SetStyle (bool use_hex_immed, HexImmediateStyle hex_style)
553{
554 m_instr_printer_ap->setPrintImmHex(use_hex_immed);
555 switch(hex_style)
556 {
557 case eHexStyleC: m_instr_printer_ap->setPrintImmHex(llvm::HexStyle::C); break;
558 case eHexStyleAsm: m_instr_printer_ap->setPrintImmHex(llvm::HexStyle::Asm); break;
559 }
560}
561
Jim Ingham0f063ba2013-03-02 00:26:47 +0000562bool
563DisassemblerLLVMC::LLVMCDisassembler::CanBranch (llvm::MCInst &mc_inst)
564{
565 return m_instr_info_ap->get(mc_inst.getOpcode()).mayAffectControlFlow(mc_inst, *m_reg_info_ap.get());
566}
567
568bool
569DisassemblerLLVMC::FlavorValidForArchSpec (const lldb_private::ArchSpec &arch, const char *flavor)
570{
571 llvm::Triple triple = arch.GetTriple();
572 if (flavor == NULL || strcmp (flavor, "default") == 0)
573 return true;
574
575 if (triple.getArch() == llvm::Triple::x86 || triple.getArch() == llvm::Triple::x86_64)
576 {
577 if (strcmp (flavor, "intel") == 0 || strcmp (flavor, "att") == 0)
578 return true;
579 else
580 return false;
581 }
582 else
583 return false;
584}
585
586
Sean Callanan95e5c632012-02-17 00:53:45 +0000587Disassembler *
Jim Ingham0f063ba2013-03-02 00:26:47 +0000588DisassemblerLLVMC::CreateInstance (const ArchSpec &arch, const char *flavor)
Sean Callanan95e5c632012-02-17 00:53:45 +0000589{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000590 if (arch.GetTriple().getArch() != llvm::Triple::UnknownArch)
591 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000592 std::unique_ptr<DisassemblerLLVMC> disasm_ap (new DisassemblerLLVMC(arch, flavor));
Sean Callanan95e5c632012-02-17 00:53:45 +0000593
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000594 if (disasm_ap.get() && disasm_ap->IsValid())
595 return disasm_ap.release();
596 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000597 return NULL;
598}
599
Jim Ingham0f063ba2013-03-02 00:26:47 +0000600DisassemblerLLVMC::DisassemblerLLVMC (const ArchSpec &arch, const char *flavor_string) :
601 Disassembler(arch, flavor_string),
Greg Claytonba812f42012-05-10 02:52:23 +0000602 m_exe_ctx (NULL),
Greg Clayton3faf47c2013-03-28 23:42:53 +0000603 m_inst (NULL),
604 m_data_from_file (false)
Sean Callanan95e5c632012-02-17 00:53:45 +0000605{
Jim Ingham0f063ba2013-03-02 00:26:47 +0000606 if (!FlavorValidForArchSpec (arch, m_flavor.c_str()))
607 {
608 m_flavor.assign("default");
609 }
610
611 const char *triple = arch.GetTriple().getTriple().c_str();
612 unsigned flavor = ~0U;
613
614 // So far the only supported flavor is "intel" on x86. The base class will set this
615 // correctly coming in.
616 if (arch.GetTriple().getArch() == llvm::Triple::x86
617 || arch.GetTriple().getArch() == llvm::Triple::x86_64)
618 {
619 if (m_flavor == "intel")
620 {
621 flavor = 1;
622 }
623 else if (m_flavor == "att")
624 {
625 flavor = 0;
626 }
627 }
628
Jason Molenda01aa5342013-07-22 22:11:53 +0000629 ArchSpec thumb_arch(arch);
Sean Callanan95e5c632012-02-17 00:53:45 +0000630 if (arch.GetTriple().getArch() == llvm::Triple::arm)
631 {
Greg Claytona80ea122013-05-03 01:05:04 +0000632 std::string thumb_arch_name (thumb_arch.GetTriple().getArchName().str());
633 // Replace "arm" with "thumb" so we get all thumb variants correct
634 if (thumb_arch_name.size() > 3)
635 {
636 thumb_arch_name.erase(0,3);
637 thumb_arch_name.insert(0, "thumb");
638 }
639 else
640 {
641 thumb_arch_name = "thumbv7";
642 }
643 thumb_arch.GetTriple().setArchName(llvm::StringRef(thumb_arch_name.c_str()));
Jason Molenda01aa5342013-07-22 22:11:53 +0000644 }
645
646 // Cortex-M3 devices (e.g. armv7m) can only execute thumb (T2) instructions,
Jason Molendaa3a04522013-09-27 23:21:54 +0000647 // so hardcode the primary disassembler to thumb mode. Same for Cortex-M4 (armv7em).
648 //
649 // Handle the Cortex-M0 (armv6m) the same; the ISA is a subset of the T and T32
650 // instructions defined in ARMv7-A.
651
Jason Molenda01aa5342013-07-22 22:11:53 +0000652 if (arch.GetTriple().getArch() == llvm::Triple::arm
Jason Molendaa3a04522013-09-27 23:21:54 +0000653 && (arch.GetCore() == ArchSpec::Core::eCore_arm_armv7m
654 || arch.GetCore() == ArchSpec::Core::eCore_arm_armv7em
655 || arch.GetCore() == ArchSpec::Core::eCore_arm_armv6m))
Jason Molenda01aa5342013-07-22 22:11:53 +0000656 {
657 triple = thumb_arch.GetTriple().getTriple().c_str();
658 }
659
660 m_disasm_ap.reset (new LLVMCDisassembler(triple, flavor, *this));
661 if (!m_disasm_ap->IsValid())
662 {
663 // We use m_disasm_ap.get() to tell whether we are valid or not, so if this isn't good for some reason,
664 // we reset it, and then we won't be valid and FindPlugin will fail and we won't get used.
665 m_disasm_ap.reset();
666 }
667
668 // For arm CPUs that can execute arm or thumb instructions, also create a thumb instruction disassembler.
669 if (arch.GetTriple().getArch() == llvm::Triple::arm)
670 {
Greg Claytonba812f42012-05-10 02:52:23 +0000671 std::string thumb_triple(thumb_arch.GetTriple().getTriple());
Jim Ingham0f063ba2013-03-02 00:26:47 +0000672 m_alternate_disasm_ap.reset(new LLVMCDisassembler(thumb_triple.c_str(), flavor, *this));
673 if (!m_alternate_disasm_ap->IsValid())
674 {
675 m_disasm_ap.reset();
676 m_alternate_disasm_ap.reset();
677 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000678 }
679}
680
681DisassemblerLLVMC::~DisassemblerLLVMC()
682{
683}
684
685size_t
686DisassemblerLLVMC::DecodeInstructions (const Address &base_addr,
687 const DataExtractor& data,
Greg Claytonc7bece562013-01-25 18:06:21 +0000688 lldb::offset_t data_offset,
689 size_t num_instructions,
Greg Clayton3faf47c2013-03-28 23:42:53 +0000690 bool append,
691 bool data_from_file)
Sean Callanan95e5c632012-02-17 00:53:45 +0000692{
693 if (!append)
694 m_instruction_list.Clear();
695
696 if (!IsValid())
697 return 0;
698
Greg Clayton3faf47c2013-03-28 23:42:53 +0000699 m_data_from_file = data_from_file;
Sean Callanan95e5c632012-02-17 00:53:45 +0000700 uint32_t data_cursor = data_offset;
Greg Claytonba812f42012-05-10 02:52:23 +0000701 const size_t data_byte_size = data.GetByteSize();
Sean Callanan95e5c632012-02-17 00:53:45 +0000702 uint32_t instructions_parsed = 0;
Greg Claytonba812f42012-05-10 02:52:23 +0000703 Address inst_addr(base_addr);
Sean Callanan95e5c632012-02-17 00:53:45 +0000704
Greg Claytonba812f42012-05-10 02:52:23 +0000705 while (data_cursor < data_byte_size && instructions_parsed < num_instructions)
Sean Callanan95e5c632012-02-17 00:53:45 +0000706 {
Sean Callanan95e5c632012-02-17 00:53:45 +0000707
Greg Claytonba812f42012-05-10 02:52:23 +0000708 AddressClass address_class = eAddressClassCode;
Sean Callanan95e5c632012-02-17 00:53:45 +0000709
Jim Ingham0f063ba2013-03-02 00:26:47 +0000710 if (m_alternate_disasm_ap.get() != NULL)
Greg Claytonba812f42012-05-10 02:52:23 +0000711 address_class = inst_addr.GetAddressClass ();
Sean Callanan95e5c632012-02-17 00:53:45 +0000712
713 InstructionSP inst_sp(new InstructionLLVMC(*this,
Greg Claytonba812f42012-05-10 02:52:23 +0000714 inst_addr,
Sean Callanan95e5c632012-02-17 00:53:45 +0000715 address_class));
716
717 if (!inst_sp)
Greg Claytonba812f42012-05-10 02:52:23 +0000718 break;
719
Sean Callanan95e5c632012-02-17 00:53:45 +0000720 uint32_t inst_size = inst_sp->Decode(*this, data, data_cursor);
721
Greg Claytonba812f42012-05-10 02:52:23 +0000722 if (inst_size == 0)
723 break;
724
Sean Callanan95e5c632012-02-17 00:53:45 +0000725 m_instruction_list.Append(inst_sp);
Sean Callanan95e5c632012-02-17 00:53:45 +0000726 data_cursor += inst_size;
Greg Claytonba812f42012-05-10 02:52:23 +0000727 inst_addr.Slide(inst_size);
Sean Callanan95e5c632012-02-17 00:53:45 +0000728 instructions_parsed++;
729 }
730
731 return data_cursor - data_offset;
732}
733
734void
735DisassemblerLLVMC::Initialize()
736{
737 PluginManager::RegisterPlugin (GetPluginNameStatic(),
Greg Clayton57abc5d2013-05-10 21:47:16 +0000738 "Disassembler that uses LLVM MC to disassemble i386, x86_64 and ARM.",
Sean Callanan95e5c632012-02-17 00:53:45 +0000739 CreateInstance);
740
741 llvm::InitializeAllTargetInfos();
742 llvm::InitializeAllTargetMCs();
743 llvm::InitializeAllAsmParsers();
744 llvm::InitializeAllDisassemblers();
745}
746
747void
748DisassemblerLLVMC::Terminate()
749{
750 PluginManager::UnregisterPlugin (CreateInstance);
751}
752
753
Greg Clayton57abc5d2013-05-10 21:47:16 +0000754ConstString
Sean Callanan95e5c632012-02-17 00:53:45 +0000755DisassemblerLLVMC::GetPluginNameStatic()
756{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000757 static ConstString g_name("llvm-mc");
758 return g_name;
Sean Callanan95e5c632012-02-17 00:53:45 +0000759}
760
Greg Claytonba812f42012-05-10 02:52:23 +0000761int DisassemblerLLVMC::OpInfoCallback (void *disassembler,
762 uint64_t pc,
763 uint64_t offset,
764 uint64_t size,
765 int tag_type,
766 void *tag_bug)
Sean Callanan95e5c632012-02-17 00:53:45 +0000767{
Greg Claytonba812f42012-05-10 02:52:23 +0000768 return static_cast<DisassemblerLLVMC*>(disassembler)->OpInfo (pc,
769 offset,
770 size,
771 tag_type,
772 tag_bug);
Sean Callanan95e5c632012-02-17 00:53:45 +0000773}
774
Greg Claytonba812f42012-05-10 02:52:23 +0000775const char *DisassemblerLLVMC::SymbolLookupCallback (void *disassembler,
776 uint64_t value,
777 uint64_t *type,
778 uint64_t pc,
779 const char **name)
Sean Callanan95e5c632012-02-17 00:53:45 +0000780{
Greg Claytonba812f42012-05-10 02:52:23 +0000781 return static_cast<DisassemblerLLVMC*>(disassembler)->SymbolLookup(value,
782 type,
783 pc,
784 name);
Sean Callanan95e5c632012-02-17 00:53:45 +0000785}
786
787int DisassemblerLLVMC::OpInfo (uint64_t PC,
788 uint64_t Offset,
789 uint64_t Size,
Greg Claytonba812f42012-05-10 02:52:23 +0000790 int tag_type,
791 void *tag_bug)
Sean Callanan95e5c632012-02-17 00:53:45 +0000792{
Greg Claytonba812f42012-05-10 02:52:23 +0000793 switch (tag_type)
Sean Callanan95e5c632012-02-17 00:53:45 +0000794 {
795 default:
796 break;
797 case 1:
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000798 memset (tag_bug, 0, sizeof(::LLVMOpInfo1));
Sean Callanan95e5c632012-02-17 00:53:45 +0000799 break;
800 }
801 return 0;
802}
803
Greg Claytonba812f42012-05-10 02:52:23 +0000804const char *DisassemblerLLVMC::SymbolLookup (uint64_t value,
805 uint64_t *type_ptr,
806 uint64_t pc,
807 const char **name)
Sean Callanan95e5c632012-02-17 00:53:45 +0000808{
Greg Claytonba812f42012-05-10 02:52:23 +0000809 if (*type_ptr)
810 {
811 if (m_exe_ctx && m_inst)
812 {
813 //std::string remove_this_prior_to_checkin;
Greg Claytonba812f42012-05-10 02:52:23 +0000814 Target *target = m_exe_ctx ? m_exe_ctx->GetTargetPtr() : NULL;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000815 Address value_so_addr;
816 if (m_inst->UsingFileAddress())
Greg Claytonba812f42012-05-10 02:52:23 +0000817 {
818 ModuleSP module_sp(m_inst->GetAddress().GetModule());
819 if (module_sp)
Greg Clayton3faf47c2013-03-28 23:42:53 +0000820 module_sp->ResolveFileAddress(value, value_so_addr);
Greg Claytonba812f42012-05-10 02:52:23 +0000821 }
Greg Clayton3faf47c2013-03-28 23:42:53 +0000822 else if (target && !target->GetSectionLoadList().IsEmpty())
823 {
824 target->GetSectionLoadList().ResolveLoadAddress(value, value_so_addr);
825 }
826
827 if (value_so_addr.IsValid() && value_so_addr.GetSection())
Sean Callanan95e5c632012-02-17 00:53:45 +0000828 {
Sean Callanan95e5c632012-02-17 00:53:45 +0000829 StreamString ss;
830
Greg Clayton3faf47c2013-03-28 23:42:53 +0000831 value_so_addr.Dump (&ss,
832 target,
833 Address::DumpStyleResolvedDescriptionNoModule,
834 Address::DumpStyleSectionNameOffset);
Sean Callanan95e5c632012-02-17 00:53:45 +0000835
Sean Callanan745af462012-03-22 20:04:23 +0000836 if (!ss.GetString().empty())
Greg Claytonba812f42012-05-10 02:52:23 +0000837 {
Greg Claytonba812f42012-05-10 02:52:23 +0000838 m_inst->AppendComment(ss.GetString());
839 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000840 }
841 }
842 }
Greg Claytonba812f42012-05-10 02:52:23 +0000843
844 *type_ptr = LLVMDisassembler_ReferenceType_InOut_None;
845 *name = NULL;
846 return NULL;
Sean Callanan95e5c632012-02-17 00:53:45 +0000847}
848
849//------------------------------------------------------------------
850// PluginInterface protocol
851//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000852ConstString
Sean Callanan95e5c632012-02-17 00:53:45 +0000853DisassemblerLLVMC::GetPluginName()
854{
Sean Callanan95e5c632012-02-17 00:53:45 +0000855 return GetPluginNameStatic();
856}
857
858uint32_t
859DisassemblerLLVMC::GetPluginVersion()
860{
861 return 1;
862}
863