blob: d20219f6d1eb6019068c218d84acee8691c72b5f [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"
Jim Ingham0f063ba2013-03-02 00:26:47 +000013#include "llvm/MC/MCAsmInfo.h"
14#include "llvm/MC/MCContext.h"
15#include "llvm/MC/MCDisassembler.h"
Greg Clayton3434b572014-04-14 21:33:38 +000016#include "llvm/MC/MCExternalSymbolizer.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000017#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"
Jim Ingham0f063ba2013-03-02 00:26:47 +000024#include "llvm/Support/TargetRegistry.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000025#include "llvm/Support/TargetSelect.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000026#include "llvm/ADT/SmallString.h"
27
Sean Callanan95e5c632012-02-17 00:53:45 +000028
29#include "lldb/Core/Address.h"
30#include "lldb/Core/DataExtractor.h"
Greg Clayton1f746072012-08-29 21:13:06 +000031#include "lldb/Core/Module.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000032#include "lldb/Core/Stream.h"
33#include "lldb/Symbol/SymbolContext.h"
34#include "lldb/Target/ExecutionContext.h"
35#include "lldb/Target/Process.h"
36#include "lldb/Target/RegisterContext.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000037#include "lldb/Target/SectionLoadList.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000038#include "lldb/Target/Target.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000039#include "lldb/Target/StackFrame.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000040
Virgile Bellob2f1fb22013-08-23 12:44:05 +000041#include "lldb/Core/RegularExpression.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000042
43using namespace lldb;
44using namespace lldb_private;
45
46class InstructionLLVMC : public lldb_private::Instruction
47{
48public:
49 InstructionLLVMC (DisassemblerLLVMC &disasm,
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000050 const lldb_private::Address &address,
Greg Claytonc8e0c242012-04-13 00:07:34 +000051 AddressClass addr_class) :
Greg Clayton3faf47c2013-03-28 23:42:53 +000052 Instruction (address, addr_class),
53 m_disasm_sp (disasm.shared_from_this()),
54 m_does_branch (eLazyBoolCalculate),
55 m_is_valid (false),
56 m_using_file_addr (false)
Sean Callanan95e5c632012-02-17 00:53:45 +000057 {
58 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000059
Sean Callanan95e5c632012-02-17 00:53:45 +000060 virtual
61 ~InstructionLLVMC ()
62 {
63 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000064
Sean Callanan95e5c632012-02-17 00:53:45 +000065 virtual bool
Jim Ingham32ce20c2013-03-13 01:55:16 +000066 DoesBranch ()
Sean Callanan95e5c632012-02-17 00:53:45 +000067 {
Jim Ingham32ce20c2013-03-13 01:55:16 +000068 if (m_does_branch == eLazyBoolCalculate)
69 {
Greg Clayton3faf47c2013-03-28 23:42:53 +000070 GetDisassemblerLLVMC().Lock(this, NULL);
Jim Ingham32ce20c2013-03-13 01:55:16 +000071 DataExtractor data;
72 if (m_opcode.GetData(data))
73 {
74 bool is_alternate_isa;
75 lldb::addr_t pc = m_address.GetFileAddress();
76
77 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr = GetDisasmToUse (is_alternate_isa);
Greg Clayton3faf47c2013-03-28 23:42:53 +000078 const uint8_t *opcode_data = data.GetDataStart();
Jim Ingham32ce20c2013-03-13 01:55:16 +000079 const size_t opcode_data_len = data.GetByteSize();
80 llvm::MCInst inst;
Greg Clayton3faf47c2013-03-28 23:42:53 +000081 const size_t inst_size = mc_disasm_ptr->GetMCInst (opcode_data,
82 opcode_data_len,
83 pc,
84 inst);
Jim Ingham32ce20c2013-03-13 01:55:16 +000085 // Be conservative, if we didn't understand the instruction, say it might branch...
86 if (inst_size == 0)
87 m_does_branch = eLazyBoolYes;
88 else
89 {
Greg Clayton3faf47c2013-03-28 23:42:53 +000090 const bool can_branch = mc_disasm_ptr->CanBranch(inst);
Jim Ingham32ce20c2013-03-13 01:55:16 +000091 if (can_branch)
92 m_does_branch = eLazyBoolYes;
93 else
94 m_does_branch = eLazyBoolNo;
95 }
96 }
Greg Clayton3faf47c2013-03-28 23:42:53 +000097 GetDisassemblerLLVMC().Unlock();
Jim Ingham32ce20c2013-03-13 01:55:16 +000098 }
Sean Callanan7725a462012-03-02 23:22:53 +000099 return m_does_branch == eLazyBoolYes;
Sean Callanan95e5c632012-02-17 00:53:45 +0000100 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000101
Jim Ingham32ce20c2013-03-13 01:55:16 +0000102 DisassemblerLLVMC::LLVMCDisassembler *
103 GetDisasmToUse (bool &is_alternate_isa)
104 {
Jim Ingham32ce20c2013-03-13 01:55:16 +0000105 is_alternate_isa = false;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000106 DisassemblerLLVMC &llvm_disasm = GetDisassemblerLLVMC();
107 if (llvm_disasm.m_alternate_disasm_ap.get() != NULL)
Jim Ingham32ce20c2013-03-13 01:55:16 +0000108 {
109 const AddressClass address_class = GetAddressClass ();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000110
Jim Ingham32ce20c2013-03-13 01:55:16 +0000111 if (address_class == eAddressClassCodeAlternateISA)
112 {
Jim Ingham32ce20c2013-03-13 01:55:16 +0000113 is_alternate_isa = true;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000114 return llvm_disasm.m_alternate_disasm_ap.get();
Jim Ingham32ce20c2013-03-13 01:55:16 +0000115 }
116 }
Greg Clayton3faf47c2013-03-28 23:42:53 +0000117 return llvm_disasm.m_disasm_ap.get();
Jim Ingham32ce20c2013-03-13 01:55:16 +0000118 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000119
Sean Callanan95e5c632012-02-17 00:53:45 +0000120 virtual size_t
121 Decode (const lldb_private::Disassembler &disassembler,
122 const lldb_private::DataExtractor &data,
Greg Claytonc7bece562013-01-25 18:06:21 +0000123 lldb::offset_t data_offset)
Sean Callanan95e5c632012-02-17 00:53:45 +0000124 {
Greg Claytonba812f42012-05-10 02:52:23 +0000125 // All we have to do is read the opcode which can be easy for some
Michael Sartaincc791bb2013-07-11 16:40:56 +0000126 // architectures
Greg Claytonba812f42012-05-10 02:52:23 +0000127 bool got_op = false;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000128 DisassemblerLLVMC &llvm_disasm = GetDisassemblerLLVMC();
129 const ArchSpec &arch = llvm_disasm.GetArchitecture();
Ed Maste90359962013-12-09 19:45:33 +0000130 const lldb::ByteOrder byte_order = data.GetByteOrder();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000131
Greg Claytonba812f42012-05-10 02:52:23 +0000132 const uint32_t min_op_byte_size = arch.GetMinimumOpcodeByteSize();
133 const uint32_t max_op_byte_size = arch.GetMaximumOpcodeByteSize();
134 if (min_op_byte_size == max_op_byte_size)
135 {
136 // Fixed size instructions, just read that amount of data.
137 if (!data.ValidOffsetForDataOfSize(data_offset, min_op_byte_size))
138 return false;
Ed Maste90359962013-12-09 19:45:33 +0000139
Greg Claytonba812f42012-05-10 02:52:23 +0000140 switch (min_op_byte_size)
141 {
142 case 1:
Ed Maste90359962013-12-09 19:45:33 +0000143 m_opcode.SetOpcode8 (data.GetU8 (&data_offset), byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000144 got_op = true;
145 break;
146
147 case 2:
Ed Maste90359962013-12-09 19:45:33 +0000148 m_opcode.SetOpcode16 (data.GetU16 (&data_offset), byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000149 got_op = true;
150 break;
151
152 case 4:
Ed Maste90359962013-12-09 19:45:33 +0000153 m_opcode.SetOpcode32 (data.GetU32 (&data_offset), byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000154 got_op = true;
155 break;
156
157 case 8:
Ed Maste90359962013-12-09 19:45:33 +0000158 m_opcode.SetOpcode64 (data.GetU64 (&data_offset), byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000159 got_op = true;
160 break;
161
162 default:
163 m_opcode.SetOpcodeBytes(data.PeekData(data_offset, min_op_byte_size), min_op_byte_size);
164 got_op = true;
165 break;
166 }
167 }
168 if (!got_op)
169 {
Jim Ingham32ce20c2013-03-13 01:55:16 +0000170 bool is_alternate_isa = false;
171 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr = GetDisasmToUse (is_alternate_isa);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000172
Greg Claytonba812f42012-05-10 02:52:23 +0000173 const llvm::Triple::ArchType machine = arch.GetMachine();
174 if (machine == llvm::Triple::arm || machine == llvm::Triple::thumb)
175 {
Jim Ingham32ce20c2013-03-13 01:55:16 +0000176 if (machine == llvm::Triple::thumb || is_alternate_isa)
Greg Claytonba812f42012-05-10 02:52:23 +0000177 {
Greg Clayton79101b52012-08-07 01:29:29 +0000178 uint32_t thumb_opcode = data.GetU16(&data_offset);
Greg Claytonba812f42012-05-10 02:52:23 +0000179 if ((thumb_opcode & 0xe000) != 0xe000 || ((thumb_opcode & 0x1800u) == 0))
180 {
Ed Maste90359962013-12-09 19:45:33 +0000181 m_opcode.SetOpcode16 (thumb_opcode, byte_order);
Sean Callanan5c97c2f2012-08-06 23:42:52 +0000182 m_is_valid = true;
Greg Claytonba812f42012-05-10 02:52:23 +0000183 }
184 else
185 {
Greg Clayton79101b52012-08-07 01:29:29 +0000186 thumb_opcode <<= 16;
187 thumb_opcode |= data.GetU16(&data_offset);
Ed Maste90359962013-12-09 19:45:33 +0000188 m_opcode.SetOpcode16_2 (thumb_opcode, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000189 m_is_valid = true;
190 }
191 }
192 else
193 {
Ed Maste90359962013-12-09 19:45:33 +0000194 m_opcode.SetOpcode32 (data.GetU32(&data_offset), byte_order);
Sean Callanan5c97c2f2012-08-06 23:42:52 +0000195 m_is_valid = true;
Greg Claytonba812f42012-05-10 02:52:23 +0000196 }
197 }
198 else
199 {
200 // The opcode isn't evenly sized, so we need to actually use the llvm
201 // disassembler to parse it and get the size.
Greg Claytonba812f42012-05-10 02:52:23 +0000202 uint8_t *opcode_data = const_cast<uint8_t *>(data.PeekData (data_offset, 1));
Greg Clayton3faf47c2013-03-28 23:42:53 +0000203 const size_t opcode_data_len = data.BytesLeft(data_offset);
Greg Claytonba812f42012-05-10 02:52:23 +0000204 const addr_t pc = m_address.GetFileAddress();
Jim Ingham0f063ba2013-03-02 00:26:47 +0000205 llvm::MCInst inst;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000206
Greg Clayton3faf47c2013-03-28 23:42:53 +0000207 llvm_disasm.Lock(this, NULL);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000208 const size_t inst_size = mc_disasm_ptr->GetMCInst(opcode_data,
Greg Clayton3faf47c2013-03-28 23:42:53 +0000209 opcode_data_len,
210 pc,
211 inst);
212 llvm_disasm.Unlock();
Greg Claytonba812f42012-05-10 02:52:23 +0000213 if (inst_size == 0)
214 m_opcode.Clear();
215 else
216 {
217 m_opcode.SetOpcodeBytes(opcode_data, inst_size);
218 m_is_valid = true;
219 }
220 }
221 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000222 return m_opcode.GetByteSize();
223 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000224
Sean Callanan95e5c632012-02-17 00:53:45 +0000225 void
Greg Claytonba812f42012-05-10 02:52:23 +0000226 AppendComment (std::string &description)
Sean Callanan95e5c632012-02-17 00:53:45 +0000227 {
Greg Claytonba812f42012-05-10 02:52:23 +0000228 if (m_comment.empty())
229 m_comment.swap (description);
Sean Callanan95e5c632012-02-17 00:53:45 +0000230 else
Greg Claytonba812f42012-05-10 02:52:23 +0000231 {
232 m_comment.append(", ");
233 m_comment.append(description);
234 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000235 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000236
Sean Callanan95e5c632012-02-17 00:53:45 +0000237 virtual void
Greg Claytonba812f42012-05-10 02:52:23 +0000238 CalculateMnemonicOperandsAndComment (const lldb_private::ExecutionContext *exe_ctx)
Sean Callanan95e5c632012-02-17 00:53:45 +0000239 {
Greg Claytonba812f42012-05-10 02:52:23 +0000240 DataExtractor data;
241 const AddressClass address_class = GetAddressClass ();
242
Sean Callanancd4ae1a2012-08-07 01:44:58 +0000243 if (m_opcode.GetData(data))
Greg Claytonba812f42012-05-10 02:52:23 +0000244 {
245 char out_string[512];
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000246
Greg Clayton3faf47c2013-03-28 23:42:53 +0000247 DisassemblerLLVMC &llvm_disasm = GetDisassemblerLLVMC();
248
Jim Ingham0f063ba2013-03-02 00:26:47 +0000249 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000250
Greg Claytonba812f42012-05-10 02:52:23 +0000251 if (address_class == eAddressClassCodeAlternateISA)
Greg Clayton3faf47c2013-03-28 23:42:53 +0000252 mc_disasm_ptr = llvm_disasm.m_alternate_disasm_ap.get();
Greg Claytonba812f42012-05-10 02:52:23 +0000253 else
Greg Clayton3faf47c2013-03-28 23:42:53 +0000254 mc_disasm_ptr = llvm_disasm.m_disasm_ap.get();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000255
Greg Clayton3faf47c2013-03-28 23:42:53 +0000256 lldb::addr_t pc = m_address.GetFileAddress();
257 m_using_file_addr = true;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000258
Greg Clayton3faf47c2013-03-28 23:42:53 +0000259 const bool data_from_file = GetDisassemblerLLVMC().m_data_from_file;
Daniel Malead79ae052013-08-07 21:54:09 +0000260 bool use_hex_immediates = true;
261 Disassembler::HexImmediateStyle hex_style = Disassembler::eHexStyleC;
262
263 if (exe_ctx)
Greg Claytonba812f42012-05-10 02:52:23 +0000264 {
Daniel Malead79ae052013-08-07 21:54:09 +0000265 Target *target = exe_ctx->GetTargetPtr();
266 if (target)
Greg Clayton3faf47c2013-03-28 23:42:53 +0000267 {
Daniel Malead79ae052013-08-07 21:54:09 +0000268 use_hex_immediates = target->GetUseHexImmediates();
269 hex_style = target->GetHexImmediateStyle();
270
271 if (!data_from_file)
Greg Clayton3faf47c2013-03-28 23:42:53 +0000272 {
273 const lldb::addr_t load_addr = m_address.GetLoadAddress(target);
274 if (load_addr != LLDB_INVALID_ADDRESS)
275 {
276 pc = load_addr;
277 m_using_file_addr = false;
278 }
279 }
280 }
Greg Claytonba812f42012-05-10 02:52:23 +0000281 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000282
Greg Clayton3faf47c2013-03-28 23:42:53 +0000283 llvm_disasm.Lock(this, exe_ctx);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000284
Greg Clayton3faf47c2013-03-28 23:42:53 +0000285 const uint8_t *opcode_data = data.GetDataStart();
Greg Claytonba812f42012-05-10 02:52:23 +0000286 const size_t opcode_data_len = data.GetByteSize();
Jim Ingham0f063ba2013-03-02 00:26:47 +0000287 llvm::MCInst inst;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000288 size_t inst_size = mc_disasm_ptr->GetMCInst (opcode_data,
289 opcode_data_len,
290 pc,
291 inst);
Daniel Malead79ae052013-08-07 21:54:09 +0000292
Jim Ingham0f063ba2013-03-02 00:26:47 +0000293 if (inst_size > 0)
Daniel Malead79ae052013-08-07 21:54:09 +0000294 {
295 mc_disasm_ptr->SetStyle(use_hex_immediates, hex_style);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000296 mc_disasm_ptr->PrintMCInst(inst, out_string, sizeof(out_string));
Daniel Malead79ae052013-08-07 21:54:09 +0000297 }
298
Greg Clayton3faf47c2013-03-28 23:42:53 +0000299 llvm_disasm.Unlock();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000300
Greg Claytonba812f42012-05-10 02:52:23 +0000301 if (inst_size == 0)
302 {
303 m_comment.assign ("unknown opcode");
304 inst_size = m_opcode.GetByteSize();
305 StreamString mnemonic_strm;
Greg Claytonc7bece562013-01-25 18:06:21 +0000306 lldb::offset_t offset = 0;
Ed Maste90359962013-12-09 19:45:33 +0000307 lldb::ByteOrder byte_order = data.GetByteOrder();
Greg Claytonba812f42012-05-10 02:52:23 +0000308 switch (inst_size)
309 {
310 case 1:
311 {
312 const uint8_t uval8 = data.GetU8 (&offset);
Ed Maste90359962013-12-09 19:45:33 +0000313 m_opcode.SetOpcode8 (uval8, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000314 m_opcode_name.assign (".byte");
315 mnemonic_strm.Printf("0x%2.2x", uval8);
316 }
317 break;
318 case 2:
319 {
320 const uint16_t uval16 = data.GetU16(&offset);
Ed Maste90359962013-12-09 19:45:33 +0000321 m_opcode.SetOpcode16(uval16, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000322 m_opcode_name.assign (".short");
323 mnemonic_strm.Printf("0x%4.4x", uval16);
324 }
325 break;
326 case 4:
327 {
328 const uint32_t uval32 = data.GetU32(&offset);
Ed Maste90359962013-12-09 19:45:33 +0000329 m_opcode.SetOpcode32(uval32, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000330 m_opcode_name.assign (".long");
331 mnemonic_strm.Printf("0x%8.8x", uval32);
332 }
333 break;
334 case 8:
335 {
336 const uint64_t uval64 = data.GetU64(&offset);
Ed Maste90359962013-12-09 19:45:33 +0000337 m_opcode.SetOpcode64(uval64, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000338 m_opcode_name.assign (".quad");
Daniel Malead01b2952012-11-29 21:49:15 +0000339 mnemonic_strm.Printf("0x%16.16" PRIx64, uval64);
Greg Claytonba812f42012-05-10 02:52:23 +0000340 }
341 break;
342 default:
343 if (inst_size == 0)
344 return;
345 else
346 {
347 const uint8_t *bytes = data.PeekData(offset, inst_size);
348 if (bytes == NULL)
349 return;
350 m_opcode_name.assign (".byte");
351 m_opcode.SetOpcodeBytes(bytes, inst_size);
352 mnemonic_strm.Printf("0x%2.2x", bytes[0]);
353 for (uint32_t i=1; i<inst_size; ++i)
354 mnemonic_strm.Printf(" 0x%2.2x", bytes[i]);
355 }
356 break;
357 }
Jim Ingham0f063ba2013-03-02 00:26:47 +0000358 m_mnemonics.swap(mnemonic_strm.GetString());
Greg Claytonba812f42012-05-10 02:52:23 +0000359 return;
360 }
361 else
362 {
363 if (m_does_branch == eLazyBoolCalculate)
364 {
Greg Clayton3faf47c2013-03-28 23:42:53 +0000365 const bool can_branch = mc_disasm_ptr->CanBranch(inst);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000366 if (can_branch)
Greg Claytonba812f42012-05-10 02:52:23 +0000367 m_does_branch = eLazyBoolYes;
368 else
369 m_does_branch = eLazyBoolNo;
Jim Ingham0f063ba2013-03-02 00:26:47 +0000370
Greg Claytonba812f42012-05-10 02:52:23 +0000371 }
372 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000373
Greg Clayton7bd4c602015-01-21 21:51:02 +0000374 static RegularExpression s_regex("[ \t]*([^ ^\t]+)[ \t]*([^ ^\t].*)?");
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000375
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000376 RegularExpression::Match matches(3);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000377
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000378 if (s_regex.Execute(out_string, &matches))
Greg Claytonba812f42012-05-10 02:52:23 +0000379 {
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000380 matches.GetMatchAtIndex(out_string, 1, m_opcode_name);
381 matches.GetMatchAtIndex(out_string, 2, m_mnemonics);
Greg Claytonba812f42012-05-10 02:52:23 +0000382 }
383 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000384 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000385
Sean Callanan95e5c632012-02-17 00:53:45 +0000386 bool
Greg Clayton3faf47c2013-03-28 23:42:53 +0000387 IsValid () const
Sean Callanan95e5c632012-02-17 00:53:45 +0000388 {
389 return m_is_valid;
390 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000391
Greg Clayton3faf47c2013-03-28 23:42:53 +0000392 bool
393 UsingFileAddress() const
394 {
395 return m_using_file_addr;
396 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000397 size_t
Greg Clayton3faf47c2013-03-28 23:42:53 +0000398 GetByteSize () const
Sean Callanan95e5c632012-02-17 00:53:45 +0000399 {
400 return m_opcode.GetByteSize();
401 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000402
Greg Clayton3faf47c2013-03-28 23:42:53 +0000403 DisassemblerLLVMC &
404 GetDisassemblerLLVMC ()
405 {
406 return *(DisassemblerLLVMC *)m_disasm_sp.get();
407 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000408protected:
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000409
Sean Callanan7e6d4e52012-08-01 18:50:59 +0000410 DisassemblerSP m_disasm_sp; // for ownership
Sean Callanan7725a462012-03-02 23:22:53 +0000411 LazyBool m_does_branch;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000412 bool m_is_valid;
413 bool m_using_file_addr;
Sean Callanan95e5c632012-02-17 00:53:45 +0000414};
415
Greg Claytondf96dd72013-08-27 19:53:47 +0000416
Sean Callanan95e5c632012-02-17 00:53:45 +0000417
Jaydeep Patil501a7812015-07-16 03:51:55 +0000418DisassemblerLLVMC::LLVMCDisassembler::LLVMCDisassembler (const char *triple, const char *cpu, const char *features_str, unsigned flavor, DisassemblerLLVMC &owner):
Jim Ingham0f063ba2013-03-02 00:26:47 +0000419 m_is_valid(true)
420{
421 std::string Error;
422 const llvm::Target *curr_target = llvm::TargetRegistry::lookupTarget(triple, Error);
423 if (!curr_target)
424 {
425 m_is_valid = false;
426 return;
427 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000428
Jim Ingham0f063ba2013-03-02 00:26:47 +0000429 m_instr_info_ap.reset(curr_target->createMCInstrInfo());
430 m_reg_info_ap.reset (curr_target->createMCRegInfo(triple));
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000431
Mohit K. Bhakkad276a9302015-06-18 06:03:27 +0000432 m_subtarget_info_ap.reset(curr_target->createMCSubtargetInfo(triple, cpu,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000433 features_str));
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000434
Jean-Daniel Dupasc6f26f82013-12-29 20:17:26 +0000435 std::unique_ptr<llvm::MCRegisterInfo> reg_info(curr_target->createMCRegInfo(triple));
436 m_asm_info_ap.reset(curr_target->createMCAsmInfo(*reg_info, triple));
Sylvestre Ledru7a89d6f2013-05-13 13:41:13 +0000437
Jim Ingham0f063ba2013-03-02 00:26:47 +0000438 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)
439 {
Matt Kopec787d1622013-03-12 17:45:38 +0000440 m_is_valid = false;
Jim Ingham0f063ba2013-03-02 00:26:47 +0000441 return;
442 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000443
Bill Wendlingd63d0a82013-06-18 07:50:43 +0000444 m_context_ap.reset(new llvm::MCContext(m_asm_info_ap.get(), m_reg_info_ap.get(), 0));
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000445
Sylvestre Ledru5ac35ae2014-04-15 12:07:25 +0000446 m_disasm_ap.reset(curr_target->createMCDisassembler(*m_subtarget_info_ap.get(), *m_context_ap.get()));
Ashok Thirumurthi78059352013-05-24 15:55:54 +0000447 if (m_disasm_ap.get() && m_context_ap.get())
Jim Ingham0f063ba2013-03-02 00:26:47 +0000448 {
Ahmed Charles8f926ad2014-03-07 04:45:22 +0000449 std::unique_ptr<llvm::MCRelocationInfo> RelInfo(curr_target->createMCRelocationInfo(triple, *m_context_ap.get()));
Ashok Thirumurthi78059352013-05-24 15:55:54 +0000450 if (!RelInfo)
451 {
452 m_is_valid = false;
453 return;
454 }
Jason Molenda64a68d62014-05-17 00:27:44 +0000455 std::unique_ptr<llvm::MCSymbolizer> symbolizer_up(curr_target->createMCSymbolizer(triple, NULL,
456 DisassemblerLLVMC::SymbolLookupCallback,
457 (void *) &owner,
Chandler Carruth215d9392015-01-19 03:07:25 +0000458 m_context_ap.get(), std::move(RelInfo)));
Jason Molenda64a68d62014-05-17 00:27:44 +0000459 m_disasm_ap->setSymbolizer(std::move(symbolizer_up));
Greg Clayton3434b572014-04-14 21:33:38 +0000460
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000461
Jim Ingham0f063ba2013-03-02 00:26:47 +0000462 unsigned asm_printer_variant;
463 if (flavor == ~0U)
464 asm_printer_variant = m_asm_info_ap->getAssemblerDialect();
465 else
466 {
467 asm_printer_variant = flavor;
468 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000469
Chaoren Lin2d6105c2015-03-31 00:59:13 +0000470 m_instr_printer_ap.reset(curr_target->createMCInstPrinter(llvm::Triple{triple},
471 asm_printer_variant,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000472 *m_asm_info_ap.get(),
473 *m_instr_info_ap.get(),
Chaoren Lin2d6105c2015-03-31 00:59:13 +0000474 *m_reg_info_ap.get()));
Jim Ingham0f063ba2013-03-02 00:26:47 +0000475 if (m_instr_printer_ap.get() == NULL)
476 {
477 m_disasm_ap.reset();
478 m_is_valid = false;
479 }
480 }
481 else
482 m_is_valid = false;
483}
484
Greg Claytone01e07b2013-04-18 18:10:51 +0000485DisassemblerLLVMC::LLVMCDisassembler::~LLVMCDisassembler()
486{
487}
488
Jim Ingham0f063ba2013-03-02 00:26:47 +0000489uint64_t
Greg Clayton3faf47c2013-03-28 23:42:53 +0000490DisassemblerLLVMC::LLVMCDisassembler::GetMCInst (const uint8_t *opcode_data,
491 size_t opcode_data_len,
492 lldb::addr_t pc,
493 llvm::MCInst &mc_inst)
Jim Ingham0f063ba2013-03-02 00:26:47 +0000494{
Rafael Espindola434df0a2014-11-12 02:04:31 +0000495 llvm::ArrayRef<uint8_t> data(opcode_data, opcode_data_len);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000496 llvm::MCDisassembler::DecodeStatus status;
497
498 uint64_t new_inst_size;
499 status = m_disasm_ap->getInstruction(mc_inst,
500 new_inst_size,
Rafael Espindola434df0a2014-11-12 02:04:31 +0000501 data,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000502 pc,
503 llvm::nulls(),
504 llvm::nulls());
505 if (status == llvm::MCDisassembler::Success)
506 return new_inst_size;
507 else
508 return 0;
509}
510
511uint64_t
Greg Clayton3faf47c2013-03-28 23:42:53 +0000512DisassemblerLLVMC::LLVMCDisassembler::PrintMCInst (llvm::MCInst &mc_inst,
513 char *dst,
514 size_t dst_len)
Jim Ingham0f063ba2013-03-02 00:26:47 +0000515{
516 llvm::StringRef unused_annotations;
517 llvm::SmallString<64> inst_string;
518 llvm::raw_svector_ostream inst_stream(inst_string);
Akira Hatanakaafbac282015-03-27 21:45:58 +0000519 m_instr_printer_ap->printInst (&mc_inst, inst_stream, unused_annotations,
520 *m_subtarget_info_ap);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000521 inst_stream.flush();
Greg Clayton3faf47c2013-03-28 23:42:53 +0000522 const size_t output_size = std::min(dst_len - 1, inst_string.size());
523 std::memcpy(dst, inst_string.data(), output_size);
524 dst[output_size] = '\0';
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000525
Jim Ingham0f063ba2013-03-02 00:26:47 +0000526 return output_size;
527}
528
Daniel Malead79ae052013-08-07 21:54:09 +0000529void
530DisassemblerLLVMC::LLVMCDisassembler::SetStyle (bool use_hex_immed, HexImmediateStyle hex_style)
531{
532 m_instr_printer_ap->setPrintImmHex(use_hex_immed);
533 switch(hex_style)
534 {
Oleksiy Vyalov37ff6c42015-06-08 17:10:27 +0000535 case eHexStyleC: m_instr_printer_ap->setPrintHexStyle(llvm::HexStyle::C); break;
536 case eHexStyleAsm: m_instr_printer_ap->setPrintHexStyle(llvm::HexStyle::Asm); break;
Daniel Malead79ae052013-08-07 21:54:09 +0000537 }
538}
539
Jim Ingham0f063ba2013-03-02 00:26:47 +0000540bool
541DisassemblerLLVMC::LLVMCDisassembler::CanBranch (llvm::MCInst &mc_inst)
542{
543 return m_instr_info_ap->get(mc_inst.getOpcode()).mayAffectControlFlow(mc_inst, *m_reg_info_ap.get());
544}
545
546bool
547DisassemblerLLVMC::FlavorValidForArchSpec (const lldb_private::ArchSpec &arch, const char *flavor)
548{
549 llvm::Triple triple = arch.GetTriple();
550 if (flavor == NULL || strcmp (flavor, "default") == 0)
551 return true;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000552
Jim Ingham0f063ba2013-03-02 00:26:47 +0000553 if (triple.getArch() == llvm::Triple::x86 || triple.getArch() == llvm::Triple::x86_64)
554 {
555 if (strcmp (flavor, "intel") == 0 || strcmp (flavor, "att") == 0)
556 return true;
557 else
558 return false;
559 }
560 else
561 return false;
562}
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000563
Jim Ingham0f063ba2013-03-02 00:26:47 +0000564
Sean Callanan95e5c632012-02-17 00:53:45 +0000565Disassembler *
Jim Ingham0f063ba2013-03-02 00:26:47 +0000566DisassemblerLLVMC::CreateInstance (const ArchSpec &arch, const char *flavor)
Sean Callanan95e5c632012-02-17 00:53:45 +0000567{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000568 if (arch.GetTriple().getArch() != llvm::Triple::UnknownArch)
569 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000570 std::unique_ptr<DisassemblerLLVMC> disasm_ap (new DisassemblerLLVMC(arch, flavor));
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000571
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000572 if (disasm_ap.get() && disasm_ap->IsValid())
573 return disasm_ap.release();
574 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000575 return NULL;
576}
577
Jim Ingham0f063ba2013-03-02 00:26:47 +0000578DisassemblerLLVMC::DisassemblerLLVMC (const ArchSpec &arch, const char *flavor_string) :
579 Disassembler(arch, flavor_string),
Greg Claytonba812f42012-05-10 02:52:23 +0000580 m_exe_ctx (NULL),
Greg Clayton3faf47c2013-03-28 23:42:53 +0000581 m_inst (NULL),
582 m_data_from_file (false)
Sean Callanan95e5c632012-02-17 00:53:45 +0000583{
Jim Ingham0f063ba2013-03-02 00:26:47 +0000584 if (!FlavorValidForArchSpec (arch, m_flavor.c_str()))
585 {
586 m_flavor.assign("default");
587 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000588
Jim Ingham0f063ba2013-03-02 00:26:47 +0000589 const char *triple = arch.GetTriple().getTriple().c_str();
590 unsigned flavor = ~0U;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000591
Jim Ingham0f063ba2013-03-02 00:26:47 +0000592 // So far the only supported flavor is "intel" on x86. The base class will set this
593 // correctly coming in.
594 if (arch.GetTriple().getArch() == llvm::Triple::x86
595 || arch.GetTriple().getArch() == llvm::Triple::x86_64)
596 {
597 if (m_flavor == "intel")
598 {
599 flavor = 1;
600 }
601 else if (m_flavor == "att")
602 {
603 flavor = 0;
604 }
605 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000606
Jason Molenda01aa5342013-07-22 22:11:53 +0000607 ArchSpec thumb_arch(arch);
Sean Callanan95e5c632012-02-17 00:53:45 +0000608 if (arch.GetTriple().getArch() == llvm::Triple::arm)
609 {
Greg Claytona80ea122013-05-03 01:05:04 +0000610 std::string thumb_arch_name (thumb_arch.GetTriple().getArchName().str());
611 // Replace "arm" with "thumb" so we get all thumb variants correct
612 if (thumb_arch_name.size() > 3)
613 {
614 thumb_arch_name.erase(0,3);
615 thumb_arch_name.insert(0, "thumb");
616 }
617 else
618 {
619 thumb_arch_name = "thumbv7";
620 }
621 thumb_arch.GetTriple().setArchName(llvm::StringRef(thumb_arch_name.c_str()));
Jason Molenda01aa5342013-07-22 22:11:53 +0000622 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000623
624 // Cortex-M3 devices (e.g. armv7m) can only execute thumb (T2) instructions,
Jason Molendaa3a04522013-09-27 23:21:54 +0000625 // so hardcode the primary disassembler to thumb mode. Same for Cortex-M4 (armv7em).
626 //
627 // Handle the Cortex-M0 (armv6m) the same; the ISA is a subset of the T and T32
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000628 // instructions defined in ARMv7-A.
Jason Molendaa3a04522013-09-27 23:21:54 +0000629
Jason Molenda01aa5342013-07-22 22:11:53 +0000630 if (arch.GetTriple().getArch() == llvm::Triple::arm
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000631 && (arch.GetCore() == ArchSpec::Core::eCore_arm_armv7m
Jason Molendaa3a04522013-09-27 23:21:54 +0000632 || arch.GetCore() == ArchSpec::Core::eCore_arm_armv7em
633 || arch.GetCore() == ArchSpec::Core::eCore_arm_armv6m))
Jason Molenda01aa5342013-07-22 22:11:53 +0000634 {
635 triple = thumb_arch.GetTriple().getTriple().c_str();
636 }
637
Mohit K. Bhakkad276a9302015-06-18 06:03:27 +0000638 const char *cpu = "";
639
640 switch (arch.GetCore())
641 {
642 case ArchSpec::eCore_mips32:
643 case ArchSpec::eCore_mips32el:
644 cpu = "mips32"; break;
645 case ArchSpec::eCore_mips32r2:
646 case ArchSpec::eCore_mips32r2el:
647 cpu = "mips32r2"; break;
648 case ArchSpec::eCore_mips32r3:
649 case ArchSpec::eCore_mips32r3el:
650 cpu = "mips32r3"; break;
651 case ArchSpec::eCore_mips32r5:
652 case ArchSpec::eCore_mips32r5el:
653 cpu = "mips32r5"; break;
654 case ArchSpec::eCore_mips32r6:
655 case ArchSpec::eCore_mips32r6el:
656 cpu = "mips32r6"; break;
657 case ArchSpec::eCore_mips64:
658 case ArchSpec::eCore_mips64el:
659 cpu = "mips64"; break;
660 case ArchSpec::eCore_mips64r2:
661 case ArchSpec::eCore_mips64r2el:
662 cpu = "mips64r2"; break;
663 case ArchSpec::eCore_mips64r3:
664 case ArchSpec::eCore_mips64r3el:
665 cpu = "mips64r3"; break;
666 case ArchSpec::eCore_mips64r5:
667 case ArchSpec::eCore_mips64r5el:
668 cpu = "mips64r5"; break;
669 case ArchSpec::eCore_mips64r6:
670 case ArchSpec::eCore_mips64r6el:
671 cpu = "mips64r6"; break;
672 default:
673 cpu = ""; break;
674 }
Jaydeep Patil501a7812015-07-16 03:51:55 +0000675
676 std::string features_str = "";
677 if (arch.GetTriple().getArch() == llvm::Triple::mips || arch.GetTriple().getArch() == llvm::Triple::mipsel
678 || arch.GetTriple().getArch() == llvm::Triple::mips64 || arch.GetTriple().getArch() == llvm::Triple::mips64el)
679 {
680 uint32_t arch_flags = arch.GetFlags ();
681 if (arch_flags & ArchSpec::eMIPSAse_msa)
682 features_str += "+msa,";
683 if (arch_flags & ArchSpec::eMIPSAse_dsp)
684 features_str += "+dsp,";
685 if (arch_flags & ArchSpec::eMIPSAse_dspr2)
686 features_str += "+dspr2,";
687 if (arch_flags & ArchSpec::eMIPSAse_mips16)
688 features_str += "+mips16,";
689 if (arch_flags & ArchSpec::eMIPSAse_micromips)
690 features_str += "+micromips,";
691 }
Mohit K. Bhakkad276a9302015-06-18 06:03:27 +0000692
Jaydeep Patil501a7812015-07-16 03:51:55 +0000693 m_disasm_ap.reset (new LLVMCDisassembler(triple, cpu, features_str.c_str(), flavor, *this));
Jason Molenda01aa5342013-07-22 22:11:53 +0000694 if (!m_disasm_ap->IsValid())
695 {
696 // We use m_disasm_ap.get() to tell whether we are valid or not, so if this isn't good for some reason,
697 // we reset it, and then we won't be valid and FindPlugin will fail and we won't get used.
698 m_disasm_ap.reset();
699 }
700
701 // For arm CPUs that can execute arm or thumb instructions, also create a thumb instruction disassembler.
702 if (arch.GetTriple().getArch() == llvm::Triple::arm)
703 {
Greg Claytonba812f42012-05-10 02:52:23 +0000704 std::string thumb_triple(thumb_arch.GetTriple().getTriple());
Jaydeep Patil501a7812015-07-16 03:51:55 +0000705 m_alternate_disasm_ap.reset(new LLVMCDisassembler(thumb_triple.c_str(), "", "", flavor, *this));
Jim Ingham0f063ba2013-03-02 00:26:47 +0000706 if (!m_alternate_disasm_ap->IsValid())
707 {
708 m_disasm_ap.reset();
709 m_alternate_disasm_ap.reset();
710 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000711 }
712}
713
714DisassemblerLLVMC::~DisassemblerLLVMC()
715{
716}
717
718size_t
719DisassemblerLLVMC::DecodeInstructions (const Address &base_addr,
720 const DataExtractor& data,
Greg Claytonc7bece562013-01-25 18:06:21 +0000721 lldb::offset_t data_offset,
722 size_t num_instructions,
Greg Clayton3faf47c2013-03-28 23:42:53 +0000723 bool append,
724 bool data_from_file)
Sean Callanan95e5c632012-02-17 00:53:45 +0000725{
726 if (!append)
727 m_instruction_list.Clear();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000728
Sean Callanan95e5c632012-02-17 00:53:45 +0000729 if (!IsValid())
730 return 0;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000731
Greg Clayton3faf47c2013-03-28 23:42:53 +0000732 m_data_from_file = data_from_file;
Sean Callanan95e5c632012-02-17 00:53:45 +0000733 uint32_t data_cursor = data_offset;
Greg Claytonba812f42012-05-10 02:52:23 +0000734 const size_t data_byte_size = data.GetByteSize();
Sean Callanan95e5c632012-02-17 00:53:45 +0000735 uint32_t instructions_parsed = 0;
Greg Claytonba812f42012-05-10 02:52:23 +0000736 Address inst_addr(base_addr);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000737
Greg Claytonba812f42012-05-10 02:52:23 +0000738 while (data_cursor < data_byte_size && instructions_parsed < num_instructions)
Sean Callanan95e5c632012-02-17 00:53:45 +0000739 {
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000740
Greg Claytonba812f42012-05-10 02:52:23 +0000741 AddressClass address_class = eAddressClassCode;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000742
Jim Ingham0f063ba2013-03-02 00:26:47 +0000743 if (m_alternate_disasm_ap.get() != NULL)
Greg Claytonba812f42012-05-10 02:52:23 +0000744 address_class = inst_addr.GetAddressClass ();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000745
Sean Callanan95e5c632012-02-17 00:53:45 +0000746 InstructionSP inst_sp(new InstructionLLVMC(*this,
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000747 inst_addr,
Sean Callanan95e5c632012-02-17 00:53:45 +0000748 address_class));
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000749
Sean Callanan95e5c632012-02-17 00:53:45 +0000750 if (!inst_sp)
Greg Claytonba812f42012-05-10 02:52:23 +0000751 break;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000752
Sean Callanan95e5c632012-02-17 00:53:45 +0000753 uint32_t inst_size = inst_sp->Decode(*this, data, data_cursor);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000754
Greg Claytonba812f42012-05-10 02:52:23 +0000755 if (inst_size == 0)
756 break;
757
Sean Callanan95e5c632012-02-17 00:53:45 +0000758 m_instruction_list.Append(inst_sp);
Sean Callanan95e5c632012-02-17 00:53:45 +0000759 data_cursor += inst_size;
Greg Claytonba812f42012-05-10 02:52:23 +0000760 inst_addr.Slide(inst_size);
Sean Callanan95e5c632012-02-17 00:53:45 +0000761 instructions_parsed++;
762 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000763
Sean Callanan95e5c632012-02-17 00:53:45 +0000764 return data_cursor - data_offset;
765}
766
767void
768DisassemblerLLVMC::Initialize()
769{
770 PluginManager::RegisterPlugin (GetPluginNameStatic(),
Jason Molendaa3329782014-03-29 18:54:20 +0000771 "Disassembler that uses LLVM MC to disassemble i386, x86_64, ARM, and ARM64.",
Sean Callanan95e5c632012-02-17 00:53:45 +0000772 CreateInstance);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000773
Sean Callanan95e5c632012-02-17 00:53:45 +0000774 llvm::InitializeAllTargetInfos();
775 llvm::InitializeAllTargetMCs();
776 llvm::InitializeAllAsmParsers();
777 llvm::InitializeAllDisassemblers();
778}
779
780void
781DisassemblerLLVMC::Terminate()
782{
783 PluginManager::UnregisterPlugin (CreateInstance);
784}
785
786
Greg Clayton57abc5d2013-05-10 21:47:16 +0000787ConstString
Sean Callanan95e5c632012-02-17 00:53:45 +0000788DisassemblerLLVMC::GetPluginNameStatic()
789{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000790 static ConstString g_name("llvm-mc");
791 return g_name;
Sean Callanan95e5c632012-02-17 00:53:45 +0000792}
793
Greg Claytonba812f42012-05-10 02:52:23 +0000794int DisassemblerLLVMC::OpInfoCallback (void *disassembler,
795 uint64_t pc,
796 uint64_t offset,
797 uint64_t size,
798 int tag_type,
799 void *tag_bug)
Sean Callanan95e5c632012-02-17 00:53:45 +0000800{
Greg Claytonba812f42012-05-10 02:52:23 +0000801 return static_cast<DisassemblerLLVMC*>(disassembler)->OpInfo (pc,
802 offset,
803 size,
804 tag_type,
805 tag_bug);
Sean Callanan95e5c632012-02-17 00:53:45 +0000806}
807
Greg Claytonba812f42012-05-10 02:52:23 +0000808const char *DisassemblerLLVMC::SymbolLookupCallback (void *disassembler,
809 uint64_t value,
810 uint64_t *type,
811 uint64_t pc,
812 const char **name)
Sean Callanan95e5c632012-02-17 00:53:45 +0000813{
Greg Claytonba812f42012-05-10 02:52:23 +0000814 return static_cast<DisassemblerLLVMC*>(disassembler)->SymbolLookup(value,
815 type,
816 pc,
817 name);
Sean Callanan95e5c632012-02-17 00:53:45 +0000818}
819
820int DisassemblerLLVMC::OpInfo (uint64_t PC,
821 uint64_t Offset,
822 uint64_t Size,
Greg Claytonba812f42012-05-10 02:52:23 +0000823 int tag_type,
824 void *tag_bug)
Sean Callanan95e5c632012-02-17 00:53:45 +0000825{
Greg Claytonba812f42012-05-10 02:52:23 +0000826 switch (tag_type)
Sean Callanan95e5c632012-02-17 00:53:45 +0000827 {
828 default:
829 break;
830 case 1:
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000831 memset (tag_bug, 0, sizeof(::LLVMOpInfo1));
Sean Callanan95e5c632012-02-17 00:53:45 +0000832 break;
833 }
834 return 0;
835}
836
Greg Claytonba812f42012-05-10 02:52:23 +0000837const char *DisassemblerLLVMC::SymbolLookup (uint64_t value,
838 uint64_t *type_ptr,
839 uint64_t pc,
840 const char **name)
Sean Callanan95e5c632012-02-17 00:53:45 +0000841{
Greg Claytonba812f42012-05-10 02:52:23 +0000842 if (*type_ptr)
843 {
844 if (m_exe_ctx && m_inst)
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000845 {
Greg Claytonba812f42012-05-10 02:52:23 +0000846 //std::string remove_this_prior_to_checkin;
Greg Claytonba812f42012-05-10 02:52:23 +0000847 Target *target = m_exe_ctx ? m_exe_ctx->GetTargetPtr() : NULL;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000848 Address value_so_addr;
Jason Molendac980fa92015-02-13 23:24:21 +0000849 Address pc_so_addr;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000850 if (m_inst->UsingFileAddress())
Greg Claytonba812f42012-05-10 02:52:23 +0000851 {
852 ModuleSP module_sp(m_inst->GetAddress().GetModule());
853 if (module_sp)
Jason Molendac980fa92015-02-13 23:24:21 +0000854 {
Greg Clayton3faf47c2013-03-28 23:42:53 +0000855 module_sp->ResolveFileAddress(value, value_so_addr);
Jason Molendac980fa92015-02-13 23:24:21 +0000856 module_sp->ResolveFileAddress(pc, pc_so_addr);
857 }
Greg Claytonba812f42012-05-10 02:52:23 +0000858 }
Greg Clayton3faf47c2013-03-28 23:42:53 +0000859 else if (target && !target->GetSectionLoadList().IsEmpty())
860 {
861 target->GetSectionLoadList().ResolveLoadAddress(value, value_so_addr);
Jason Molendac980fa92015-02-13 23:24:21 +0000862 target->GetSectionLoadList().ResolveLoadAddress(pc, pc_so_addr);
863 }
864
865 SymbolContext sym_ctx;
866 const uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
867 if (pc_so_addr.IsValid() && pc_so_addr.GetModule())
868 {
869 pc_so_addr.GetModule()->ResolveSymbolContextForAddress (pc_so_addr, resolve_scope, sym_ctx);
Greg Clayton3faf47c2013-03-28 23:42:53 +0000870 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000871
Greg Clayton3faf47c2013-03-28 23:42:53 +0000872 if (value_so_addr.IsValid() && value_so_addr.GetSection())
Sean Callanan95e5c632012-02-17 00:53:45 +0000873 {
Sean Callanan95e5c632012-02-17 00:53:45 +0000874 StreamString ss;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000875
Jason Molendac980fa92015-02-13 23:24:21 +0000876 bool format_omitting_current_func_name = false;
877 if (sym_ctx.symbol || sym_ctx.function)
878 {
879 AddressRange range;
880 if (sym_ctx.GetAddressRange (resolve_scope, 0, false, range)
881 && range.GetBaseAddress().IsValid()
882 && range.ContainsLoadAddress (value_so_addr, target))
883 {
884 format_omitting_current_func_name = true;
885 }
886 }
887
888 // If the "value" address (the target address we're symbolicating)
889 // is inside the same SymbolContext as the current instruction pc
890 // (pc_so_addr), don't print the full function name - just print it
891 // with DumpStyleNoFunctionName style, e.g. "<+36>".
892 if (format_omitting_current_func_name)
893 {
894 value_so_addr.Dump (&ss,
895 target,
896 Address::DumpStyleNoFunctionName,
897 Address::DumpStyleSectionNameOffset);
898 }
899 else
900 {
901 value_so_addr.Dump (&ss,
902 target,
903 Address::DumpStyleResolvedDescriptionNoFunctionArguments,
904 Address::DumpStyleSectionNameOffset);
905 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000906
Sean Callanan745af462012-03-22 20:04:23 +0000907 if (!ss.GetString().empty())
Greg Claytonba812f42012-05-10 02:52:23 +0000908 {
Jason Molendaaff1b352014-10-10 23:07:36 +0000909 // If Address::Dump returned a multi-line description, most commonly seen when we
910 // have multiple levels of inlined functions at an address, only show the first line.
911 std::string &str(ss.GetString());
912 size_t first_eol_char = str.find_first_of ("\r\n");
913 if (first_eol_char != std::string::npos)
914 {
915 str.erase (first_eol_char);
916 }
Greg Claytonba812f42012-05-10 02:52:23 +0000917 m_inst->AppendComment(ss.GetString());
918 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000919 }
920 }
921 }
Greg Claytonba812f42012-05-10 02:52:23 +0000922
923 *type_ptr = LLVMDisassembler_ReferenceType_InOut_None;
924 *name = NULL;
925 return NULL;
Sean Callanan95e5c632012-02-17 00:53:45 +0000926}
927
928//------------------------------------------------------------------
929// PluginInterface protocol
930//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000931ConstString
Sean Callanan95e5c632012-02-17 00:53:45 +0000932DisassemblerLLVMC::GetPluginName()
933{
Sean Callanan95e5c632012-02-17 00:53:45 +0000934 return GetPluginNameStatic();
935}
936
937uint32_t
938DisassemblerLLVMC::GetPluginVersion()
939{
940 return 1;
941}