blob: 5c279c91613c0801e75b77069bbf35cac1be895e [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"
16#include "llvm/MC/MCInst.h"
17#include "llvm/MC/MCInstPrinter.h"
18#include "llvm/MC/MCInstrInfo.h"
19#include "llvm/MC/MCRegisterInfo.h"
Ashok Thirumurthi78059352013-05-24 15:55:54 +000020#include "llvm/MC/MCRelocationInfo.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000021#include "llvm/MC/MCSubtargetInfo.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/MemoryObject.h"
24#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,
50 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 }
59
60 virtual
61 ~InstructionLLVMC ()
62 {
63 }
64
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 }
101
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 ();
110
111 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 }
119
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();
Sean Callanan95e5c632012-02-17 00:53:45 +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);
Greg Claytonba812f42012-05-10 02:52:23 +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;
206
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 }
224
225 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 }
236
237 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];
246
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;
Greg Claytonba812f42012-05-10 02:52:23 +0000250
251 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();
Greg Claytonba812f42012-05-10 02:52:23 +0000255
Greg Clayton3faf47c2013-03-28 23:42:53 +0000256 lldb::addr_t pc = m_address.GetFileAddress();
257 m_using_file_addr = true;
Greg Claytonba812f42012-05-10 02:52:23 +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 }
282
Greg Clayton3faf47c2013-03-28 23:42:53 +0000283 llvm_disasm.Lock(this, exe_ctx);
Greg Claytonba812f42012-05-10 02:52:23 +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();
Greg Claytonba812f42012-05-10 02:52:23 +0000300
301 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 }
373
Greg Claytondf96dd72013-08-27 19:53:47 +0000374 static RegularExpression s_regex("[ \t]*([^ ^\t]+)[ \t]*([^ ^\t].*)?", REG_EXTENDED);
Greg Claytonba812f42012-05-10 02:52:23 +0000375
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000376 RegularExpression::Match matches(3);
Greg Claytonba812f42012-05-10 02:52:23 +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 }
385
386 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 }
391
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 }
Greg Clayton3faf47c2013-03-28 23:42:53 +0000402
403 DisassemblerLLVMC &
404 GetDisassemblerLLVMC ()
405 {
406 return *(DisassemblerLLVMC *)m_disasm_sp.get();
407 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000408protected:
Sean Callanan95e5c632012-02-17 00:53:45 +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
Jim Ingham0f063ba2013-03-02 00:26:47 +0000418DisassemblerLLVMC::LLVMCDisassembler::LLVMCDisassembler (const char *triple, unsigned flavor, DisassemblerLLVMC &owner):
419 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 }
428
429 m_instr_info_ap.reset(curr_target->createMCInstrInfo());
430 m_reg_info_ap.reset (curr_target->createMCRegInfo(triple));
431
432 std::string features_str;
433
434 m_subtarget_info_ap.reset(curr_target->createMCSubtargetInfo(triple, "",
435 features_str));
436
Jean-Daniel Dupasc6f26f82013-12-29 20:17:26 +0000437 std::unique_ptr<llvm::MCRegisterInfo> reg_info(curr_target->createMCRegInfo(triple));
438 m_asm_info_ap.reset(curr_target->createMCAsmInfo(*reg_info, triple));
Sylvestre Ledru7a89d6f2013-05-13 13:41:13 +0000439
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 {
Ahmed Charles8f926ad2014-03-07 04:45:22 +0000451 std::unique_ptr<llvm::MCRelocationInfo> RelInfo(curr_target->createMCRelocationInfo(triple, *m_context_ap.get()));
Ashok Thirumurthi78059352013-05-24 15:55:54 +0000452 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