blob: 5bc782c19db720ee59a8af5acf3c34590f75070f [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
Eugene Zelenko45a40142015-10-22 21:24:37 +000010// C Includes
11// C++ Includes
12// Project includes
Sean Callanan95e5c632012-02-17 00:53:45 +000013#include "llvm-c/Disassembler.h"
Benjamin Kramer79dad1d2016-01-26 16:45:00 +000014#include "llvm/ADT/SmallString.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000015#include "llvm/MC/MCAsmInfo.h"
16#include "llvm/MC/MCContext.h"
Benjamin Kramer79dad1d2016-01-26 16:45:00 +000017#include "llvm/MC/MCDisassembler/MCDisassembler.h"
18#include "llvm/MC/MCDisassembler/MCExternalSymbolizer.h"
19#include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000020#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCInstPrinter.h"
22#include "llvm/MC/MCInstrInfo.h"
23#include "llvm/MC/MCRegisterInfo.h"
24#include "llvm/MC/MCSubtargetInfo.h"
25#include "llvm/Support/ErrorHandling.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000026#include "llvm/Support/TargetRegistry.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000027#include "llvm/Support/TargetSelect.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000028
Eugene Zelenko45a40142015-10-22 21:24:37 +000029// Other libraries and framework includes
30#include "DisassemblerLLVMC.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000031
32#include "lldb/Core/Address.h"
33#include "lldb/Core/DataExtractor.h"
Greg Clayton1f746072012-08-29 21:13:06 +000034#include "lldb/Core/Module.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000035#include "lldb/Core/Stream.h"
36#include "lldb/Symbol/SymbolContext.h"
37#include "lldb/Target/ExecutionContext.h"
38#include "lldb/Target/Process.h"
39#include "lldb/Target/RegisterContext.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000040#include "lldb/Target/SectionLoadList.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000041#include "lldb/Target/Target.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000042#include "lldb/Target/StackFrame.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000043
Virgile Bellob2f1fb22013-08-23 12:44:05 +000044#include "lldb/Core/RegularExpression.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000045
46using namespace lldb;
47using namespace lldb_private;
48
49class InstructionLLVMC : public lldb_private::Instruction
50{
51public:
52 InstructionLLVMC (DisassemblerLLVMC &disasm,
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000053 const lldb_private::Address &address,
Greg Claytonc8e0c242012-04-13 00:07:34 +000054 AddressClass addr_class) :
Greg Clayton3faf47c2013-03-28 23:42:53 +000055 Instruction (address, addr_class),
56 m_disasm_sp (disasm.shared_from_this()),
57 m_does_branch (eLazyBoolCalculate),
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +000058 m_has_delay_slot (eLazyBoolCalculate),
Greg Clayton3faf47c2013-03-28 23:42:53 +000059 m_is_valid (false),
60 m_using_file_addr (false)
Sean Callanan95e5c632012-02-17 00:53:45 +000061 {
62 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000063
Eugene Zelenko45a40142015-10-22 21:24:37 +000064 ~InstructionLLVMC() override = default;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000065
Eugene Zelenko45a40142015-10-22 21:24:37 +000066 bool
67 DoesBranch() override
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 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000102
Eugene Zelenko45a40142015-10-22 21:24:37 +0000103 bool
104 HasDelaySlot() override
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +0000105 {
106 if (m_has_delay_slot == eLazyBoolCalculate)
107 {
108 GetDisassemblerLLVMC().Lock(this, NULL);
109 DataExtractor data;
110 if (m_opcode.GetData(data))
111 {
112 bool is_alternate_isa;
113 lldb::addr_t pc = m_address.GetFileAddress();
114
115 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr = GetDisasmToUse (is_alternate_isa);
116 const uint8_t *opcode_data = data.GetDataStart();
117 const size_t opcode_data_len = data.GetByteSize();
118 llvm::MCInst inst;
119 const size_t inst_size = mc_disasm_ptr->GetMCInst (opcode_data,
120 opcode_data_len,
121 pc,
122 inst);
123 // if we didn't understand the instruction, say it doesn't have a delay slot...
124 if (inst_size == 0)
125 m_has_delay_slot = eLazyBoolNo;
126 else
127 {
128 const bool has_delay_slot = mc_disasm_ptr->HasDelaySlot(inst);
129 if (has_delay_slot)
130 m_has_delay_slot = eLazyBoolYes;
131 else
132 m_has_delay_slot = eLazyBoolNo;
133 }
134 }
135 GetDisassemblerLLVMC().Unlock();
136 }
137 return m_has_delay_slot == eLazyBoolYes;
138 }
139
Jim Ingham32ce20c2013-03-13 01:55:16 +0000140 DisassemblerLLVMC::LLVMCDisassembler *
141 GetDisasmToUse (bool &is_alternate_isa)
142 {
Jim Ingham32ce20c2013-03-13 01:55:16 +0000143 is_alternate_isa = false;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000144 DisassemblerLLVMC &llvm_disasm = GetDisassemblerLLVMC();
145 if (llvm_disasm.m_alternate_disasm_ap.get() != NULL)
Jim Ingham32ce20c2013-03-13 01:55:16 +0000146 {
147 const AddressClass address_class = GetAddressClass ();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000148
Jim Ingham32ce20c2013-03-13 01:55:16 +0000149 if (address_class == eAddressClassCodeAlternateISA)
150 {
Jim Ingham32ce20c2013-03-13 01:55:16 +0000151 is_alternate_isa = true;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000152 return llvm_disasm.m_alternate_disasm_ap.get();
Jim Ingham32ce20c2013-03-13 01:55:16 +0000153 }
154 }
Greg Clayton3faf47c2013-03-28 23:42:53 +0000155 return llvm_disasm.m_disasm_ap.get();
Jim Ingham32ce20c2013-03-13 01:55:16 +0000156 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000157
Eugene Zelenko45a40142015-10-22 21:24:37 +0000158 size_t
159 Decode(const lldb_private::Disassembler &disassembler,
160 const lldb_private::DataExtractor &data,
161 lldb::offset_t data_offset) override
Sean Callanan95e5c632012-02-17 00:53:45 +0000162 {
Greg Claytonba812f42012-05-10 02:52:23 +0000163 // All we have to do is read the opcode which can be easy for some
Michael Sartaincc791bb2013-07-11 16:40:56 +0000164 // architectures
Greg Claytonba812f42012-05-10 02:52:23 +0000165 bool got_op = false;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000166 DisassemblerLLVMC &llvm_disasm = GetDisassemblerLLVMC();
167 const ArchSpec &arch = llvm_disasm.GetArchitecture();
Ed Maste90359962013-12-09 19:45:33 +0000168 const lldb::ByteOrder byte_order = data.GetByteOrder();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000169
Greg Claytonba812f42012-05-10 02:52:23 +0000170 const uint32_t min_op_byte_size = arch.GetMinimumOpcodeByteSize();
171 const uint32_t max_op_byte_size = arch.GetMaximumOpcodeByteSize();
172 if (min_op_byte_size == max_op_byte_size)
173 {
174 // Fixed size instructions, just read that amount of data.
175 if (!data.ValidOffsetForDataOfSize(data_offset, min_op_byte_size))
176 return false;
Ed Maste90359962013-12-09 19:45:33 +0000177
Greg Claytonba812f42012-05-10 02:52:23 +0000178 switch (min_op_byte_size)
179 {
180 case 1:
Ed Maste90359962013-12-09 19:45:33 +0000181 m_opcode.SetOpcode8 (data.GetU8 (&data_offset), byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000182 got_op = true;
183 break;
184
185 case 2:
Ed Maste90359962013-12-09 19:45:33 +0000186 m_opcode.SetOpcode16 (data.GetU16 (&data_offset), byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000187 got_op = true;
188 break;
189
190 case 4:
Ed Maste90359962013-12-09 19:45:33 +0000191 m_opcode.SetOpcode32 (data.GetU32 (&data_offset), byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000192 got_op = true;
193 break;
194
195 case 8:
Ed Maste90359962013-12-09 19:45:33 +0000196 m_opcode.SetOpcode64 (data.GetU64 (&data_offset), byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000197 got_op = true;
198 break;
199
200 default:
201 m_opcode.SetOpcodeBytes(data.PeekData(data_offset, min_op_byte_size), min_op_byte_size);
202 got_op = true;
203 break;
204 }
205 }
206 if (!got_op)
207 {
Jim Ingham32ce20c2013-03-13 01:55:16 +0000208 bool is_alternate_isa = false;
209 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr = GetDisasmToUse (is_alternate_isa);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000210
Greg Claytonba812f42012-05-10 02:52:23 +0000211 const llvm::Triple::ArchType machine = arch.GetMachine();
212 if (machine == llvm::Triple::arm || machine == llvm::Triple::thumb)
213 {
Jim Ingham32ce20c2013-03-13 01:55:16 +0000214 if (machine == llvm::Triple::thumb || is_alternate_isa)
Greg Claytonba812f42012-05-10 02:52:23 +0000215 {
Greg Clayton79101b52012-08-07 01:29:29 +0000216 uint32_t thumb_opcode = data.GetU16(&data_offset);
Greg Claytonba812f42012-05-10 02:52:23 +0000217 if ((thumb_opcode & 0xe000) != 0xe000 || ((thumb_opcode & 0x1800u) == 0))
218 {
Ed Maste90359962013-12-09 19:45:33 +0000219 m_opcode.SetOpcode16 (thumb_opcode, byte_order);
Sean Callanan5c97c2f2012-08-06 23:42:52 +0000220 m_is_valid = true;
Greg Claytonba812f42012-05-10 02:52:23 +0000221 }
222 else
223 {
Greg Clayton79101b52012-08-07 01:29:29 +0000224 thumb_opcode <<= 16;
225 thumb_opcode |= data.GetU16(&data_offset);
Ed Maste90359962013-12-09 19:45:33 +0000226 m_opcode.SetOpcode16_2 (thumb_opcode, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000227 m_is_valid = true;
228 }
229 }
230 else
231 {
Ed Maste90359962013-12-09 19:45:33 +0000232 m_opcode.SetOpcode32 (data.GetU32(&data_offset), byte_order);
Sean Callanan5c97c2f2012-08-06 23:42:52 +0000233 m_is_valid = true;
Greg Claytonba812f42012-05-10 02:52:23 +0000234 }
235 }
236 else
237 {
238 // The opcode isn't evenly sized, so we need to actually use the llvm
239 // disassembler to parse it and get the size.
Greg Claytonba812f42012-05-10 02:52:23 +0000240 uint8_t *opcode_data = const_cast<uint8_t *>(data.PeekData (data_offset, 1));
Greg Clayton3faf47c2013-03-28 23:42:53 +0000241 const size_t opcode_data_len = data.BytesLeft(data_offset);
Greg Claytonba812f42012-05-10 02:52:23 +0000242 const addr_t pc = m_address.GetFileAddress();
Jim Ingham0f063ba2013-03-02 00:26:47 +0000243 llvm::MCInst inst;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000244
Greg Clayton3faf47c2013-03-28 23:42:53 +0000245 llvm_disasm.Lock(this, NULL);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000246 const size_t inst_size = mc_disasm_ptr->GetMCInst(opcode_data,
Greg Clayton3faf47c2013-03-28 23:42:53 +0000247 opcode_data_len,
248 pc,
249 inst);
250 llvm_disasm.Unlock();
Greg Claytonba812f42012-05-10 02:52:23 +0000251 if (inst_size == 0)
252 m_opcode.Clear();
253 else
254 {
255 m_opcode.SetOpcodeBytes(opcode_data, inst_size);
256 m_is_valid = true;
257 }
258 }
259 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000260 return m_opcode.GetByteSize();
261 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000262
Sean Callanan95e5c632012-02-17 00:53:45 +0000263 void
Greg Claytonba812f42012-05-10 02:52:23 +0000264 AppendComment (std::string &description)
Sean Callanan95e5c632012-02-17 00:53:45 +0000265 {
Greg Claytonba812f42012-05-10 02:52:23 +0000266 if (m_comment.empty())
267 m_comment.swap (description);
Sean Callanan95e5c632012-02-17 00:53:45 +0000268 else
Greg Claytonba812f42012-05-10 02:52:23 +0000269 {
270 m_comment.append(", ");
271 m_comment.append(description);
272 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000273 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000274
Eugene Zelenko45a40142015-10-22 21:24:37 +0000275 void
276 CalculateMnemonicOperandsAndComment(const lldb_private::ExecutionContext *exe_ctx) override
Sean Callanan95e5c632012-02-17 00:53:45 +0000277 {
Greg Claytonba812f42012-05-10 02:52:23 +0000278 DataExtractor data;
279 const AddressClass address_class = GetAddressClass ();
280
Sean Callanancd4ae1a2012-08-07 01:44:58 +0000281 if (m_opcode.GetData(data))
Greg Claytonba812f42012-05-10 02:52:23 +0000282 {
Sean Callanand38f4d22015-12-11 19:10:04 +0000283 std::string out_string;
284 std::string comment_string;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000285
Greg Clayton3faf47c2013-03-28 23:42:53 +0000286 DisassemblerLLVMC &llvm_disasm = GetDisassemblerLLVMC();
287
Jim Ingham0f063ba2013-03-02 00:26:47 +0000288 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000289
Greg Claytonba812f42012-05-10 02:52:23 +0000290 if (address_class == eAddressClassCodeAlternateISA)
Greg Clayton3faf47c2013-03-28 23:42:53 +0000291 mc_disasm_ptr = llvm_disasm.m_alternate_disasm_ap.get();
Greg Claytonba812f42012-05-10 02:52:23 +0000292 else
Greg Clayton3faf47c2013-03-28 23:42:53 +0000293 mc_disasm_ptr = llvm_disasm.m_disasm_ap.get();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000294
Greg Clayton3faf47c2013-03-28 23:42:53 +0000295 lldb::addr_t pc = m_address.GetFileAddress();
296 m_using_file_addr = true;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000297
Greg Clayton3faf47c2013-03-28 23:42:53 +0000298 const bool data_from_file = GetDisassemblerLLVMC().m_data_from_file;
Daniel Malead79ae052013-08-07 21:54:09 +0000299 bool use_hex_immediates = true;
300 Disassembler::HexImmediateStyle hex_style = Disassembler::eHexStyleC;
301
302 if (exe_ctx)
Greg Claytonba812f42012-05-10 02:52:23 +0000303 {
Daniel Malead79ae052013-08-07 21:54:09 +0000304 Target *target = exe_ctx->GetTargetPtr();
305 if (target)
Greg Clayton3faf47c2013-03-28 23:42:53 +0000306 {
Daniel Malead79ae052013-08-07 21:54:09 +0000307 use_hex_immediates = target->GetUseHexImmediates();
308 hex_style = target->GetHexImmediateStyle();
309
310 if (!data_from_file)
Greg Clayton3faf47c2013-03-28 23:42:53 +0000311 {
312 const lldb::addr_t load_addr = m_address.GetLoadAddress(target);
313 if (load_addr != LLDB_INVALID_ADDRESS)
314 {
315 pc = load_addr;
316 m_using_file_addr = false;
317 }
318 }
319 }
Greg Claytonba812f42012-05-10 02:52:23 +0000320 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000321
Greg Clayton3faf47c2013-03-28 23:42:53 +0000322 llvm_disasm.Lock(this, exe_ctx);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000323
Greg Clayton3faf47c2013-03-28 23:42:53 +0000324 const uint8_t *opcode_data = data.GetDataStart();
Greg Claytonba812f42012-05-10 02:52:23 +0000325 const size_t opcode_data_len = data.GetByteSize();
Jim Ingham0f063ba2013-03-02 00:26:47 +0000326 llvm::MCInst inst;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000327 size_t inst_size = mc_disasm_ptr->GetMCInst (opcode_data,
328 opcode_data_len,
329 pc,
330 inst);
Daniel Malead79ae052013-08-07 21:54:09 +0000331
Jim Ingham0f063ba2013-03-02 00:26:47 +0000332 if (inst_size > 0)
Daniel Malead79ae052013-08-07 21:54:09 +0000333 {
334 mc_disasm_ptr->SetStyle(use_hex_immediates, hex_style);
Sean Callanand38f4d22015-12-11 19:10:04 +0000335 mc_disasm_ptr->PrintMCInst(inst, out_string, comment_string);
336
337 if (!comment_string.empty())
338 {
339 AppendComment(comment_string);
340 }
Daniel Malead79ae052013-08-07 21:54:09 +0000341 }
342
Greg Clayton3faf47c2013-03-28 23:42:53 +0000343 llvm_disasm.Unlock();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000344
Greg Claytonba812f42012-05-10 02:52:23 +0000345 if (inst_size == 0)
346 {
347 m_comment.assign ("unknown opcode");
348 inst_size = m_opcode.GetByteSize();
349 StreamString mnemonic_strm;
Greg Claytonc7bece562013-01-25 18:06:21 +0000350 lldb::offset_t offset = 0;
Ed Maste90359962013-12-09 19:45:33 +0000351 lldb::ByteOrder byte_order = data.GetByteOrder();
Greg Claytonba812f42012-05-10 02:52:23 +0000352 switch (inst_size)
353 {
354 case 1:
355 {
356 const uint8_t uval8 = data.GetU8 (&offset);
Ed Maste90359962013-12-09 19:45:33 +0000357 m_opcode.SetOpcode8 (uval8, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000358 m_opcode_name.assign (".byte");
359 mnemonic_strm.Printf("0x%2.2x", uval8);
360 }
361 break;
362 case 2:
363 {
364 const uint16_t uval16 = data.GetU16(&offset);
Ed Maste90359962013-12-09 19:45:33 +0000365 m_opcode.SetOpcode16(uval16, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000366 m_opcode_name.assign (".short");
367 mnemonic_strm.Printf("0x%4.4x", uval16);
368 }
369 break;
370 case 4:
371 {
372 const uint32_t uval32 = data.GetU32(&offset);
Ed Maste90359962013-12-09 19:45:33 +0000373 m_opcode.SetOpcode32(uval32, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000374 m_opcode_name.assign (".long");
375 mnemonic_strm.Printf("0x%8.8x", uval32);
376 }
377 break;
378 case 8:
379 {
380 const uint64_t uval64 = data.GetU64(&offset);
Ed Maste90359962013-12-09 19:45:33 +0000381 m_opcode.SetOpcode64(uval64, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000382 m_opcode_name.assign (".quad");
Daniel Malead01b2952012-11-29 21:49:15 +0000383 mnemonic_strm.Printf("0x%16.16" PRIx64, uval64);
Greg Claytonba812f42012-05-10 02:52:23 +0000384 }
385 break;
386 default:
387 if (inst_size == 0)
388 return;
389 else
390 {
391 const uint8_t *bytes = data.PeekData(offset, inst_size);
392 if (bytes == NULL)
393 return;
394 m_opcode_name.assign (".byte");
395 m_opcode.SetOpcodeBytes(bytes, inst_size);
396 mnemonic_strm.Printf("0x%2.2x", bytes[0]);
397 for (uint32_t i=1; i<inst_size; ++i)
398 mnemonic_strm.Printf(" 0x%2.2x", bytes[i]);
399 }
400 break;
401 }
Jim Ingham0f063ba2013-03-02 00:26:47 +0000402 m_mnemonics.swap(mnemonic_strm.GetString());
Greg Claytonba812f42012-05-10 02:52:23 +0000403 return;
404 }
405 else
406 {
407 if (m_does_branch == eLazyBoolCalculate)
408 {
Greg Clayton3faf47c2013-03-28 23:42:53 +0000409 const bool can_branch = mc_disasm_ptr->CanBranch(inst);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000410 if (can_branch)
Greg Claytonba812f42012-05-10 02:52:23 +0000411 m_does_branch = eLazyBoolYes;
412 else
413 m_does_branch = eLazyBoolNo;
Jim Ingham0f063ba2013-03-02 00:26:47 +0000414
Greg Claytonba812f42012-05-10 02:52:23 +0000415 }
416 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000417
Greg Clayton7bd4c602015-01-21 21:51:02 +0000418 static RegularExpression s_regex("[ \t]*([^ ^\t]+)[ \t]*([^ ^\t].*)?");
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000419
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000420 RegularExpression::Match matches(3);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000421
Sean Callanand38f4d22015-12-11 19:10:04 +0000422 if (s_regex.Execute(out_string.c_str(), &matches))
Greg Claytonba812f42012-05-10 02:52:23 +0000423 {
Sean Callanand38f4d22015-12-11 19:10:04 +0000424 matches.GetMatchAtIndex(out_string.c_str(), 1, m_opcode_name);
425 matches.GetMatchAtIndex(out_string.c_str(), 2, m_mnemonics);
Greg Claytonba812f42012-05-10 02:52:23 +0000426 }
427 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000428 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000429
Sean Callanan95e5c632012-02-17 00:53:45 +0000430 bool
Greg Clayton3faf47c2013-03-28 23:42:53 +0000431 IsValid () const
Sean Callanan95e5c632012-02-17 00:53:45 +0000432 {
433 return m_is_valid;
434 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000435
Greg Clayton3faf47c2013-03-28 23:42:53 +0000436 bool
437 UsingFileAddress() const
438 {
439 return m_using_file_addr;
440 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000441 size_t
Greg Clayton3faf47c2013-03-28 23:42:53 +0000442 GetByteSize () const
Sean Callanan95e5c632012-02-17 00:53:45 +0000443 {
444 return m_opcode.GetByteSize();
445 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000446
Greg Clayton3faf47c2013-03-28 23:42:53 +0000447 DisassemblerLLVMC &
448 GetDisassemblerLLVMC ()
449 {
450 return *(DisassemblerLLVMC *)m_disasm_sp.get();
451 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000452protected:
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000453
Sean Callanan7e6d4e52012-08-01 18:50:59 +0000454 DisassemblerSP m_disasm_sp; // for ownership
Sean Callanan7725a462012-03-02 23:22:53 +0000455 LazyBool m_does_branch;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +0000456 LazyBool m_has_delay_slot;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000457 bool m_is_valid;
458 bool m_using_file_addr;
Sean Callanan95e5c632012-02-17 00:53:45 +0000459};
460
Jaydeep Patil501a7812015-07-16 03:51:55 +0000461DisassemblerLLVMC::LLVMCDisassembler::LLVMCDisassembler (const char *triple, const char *cpu, const char *features_str, unsigned flavor, DisassemblerLLVMC &owner):
Jim Ingham0f063ba2013-03-02 00:26:47 +0000462 m_is_valid(true)
463{
464 std::string Error;
465 const llvm::Target *curr_target = llvm::TargetRegistry::lookupTarget(triple, Error);
466 if (!curr_target)
467 {
468 m_is_valid = false;
469 return;
470 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000471
Jim Ingham0f063ba2013-03-02 00:26:47 +0000472 m_instr_info_ap.reset(curr_target->createMCInstrInfo());
473 m_reg_info_ap.reset (curr_target->createMCRegInfo(triple));
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000474
Mohit K. Bhakkad276a9302015-06-18 06:03:27 +0000475 m_subtarget_info_ap.reset(curr_target->createMCSubtargetInfo(triple, cpu,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000476 features_str));
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000477
Jean-Daniel Dupasc6f26f82013-12-29 20:17:26 +0000478 std::unique_ptr<llvm::MCRegisterInfo> reg_info(curr_target->createMCRegInfo(triple));
479 m_asm_info_ap.reset(curr_target->createMCAsmInfo(*reg_info, triple));
Sylvestre Ledru7a89d6f2013-05-13 13:41:13 +0000480
Jim Ingham0f063ba2013-03-02 00:26:47 +0000481 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)
482 {
Matt Kopec787d1622013-03-12 17:45:38 +0000483 m_is_valid = false;
Jim Ingham0f063ba2013-03-02 00:26:47 +0000484 return;
485 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000486
Bill Wendlingd63d0a82013-06-18 07:50:43 +0000487 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 +0000488
Sylvestre Ledru5ac35ae2014-04-15 12:07:25 +0000489 m_disasm_ap.reset(curr_target->createMCDisassembler(*m_subtarget_info_ap.get(), *m_context_ap.get()));
Ashok Thirumurthi78059352013-05-24 15:55:54 +0000490 if (m_disasm_ap.get() && m_context_ap.get())
Jim Ingham0f063ba2013-03-02 00:26:47 +0000491 {
Ahmed Charles8f926ad2014-03-07 04:45:22 +0000492 std::unique_ptr<llvm::MCRelocationInfo> RelInfo(curr_target->createMCRelocationInfo(triple, *m_context_ap.get()));
Ashok Thirumurthi78059352013-05-24 15:55:54 +0000493 if (!RelInfo)
494 {
495 m_is_valid = false;
496 return;
497 }
Jason Molenda64a68d62014-05-17 00:27:44 +0000498 std::unique_ptr<llvm::MCSymbolizer> symbolizer_up(curr_target->createMCSymbolizer(triple, NULL,
499 DisassemblerLLVMC::SymbolLookupCallback,
500 (void *) &owner,
Chandler Carruth215d9392015-01-19 03:07:25 +0000501 m_context_ap.get(), std::move(RelInfo)));
Jason Molenda64a68d62014-05-17 00:27:44 +0000502 m_disasm_ap->setSymbolizer(std::move(symbolizer_up));
Greg Clayton3434b572014-04-14 21:33:38 +0000503
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000504
Jim Ingham0f063ba2013-03-02 00:26:47 +0000505 unsigned asm_printer_variant;
506 if (flavor == ~0U)
507 asm_printer_variant = m_asm_info_ap->getAssemblerDialect();
508 else
509 {
510 asm_printer_variant = flavor;
511 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000512
Daniel Sanders32764b52015-09-15 16:33:17 +0000513 m_instr_printer_ap.reset(curr_target->createMCInstPrinter(llvm::Triple{triple},
Chaoren Lin2d6105c2015-03-31 00:59:13 +0000514 asm_printer_variant,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000515 *m_asm_info_ap.get(),
516 *m_instr_info_ap.get(),
Chaoren Lin2d6105c2015-03-31 00:59:13 +0000517 *m_reg_info_ap.get()));
Jim Ingham0f063ba2013-03-02 00:26:47 +0000518 if (m_instr_printer_ap.get() == NULL)
519 {
520 m_disasm_ap.reset();
521 m_is_valid = false;
522 }
523 }
524 else
525 m_is_valid = false;
526}
527
Eugene Zelenko45a40142015-10-22 21:24:37 +0000528DisassemblerLLVMC::LLVMCDisassembler::~LLVMCDisassembler() = default;
Eugene Zelenko8dd3fdb2015-10-21 01:42:15 +0000529
Jim Ingham0f063ba2013-03-02 00:26:47 +0000530uint64_t
Greg Clayton3faf47c2013-03-28 23:42:53 +0000531DisassemblerLLVMC::LLVMCDisassembler::GetMCInst (const uint8_t *opcode_data,
532 size_t opcode_data_len,
533 lldb::addr_t pc,
534 llvm::MCInst &mc_inst)
Jim Ingham0f063ba2013-03-02 00:26:47 +0000535{
Rafael Espindola434df0a2014-11-12 02:04:31 +0000536 llvm::ArrayRef<uint8_t> data(opcode_data, opcode_data_len);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000537 llvm::MCDisassembler::DecodeStatus status;
538
539 uint64_t new_inst_size;
540 status = m_disasm_ap->getInstruction(mc_inst,
541 new_inst_size,
Rafael Espindola434df0a2014-11-12 02:04:31 +0000542 data,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000543 pc,
544 llvm::nulls(),
545 llvm::nulls());
546 if (status == llvm::MCDisassembler::Success)
547 return new_inst_size;
548 else
549 return 0;
550}
551
Sean Callanand38f4d22015-12-11 19:10:04 +0000552void
Greg Clayton3faf47c2013-03-28 23:42:53 +0000553DisassemblerLLVMC::LLVMCDisassembler::PrintMCInst (llvm::MCInst &mc_inst,
Sean Callanand38f4d22015-12-11 19:10:04 +0000554 std::string &inst_string,
555 std::string &comments_string)
Jim Ingham0f063ba2013-03-02 00:26:47 +0000556{
Sean Callanand38f4d22015-12-11 19:10:04 +0000557 llvm::raw_string_ostream inst_stream(inst_string);
558 llvm::raw_string_ostream comments_stream(comments_string);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000559
Sean Callanand38f4d22015-12-11 19:10:04 +0000560 m_instr_printer_ap->setCommentStream(comments_stream);
561 m_instr_printer_ap->printInst (&mc_inst, inst_stream, llvm::StringRef(), *m_subtarget_info_ap);
562 m_instr_printer_ap->setCommentStream(llvm::nulls());
563 comments_stream.flush();
564
565 static std::string g_newlines("\r\n");
566
567 for (size_t newline_pos = 0; (newline_pos = comments_string.find_first_of(g_newlines, newline_pos)) != comments_string.npos; /**/)
568 {
569 comments_string.replace(comments_string.begin() + newline_pos, comments_string.begin() + newline_pos + 1, 1, ' ');
570 }
Jim Ingham0f063ba2013-03-02 00:26:47 +0000571}
572
Daniel Malead79ae052013-08-07 21:54:09 +0000573void
574DisassemblerLLVMC::LLVMCDisassembler::SetStyle (bool use_hex_immed, HexImmediateStyle hex_style)
575{
576 m_instr_printer_ap->setPrintImmHex(use_hex_immed);
577 switch(hex_style)
578 {
Oleksiy Vyalov37ff6c42015-06-08 17:10:27 +0000579 case eHexStyleC: m_instr_printer_ap->setPrintHexStyle(llvm::HexStyle::C); break;
580 case eHexStyleAsm: m_instr_printer_ap->setPrintHexStyle(llvm::HexStyle::Asm); break;
Daniel Malead79ae052013-08-07 21:54:09 +0000581 }
582}
583
Jim Ingham0f063ba2013-03-02 00:26:47 +0000584bool
585DisassemblerLLVMC::LLVMCDisassembler::CanBranch (llvm::MCInst &mc_inst)
586{
587 return m_instr_info_ap->get(mc_inst.getOpcode()).mayAffectControlFlow(mc_inst, *m_reg_info_ap.get());
588}
589
590bool
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +0000591DisassemblerLLVMC::LLVMCDisassembler::HasDelaySlot (llvm::MCInst &mc_inst)
592{
593 return m_instr_info_ap->get(mc_inst.getOpcode()).hasDelaySlot();
594}
595
Jim Ingham0f063ba2013-03-02 00:26:47 +0000596DisassemblerLLVMC::DisassemblerLLVMC (const ArchSpec &arch, const char *flavor_string) :
597 Disassembler(arch, flavor_string),
Greg Claytonba812f42012-05-10 02:52:23 +0000598 m_exe_ctx (NULL),
Greg Clayton3faf47c2013-03-28 23:42:53 +0000599 m_inst (NULL),
600 m_data_from_file (false)
Sean Callanan95e5c632012-02-17 00:53:45 +0000601{
Jim Ingham0f063ba2013-03-02 00:26:47 +0000602 if (!FlavorValidForArchSpec (arch, m_flavor.c_str()))
603 {
604 m_flavor.assign("default");
605 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000606
Jim Ingham0f063ba2013-03-02 00:26:47 +0000607 unsigned flavor = ~0U;
Tamas Berghammerff417ef2015-09-02 13:24:50 +0000608 llvm::Triple triple = arch.GetTriple();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000609
Jim Ingham0f063ba2013-03-02 00:26:47 +0000610 // So far the only supported flavor is "intel" on x86. The base class will set this
611 // correctly coming in.
Tamas Berghammerff417ef2015-09-02 13:24:50 +0000612 if (triple.getArch() == llvm::Triple::x86 || triple.getArch() == llvm::Triple::x86_64)
Jim Ingham0f063ba2013-03-02 00:26:47 +0000613 {
614 if (m_flavor == "intel")
615 {
616 flavor = 1;
617 }
618 else if (m_flavor == "att")
619 {
620 flavor = 0;
621 }
622 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000623
Jason Molenda01aa5342013-07-22 22:11:53 +0000624 ArchSpec thumb_arch(arch);
Tamas Berghammerc17469b2015-09-02 13:31:18 +0000625 if (triple.getArch() == llvm::Triple::arm)
Sean Callanan95e5c632012-02-17 00:53:45 +0000626 {
Greg Claytona80ea122013-05-03 01:05:04 +0000627 std::string thumb_arch_name (thumb_arch.GetTriple().getArchName().str());
628 // Replace "arm" with "thumb" so we get all thumb variants correct
629 if (thumb_arch_name.size() > 3)
630 {
631 thumb_arch_name.erase(0,3);
632 thumb_arch_name.insert(0, "thumb");
633 }
634 else
635 {
Tamas Berghammer45dbfa12016-03-02 12:42:43 +0000636 thumb_arch_name = "thumbv8.2a";
Greg Claytona80ea122013-05-03 01:05:04 +0000637 }
638 thumb_arch.GetTriple().setArchName(llvm::StringRef(thumb_arch_name.c_str()));
Jason Molenda01aa5342013-07-22 22:11:53 +0000639 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000640
Tamas Berghammerff417ef2015-09-02 13:24:50 +0000641 // If no sub architecture specified then use the most recent arm architecture so the
642 // disassembler will return all instruction. Without it we will see a lot of unknow opcode
643 // in case the code uses instructions which are not available in the oldest arm version
644 // (used when no sub architecture is specified)
645 if (triple.getArch() == llvm::Triple::arm && triple.getSubArch() == llvm::Triple::NoSubArch)
Tamas Berghammer45dbfa12016-03-02 12:42:43 +0000646 triple.setArchName("armv8.2a");
Tamas Berghammerff417ef2015-09-02 13:24:50 +0000647
648 const char *triple_str = triple.getTriple().c_str();
649
Jason Molenda8825c5c2015-10-08 21:48:35 +0000650 // v. https://en.wikipedia.org/wiki/ARM_Cortex-M#Silicon_customization
651 //
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000652 // Cortex-M3 devices (e.g. armv7m) can only execute thumb (T2) instructions,
Jason Molendaa3a04522013-09-27 23:21:54 +0000653 // so hardcode the primary disassembler to thumb mode. Same for Cortex-M4 (armv7em).
654 //
655 // Handle the Cortex-M0 (armv6m) the same; the ISA is a subset of the T and T32
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000656 // instructions defined in ARMv7-A.
Jason Molendaa3a04522013-09-27 23:21:54 +0000657
Tamas Berghammerff417ef2015-09-02 13:24:50 +0000658 if ((triple.getArch() == llvm::Triple::arm || triple.getArch() == llvm::Triple::thumb)
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000659 && (arch.GetCore() == ArchSpec::Core::eCore_arm_armv7m
Jason Molendaa3a04522013-09-27 23:21:54 +0000660 || arch.GetCore() == ArchSpec::Core::eCore_arm_armv7em
661 || arch.GetCore() == ArchSpec::Core::eCore_arm_armv6m))
Jason Molenda01aa5342013-07-22 22:11:53 +0000662 {
Tamas Berghammerff417ef2015-09-02 13:24:50 +0000663 triple_str = thumb_arch.GetTriple().getTriple().c_str();
Jason Molenda01aa5342013-07-22 22:11:53 +0000664 }
665
Mohit K. Bhakkad276a9302015-06-18 06:03:27 +0000666 const char *cpu = "";
667
668 switch (arch.GetCore())
669 {
670 case ArchSpec::eCore_mips32:
671 case ArchSpec::eCore_mips32el:
672 cpu = "mips32"; break;
673 case ArchSpec::eCore_mips32r2:
674 case ArchSpec::eCore_mips32r2el:
675 cpu = "mips32r2"; break;
676 case ArchSpec::eCore_mips32r3:
677 case ArchSpec::eCore_mips32r3el:
678 cpu = "mips32r3"; break;
679 case ArchSpec::eCore_mips32r5:
680 case ArchSpec::eCore_mips32r5el:
681 cpu = "mips32r5"; break;
682 case ArchSpec::eCore_mips32r6:
683 case ArchSpec::eCore_mips32r6el:
684 cpu = "mips32r6"; break;
685 case ArchSpec::eCore_mips64:
686 case ArchSpec::eCore_mips64el:
687 cpu = "mips64"; break;
688 case ArchSpec::eCore_mips64r2:
689 case ArchSpec::eCore_mips64r2el:
690 cpu = "mips64r2"; break;
691 case ArchSpec::eCore_mips64r3:
692 case ArchSpec::eCore_mips64r3el:
693 cpu = "mips64r3"; break;
694 case ArchSpec::eCore_mips64r5:
695 case ArchSpec::eCore_mips64r5el:
696 cpu = "mips64r5"; break;
697 case ArchSpec::eCore_mips64r6:
698 case ArchSpec::eCore_mips64r6el:
699 cpu = "mips64r6"; break;
700 default:
701 cpu = ""; break;
702 }
Jaydeep Patil501a7812015-07-16 03:51:55 +0000703
704 std::string features_str = "";
Tamas Berghammerff417ef2015-09-02 13:24:50 +0000705 if (triple.getArch() == llvm::Triple::mips || triple.getArch() == llvm::Triple::mipsel
706 || triple.getArch() == llvm::Triple::mips64 || triple.getArch() == llvm::Triple::mips64el)
Jaydeep Patil501a7812015-07-16 03:51:55 +0000707 {
708 uint32_t arch_flags = arch.GetFlags ();
709 if (arch_flags & ArchSpec::eMIPSAse_msa)
710 features_str += "+msa,";
711 if (arch_flags & ArchSpec::eMIPSAse_dsp)
712 features_str += "+dsp,";
713 if (arch_flags & ArchSpec::eMIPSAse_dspr2)
714 features_str += "+dspr2,";
Jaydeep Patil501a7812015-07-16 03:51:55 +0000715 }
Mohit K. Bhakkad276a9302015-06-18 06:03:27 +0000716
Tamas Berghammerff417ef2015-09-02 13:24:50 +0000717 m_disasm_ap.reset (new LLVMCDisassembler(triple_str, cpu, features_str.c_str(), flavor, *this));
Jason Molenda01aa5342013-07-22 22:11:53 +0000718 if (!m_disasm_ap->IsValid())
719 {
720 // We use m_disasm_ap.get() to tell whether we are valid or not, so if this isn't good for some reason,
721 // we reset it, and then we won't be valid and FindPlugin will fail and we won't get used.
722 m_disasm_ap.reset();
723 }
724
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000725 llvm::Triple::ArchType llvm_arch = triple.getArch();
726
Jason Molenda01aa5342013-07-22 22:11:53 +0000727 // For arm CPUs that can execute arm or thumb instructions, also create a thumb instruction disassembler.
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000728 if (llvm_arch == llvm::Triple::arm)
Jason Molenda01aa5342013-07-22 22:11:53 +0000729 {
Greg Claytonba812f42012-05-10 02:52:23 +0000730 std::string thumb_triple(thumb_arch.GetTriple().getTriple());
Jaydeep Patil501a7812015-07-16 03:51:55 +0000731 m_alternate_disasm_ap.reset(new LLVMCDisassembler(thumb_triple.c_str(), "", "", flavor, *this));
Jim Ingham0f063ba2013-03-02 00:26:47 +0000732 if (!m_alternate_disasm_ap->IsValid())
733 {
734 m_disasm_ap.reset();
735 m_alternate_disasm_ap.reset();
736 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000737 }
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000738 else if (llvm_arch == llvm::Triple::mips
739 || llvm_arch == llvm::Triple::mipsel
740 || llvm_arch == llvm::Triple::mips64
741 || llvm_arch == llvm::Triple::mips64el)
742 {
743 /* Create alternate disassembler for MIPS16 and microMIPS */
744 uint32_t arch_flags = arch.GetFlags ();
745 if (arch_flags & ArchSpec::eMIPSAse_mips16)
746 features_str += "+mips16,";
747 else if (arch_flags & ArchSpec::eMIPSAse_micromips)
748 features_str += "+micromips,";
749
750 m_alternate_disasm_ap.reset(new LLVMCDisassembler (triple_str, cpu, features_str.c_str(), flavor, *this));
751 if (!m_alternate_disasm_ap->IsValid())
752 {
753 m_disasm_ap.reset();
754 m_alternate_disasm_ap.reset();
755 }
756 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000757}
758
Eugene Zelenko45a40142015-10-22 21:24:37 +0000759DisassemblerLLVMC::~DisassemblerLLVMC() = default;
760
761Disassembler *
762DisassemblerLLVMC::CreateInstance (const ArchSpec &arch, const char *flavor)
Eugene Zelenko8dd3fdb2015-10-21 01:42:15 +0000763{
Eugene Zelenko45a40142015-10-22 21:24:37 +0000764 if (arch.GetTriple().getArch() != llvm::Triple::UnknownArch)
765 {
766 std::unique_ptr<DisassemblerLLVMC> disasm_ap (new DisassemblerLLVMC(arch, flavor));
767
768 if (disasm_ap.get() && disasm_ap->IsValid())
769 return disasm_ap.release();
770 }
771 return NULL;
Eugene Zelenko8dd3fdb2015-10-21 01:42:15 +0000772}
773
Sean Callanan95e5c632012-02-17 00:53:45 +0000774size_t
775DisassemblerLLVMC::DecodeInstructions (const Address &base_addr,
776 const DataExtractor& data,
Greg Claytonc7bece562013-01-25 18:06:21 +0000777 lldb::offset_t data_offset,
778 size_t num_instructions,
Greg Clayton3faf47c2013-03-28 23:42:53 +0000779 bool append,
780 bool data_from_file)
Sean Callanan95e5c632012-02-17 00:53:45 +0000781{
782 if (!append)
783 m_instruction_list.Clear();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000784
Sean Callanan95e5c632012-02-17 00:53:45 +0000785 if (!IsValid())
786 return 0;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000787
Greg Clayton3faf47c2013-03-28 23:42:53 +0000788 m_data_from_file = data_from_file;
Sean Callanan95e5c632012-02-17 00:53:45 +0000789 uint32_t data_cursor = data_offset;
Greg Claytonba812f42012-05-10 02:52:23 +0000790 const size_t data_byte_size = data.GetByteSize();
Sean Callanan95e5c632012-02-17 00:53:45 +0000791 uint32_t instructions_parsed = 0;
Greg Claytonba812f42012-05-10 02:52:23 +0000792 Address inst_addr(base_addr);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000793
Greg Claytonba812f42012-05-10 02:52:23 +0000794 while (data_cursor < data_byte_size && instructions_parsed < num_instructions)
Sean Callanan95e5c632012-02-17 00:53:45 +0000795 {
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000796
Greg Claytonba812f42012-05-10 02:52:23 +0000797 AddressClass address_class = eAddressClassCode;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000798
Jim Ingham0f063ba2013-03-02 00:26:47 +0000799 if (m_alternate_disasm_ap.get() != NULL)
Greg Claytonba812f42012-05-10 02:52:23 +0000800 address_class = inst_addr.GetAddressClass ();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000801
Sean Callanan95e5c632012-02-17 00:53:45 +0000802 InstructionSP inst_sp(new InstructionLLVMC(*this,
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000803 inst_addr,
Sean Callanan95e5c632012-02-17 00:53:45 +0000804 address_class));
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000805
Sean Callanan95e5c632012-02-17 00:53:45 +0000806 if (!inst_sp)
Greg Claytonba812f42012-05-10 02:52:23 +0000807 break;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000808
Sean Callanan95e5c632012-02-17 00:53:45 +0000809 uint32_t inst_size = inst_sp->Decode(*this, data, data_cursor);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000810
Greg Claytonba812f42012-05-10 02:52:23 +0000811 if (inst_size == 0)
812 break;
813
Sean Callanan95e5c632012-02-17 00:53:45 +0000814 m_instruction_list.Append(inst_sp);
Sean Callanan95e5c632012-02-17 00:53:45 +0000815 data_cursor += inst_size;
Greg Claytonba812f42012-05-10 02:52:23 +0000816 inst_addr.Slide(inst_size);
Sean Callanan95e5c632012-02-17 00:53:45 +0000817 instructions_parsed++;
818 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000819
Sean Callanan95e5c632012-02-17 00:53:45 +0000820 return data_cursor - data_offset;
821}
822
823void
824DisassemblerLLVMC::Initialize()
825{
826 PluginManager::RegisterPlugin (GetPluginNameStatic(),
Jason Molendaa3329782014-03-29 18:54:20 +0000827 "Disassembler that uses LLVM MC to disassemble i386, x86_64, ARM, and ARM64.",
Sean Callanan95e5c632012-02-17 00:53:45 +0000828 CreateInstance);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000829
Sean Callanan95e5c632012-02-17 00:53:45 +0000830 llvm::InitializeAllTargetInfos();
831 llvm::InitializeAllTargetMCs();
832 llvm::InitializeAllAsmParsers();
833 llvm::InitializeAllDisassemblers();
834}
835
836void
837DisassemblerLLVMC::Terminate()
838{
839 PluginManager::UnregisterPlugin (CreateInstance);
840}
841
842
Greg Clayton57abc5d2013-05-10 21:47:16 +0000843ConstString
Sean Callanan95e5c632012-02-17 00:53:45 +0000844DisassemblerLLVMC::GetPluginNameStatic()
845{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000846 static ConstString g_name("llvm-mc");
847 return g_name;
Sean Callanan95e5c632012-02-17 00:53:45 +0000848}
849
Greg Claytonba812f42012-05-10 02:52:23 +0000850int DisassemblerLLVMC::OpInfoCallback (void *disassembler,
851 uint64_t pc,
852 uint64_t offset,
853 uint64_t size,
854 int tag_type,
855 void *tag_bug)
Sean Callanan95e5c632012-02-17 00:53:45 +0000856{
Greg Claytonba812f42012-05-10 02:52:23 +0000857 return static_cast<DisassemblerLLVMC*>(disassembler)->OpInfo (pc,
858 offset,
859 size,
860 tag_type,
861 tag_bug);
Sean Callanan95e5c632012-02-17 00:53:45 +0000862}
863
Greg Claytonba812f42012-05-10 02:52:23 +0000864const char *DisassemblerLLVMC::SymbolLookupCallback (void *disassembler,
865 uint64_t value,
866 uint64_t *type,
867 uint64_t pc,
868 const char **name)
Sean Callanan95e5c632012-02-17 00:53:45 +0000869{
Greg Claytonba812f42012-05-10 02:52:23 +0000870 return static_cast<DisassemblerLLVMC*>(disassembler)->SymbolLookup(value,
871 type,
872 pc,
873 name);
Sean Callanan95e5c632012-02-17 00:53:45 +0000874}
875
Eugene Zelenko45a40142015-10-22 21:24:37 +0000876bool
877DisassemblerLLVMC::FlavorValidForArchSpec (const lldb_private::ArchSpec &arch, const char *flavor)
878{
879 llvm::Triple triple = arch.GetTriple();
880 if (flavor == NULL || strcmp (flavor, "default") == 0)
881 return true;
882
883 if (triple.getArch() == llvm::Triple::x86 || triple.getArch() == llvm::Triple::x86_64)
884 {
885 if (strcmp (flavor, "intel") == 0 || strcmp (flavor, "att") == 0)
886 return true;
887 else
888 return false;
889 }
890 else
891 return false;
892}
893
Sean Callanan95e5c632012-02-17 00:53:45 +0000894int DisassemblerLLVMC::OpInfo (uint64_t PC,
895 uint64_t Offset,
896 uint64_t Size,
Greg Claytonba812f42012-05-10 02:52:23 +0000897 int tag_type,
898 void *tag_bug)
Sean Callanan95e5c632012-02-17 00:53:45 +0000899{
Greg Claytonba812f42012-05-10 02:52:23 +0000900 switch (tag_type)
Sean Callanan95e5c632012-02-17 00:53:45 +0000901 {
902 default:
903 break;
904 case 1:
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000905 memset (tag_bug, 0, sizeof(::LLVMOpInfo1));
Sean Callanan95e5c632012-02-17 00:53:45 +0000906 break;
907 }
908 return 0;
909}
910
Greg Claytonba812f42012-05-10 02:52:23 +0000911const char *DisassemblerLLVMC::SymbolLookup (uint64_t value,
912 uint64_t *type_ptr,
913 uint64_t pc,
914 const char **name)
Sean Callanan95e5c632012-02-17 00:53:45 +0000915{
Greg Claytonba812f42012-05-10 02:52:23 +0000916 if (*type_ptr)
917 {
918 if (m_exe_ctx && m_inst)
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000919 {
Greg Claytonba812f42012-05-10 02:52:23 +0000920 //std::string remove_this_prior_to_checkin;
Greg Claytonba812f42012-05-10 02:52:23 +0000921 Target *target = m_exe_ctx ? m_exe_ctx->GetTargetPtr() : NULL;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000922 Address value_so_addr;
Jason Molendac980fa92015-02-13 23:24:21 +0000923 Address pc_so_addr;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000924 if (m_inst->UsingFileAddress())
Greg Claytonba812f42012-05-10 02:52:23 +0000925 {
926 ModuleSP module_sp(m_inst->GetAddress().GetModule());
927 if (module_sp)
Jason Molendac980fa92015-02-13 23:24:21 +0000928 {
Greg Clayton3faf47c2013-03-28 23:42:53 +0000929 module_sp->ResolveFileAddress(value, value_so_addr);
Jason Molendac980fa92015-02-13 23:24:21 +0000930 module_sp->ResolveFileAddress(pc, pc_so_addr);
931 }
Greg Claytonba812f42012-05-10 02:52:23 +0000932 }
Greg Clayton3faf47c2013-03-28 23:42:53 +0000933 else if (target && !target->GetSectionLoadList().IsEmpty())
934 {
935 target->GetSectionLoadList().ResolveLoadAddress(value, value_so_addr);
Jason Molendac980fa92015-02-13 23:24:21 +0000936 target->GetSectionLoadList().ResolveLoadAddress(pc, pc_so_addr);
937 }
938
939 SymbolContext sym_ctx;
940 const uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
941 if (pc_so_addr.IsValid() && pc_so_addr.GetModule())
942 {
943 pc_so_addr.GetModule()->ResolveSymbolContextForAddress (pc_so_addr, resolve_scope, sym_ctx);
Greg Clayton3faf47c2013-03-28 23:42:53 +0000944 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000945
Greg Clayton3faf47c2013-03-28 23:42:53 +0000946 if (value_so_addr.IsValid() && value_so_addr.GetSection())
Sean Callanan95e5c632012-02-17 00:53:45 +0000947 {
Sean Callanan95e5c632012-02-17 00:53:45 +0000948 StreamString ss;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000949
Jason Molendac980fa92015-02-13 23:24:21 +0000950 bool format_omitting_current_func_name = false;
951 if (sym_ctx.symbol || sym_ctx.function)
952 {
953 AddressRange range;
954 if (sym_ctx.GetAddressRange (resolve_scope, 0, false, range)
955 && range.GetBaseAddress().IsValid()
956 && range.ContainsLoadAddress (value_so_addr, target))
957 {
958 format_omitting_current_func_name = true;
959 }
960 }
961
962 // If the "value" address (the target address we're symbolicating)
963 // is inside the same SymbolContext as the current instruction pc
964 // (pc_so_addr), don't print the full function name - just print it
965 // with DumpStyleNoFunctionName style, e.g. "<+36>".
966 if (format_omitting_current_func_name)
967 {
968 value_so_addr.Dump (&ss,
969 target,
970 Address::DumpStyleNoFunctionName,
971 Address::DumpStyleSectionNameOffset);
972 }
973 else
974 {
975 value_so_addr.Dump (&ss,
976 target,
977 Address::DumpStyleResolvedDescriptionNoFunctionArguments,
978 Address::DumpStyleSectionNameOffset);
979 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000980
Sean Callanan745af462012-03-22 20:04:23 +0000981 if (!ss.GetString().empty())
Greg Claytonba812f42012-05-10 02:52:23 +0000982 {
Jason Molendaaff1b352014-10-10 23:07:36 +0000983 // If Address::Dump returned a multi-line description, most commonly seen when we
984 // have multiple levels of inlined functions at an address, only show the first line.
985 std::string &str(ss.GetString());
986 size_t first_eol_char = str.find_first_of ("\r\n");
987 if (first_eol_char != std::string::npos)
988 {
989 str.erase (first_eol_char);
990 }
Greg Claytonba812f42012-05-10 02:52:23 +0000991 m_inst->AppendComment(ss.GetString());
992 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000993 }
994 }
995 }
Greg Claytonba812f42012-05-10 02:52:23 +0000996
997 *type_ptr = LLVMDisassembler_ReferenceType_InOut_None;
998 *name = NULL;
999 return NULL;
Sean Callanan95e5c632012-02-17 00:53:45 +00001000}
1001
1002//------------------------------------------------------------------
1003// PluginInterface protocol
1004//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00001005ConstString
Sean Callanan95e5c632012-02-17 00:53:45 +00001006DisassemblerLLVMC::GetPluginName()
1007{
Sean Callanan95e5c632012-02-17 00:53:45 +00001008 return GetPluginNameStatic();
1009}
1010
1011uint32_t
1012DisassemblerLLVMC::GetPluginVersion()
1013{
1014 return 1;
1015}