blob: 1030c6866fc386d535a6aa9ee56921fc17a1ca83 [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"
Rafael Espindola97ae14e2014-11-07 04:24:12 +000024#include "llvm/Support/StringRefMemoryObject.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000025#include "llvm/Support/TargetRegistry.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000026#include "llvm/Support/TargetSelect.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000027#include "llvm/ADT/SmallString.h"
28
Sean Callanan95e5c632012-02-17 00:53:45 +000029
30#include "lldb/Core/Address.h"
31#include "lldb/Core/DataExtractor.h"
Greg Clayton1f746072012-08-29 21:13:06 +000032#include "lldb/Core/Module.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000033#include "lldb/Core/Stream.h"
34#include "lldb/Symbol/SymbolContext.h"
35#include "lldb/Target/ExecutionContext.h"
36#include "lldb/Target/Process.h"
37#include "lldb/Target/RegisterContext.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000038#include "lldb/Target/SectionLoadList.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000039#include "lldb/Target/Target.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000040#include "lldb/Target/StackFrame.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000041
Virgile Bellob2f1fb22013-08-23 12:44:05 +000042#include "lldb/Core/RegularExpression.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000043
44using namespace lldb;
45using namespace lldb_private;
46
47class InstructionLLVMC : public lldb_private::Instruction
48{
49public:
50 InstructionLLVMC (DisassemblerLLVMC &disasm,
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000051 const lldb_private::Address &address,
Greg Claytonc8e0c242012-04-13 00:07:34 +000052 AddressClass addr_class) :
Greg Clayton3faf47c2013-03-28 23:42:53 +000053 Instruction (address, addr_class),
54 m_disasm_sp (disasm.shared_from_this()),
55 m_does_branch (eLazyBoolCalculate),
56 m_is_valid (false),
57 m_using_file_addr (false)
Sean Callanan95e5c632012-02-17 00:53:45 +000058 {
59 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000060
Sean Callanan95e5c632012-02-17 00:53:45 +000061 virtual
62 ~InstructionLLVMC ()
63 {
64 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000065
Sean Callanan95e5c632012-02-17 00:53:45 +000066 virtual bool
Jim Ingham32ce20c2013-03-13 01:55:16 +000067 DoesBranch ()
Sean Callanan95e5c632012-02-17 00:53:45 +000068 {
Jim Ingham32ce20c2013-03-13 01:55:16 +000069 if (m_does_branch == eLazyBoolCalculate)
70 {
Greg Clayton3faf47c2013-03-28 23:42:53 +000071 GetDisassemblerLLVMC().Lock(this, NULL);
Jim Ingham32ce20c2013-03-13 01:55:16 +000072 DataExtractor data;
73 if (m_opcode.GetData(data))
74 {
75 bool is_alternate_isa;
76 lldb::addr_t pc = m_address.GetFileAddress();
77
78 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr = GetDisasmToUse (is_alternate_isa);
Greg Clayton3faf47c2013-03-28 23:42:53 +000079 const uint8_t *opcode_data = data.GetDataStart();
Jim Ingham32ce20c2013-03-13 01:55:16 +000080 const size_t opcode_data_len = data.GetByteSize();
81 llvm::MCInst inst;
Greg Clayton3faf47c2013-03-28 23:42:53 +000082 const size_t inst_size = mc_disasm_ptr->GetMCInst (opcode_data,
83 opcode_data_len,
84 pc,
85 inst);
Jim Ingham32ce20c2013-03-13 01:55:16 +000086 // Be conservative, if we didn't understand the instruction, say it might branch...
87 if (inst_size == 0)
88 m_does_branch = eLazyBoolYes;
89 else
90 {
Greg Clayton3faf47c2013-03-28 23:42:53 +000091 const bool can_branch = mc_disasm_ptr->CanBranch(inst);
Jim Ingham32ce20c2013-03-13 01:55:16 +000092 if (can_branch)
93 m_does_branch = eLazyBoolYes;
94 else
95 m_does_branch = eLazyBoolNo;
96 }
97 }
Greg Clayton3faf47c2013-03-28 23:42:53 +000098 GetDisassemblerLLVMC().Unlock();
Jim Ingham32ce20c2013-03-13 01:55:16 +000099 }
Sean Callanan7725a462012-03-02 23:22:53 +0000100 return m_does_branch == eLazyBoolYes;
Sean Callanan95e5c632012-02-17 00:53:45 +0000101 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000102
Jim Ingham32ce20c2013-03-13 01:55:16 +0000103 DisassemblerLLVMC::LLVMCDisassembler *
104 GetDisasmToUse (bool &is_alternate_isa)
105 {
Jim Ingham32ce20c2013-03-13 01:55:16 +0000106 is_alternate_isa = false;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000107 DisassemblerLLVMC &llvm_disasm = GetDisassemblerLLVMC();
108 if (llvm_disasm.m_alternate_disasm_ap.get() != NULL)
Jim Ingham32ce20c2013-03-13 01:55:16 +0000109 {
110 const AddressClass address_class = GetAddressClass ();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000111
Jim Ingham32ce20c2013-03-13 01:55:16 +0000112 if (address_class == eAddressClassCodeAlternateISA)
113 {
Jim Ingham32ce20c2013-03-13 01:55:16 +0000114 is_alternate_isa = true;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000115 return llvm_disasm.m_alternate_disasm_ap.get();
Jim Ingham32ce20c2013-03-13 01:55:16 +0000116 }
117 }
Greg Clayton3faf47c2013-03-28 23:42:53 +0000118 return llvm_disasm.m_disasm_ap.get();
Jim Ingham32ce20c2013-03-13 01:55:16 +0000119 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000120
Sean Callanan95e5c632012-02-17 00:53:45 +0000121 virtual size_t
122 Decode (const lldb_private::Disassembler &disassembler,
123 const lldb_private::DataExtractor &data,
Greg Claytonc7bece562013-01-25 18:06:21 +0000124 lldb::offset_t data_offset)
Sean Callanan95e5c632012-02-17 00:53:45 +0000125 {
Greg Claytonba812f42012-05-10 02:52:23 +0000126 // All we have to do is read the opcode which can be easy for some
Michael Sartaincc791bb2013-07-11 16:40:56 +0000127 // architectures
Greg Claytonba812f42012-05-10 02:52:23 +0000128 bool got_op = false;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000129 DisassemblerLLVMC &llvm_disasm = GetDisassemblerLLVMC();
130 const ArchSpec &arch = llvm_disasm.GetArchitecture();
Ed Maste90359962013-12-09 19:45:33 +0000131 const lldb::ByteOrder byte_order = data.GetByteOrder();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000132
Greg Claytonba812f42012-05-10 02:52:23 +0000133 const uint32_t min_op_byte_size = arch.GetMinimumOpcodeByteSize();
134 const uint32_t max_op_byte_size = arch.GetMaximumOpcodeByteSize();
135 if (min_op_byte_size == max_op_byte_size)
136 {
137 // Fixed size instructions, just read that amount of data.
138 if (!data.ValidOffsetForDataOfSize(data_offset, min_op_byte_size))
139 return false;
Ed Maste90359962013-12-09 19:45:33 +0000140
Greg Claytonba812f42012-05-10 02:52:23 +0000141 switch (min_op_byte_size)
142 {
143 case 1:
Ed Maste90359962013-12-09 19:45:33 +0000144 m_opcode.SetOpcode8 (data.GetU8 (&data_offset), byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000145 got_op = true;
146 break;
147
148 case 2:
Ed Maste90359962013-12-09 19:45:33 +0000149 m_opcode.SetOpcode16 (data.GetU16 (&data_offset), byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000150 got_op = true;
151 break;
152
153 case 4:
Ed Maste90359962013-12-09 19:45:33 +0000154 m_opcode.SetOpcode32 (data.GetU32 (&data_offset), byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000155 got_op = true;
156 break;
157
158 case 8:
Ed Maste90359962013-12-09 19:45:33 +0000159 m_opcode.SetOpcode64 (data.GetU64 (&data_offset), byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000160 got_op = true;
161 break;
162
163 default:
164 m_opcode.SetOpcodeBytes(data.PeekData(data_offset, min_op_byte_size), min_op_byte_size);
165 got_op = true;
166 break;
167 }
168 }
169 if (!got_op)
170 {
Jim Ingham32ce20c2013-03-13 01:55:16 +0000171 bool is_alternate_isa = false;
172 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr = GetDisasmToUse (is_alternate_isa);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000173
Greg Claytonba812f42012-05-10 02:52:23 +0000174 const llvm::Triple::ArchType machine = arch.GetMachine();
175 if (machine == llvm::Triple::arm || machine == llvm::Triple::thumb)
176 {
Jim Ingham32ce20c2013-03-13 01:55:16 +0000177 if (machine == llvm::Triple::thumb || is_alternate_isa)
Greg Claytonba812f42012-05-10 02:52:23 +0000178 {
Greg Clayton79101b52012-08-07 01:29:29 +0000179 uint32_t thumb_opcode = data.GetU16(&data_offset);
Greg Claytonba812f42012-05-10 02:52:23 +0000180 if ((thumb_opcode & 0xe000) != 0xe000 || ((thumb_opcode & 0x1800u) == 0))
181 {
Ed Maste90359962013-12-09 19:45:33 +0000182 m_opcode.SetOpcode16 (thumb_opcode, byte_order);
Sean Callanan5c97c2f2012-08-06 23:42:52 +0000183 m_is_valid = true;
Greg Claytonba812f42012-05-10 02:52:23 +0000184 }
185 else
186 {
Greg Clayton79101b52012-08-07 01:29:29 +0000187 thumb_opcode <<= 16;
188 thumb_opcode |= data.GetU16(&data_offset);
Ed Maste90359962013-12-09 19:45:33 +0000189 m_opcode.SetOpcode16_2 (thumb_opcode, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000190 m_is_valid = true;
191 }
192 }
193 else
194 {
Ed Maste90359962013-12-09 19:45:33 +0000195 m_opcode.SetOpcode32 (data.GetU32(&data_offset), byte_order);
Sean Callanan5c97c2f2012-08-06 23:42:52 +0000196 m_is_valid = true;
Greg Claytonba812f42012-05-10 02:52:23 +0000197 }
198 }
199 else
200 {
201 // The opcode isn't evenly sized, so we need to actually use the llvm
202 // disassembler to parse it and get the size.
Greg Claytonba812f42012-05-10 02:52:23 +0000203 uint8_t *opcode_data = const_cast<uint8_t *>(data.PeekData (data_offset, 1));
Greg Clayton3faf47c2013-03-28 23:42:53 +0000204 const size_t opcode_data_len = data.BytesLeft(data_offset);
Greg Claytonba812f42012-05-10 02:52:23 +0000205 const addr_t pc = m_address.GetFileAddress();
Jim Ingham0f063ba2013-03-02 00:26:47 +0000206 llvm::MCInst inst;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000207
Greg Clayton3faf47c2013-03-28 23:42:53 +0000208 llvm_disasm.Lock(this, NULL);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000209 const size_t inst_size = mc_disasm_ptr->GetMCInst(opcode_data,
Greg Clayton3faf47c2013-03-28 23:42:53 +0000210 opcode_data_len,
211 pc,
212 inst);
213 llvm_disasm.Unlock();
Greg Claytonba812f42012-05-10 02:52:23 +0000214 if (inst_size == 0)
215 m_opcode.Clear();
216 else
217 {
218 m_opcode.SetOpcodeBytes(opcode_data, inst_size);
219 m_is_valid = true;
220 }
221 }
222 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000223 return m_opcode.GetByteSize();
224 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000225
Sean Callanan95e5c632012-02-17 00:53:45 +0000226 void
Greg Claytonba812f42012-05-10 02:52:23 +0000227 AppendComment (std::string &description)
Sean Callanan95e5c632012-02-17 00:53:45 +0000228 {
Greg Claytonba812f42012-05-10 02:52:23 +0000229 if (m_comment.empty())
230 m_comment.swap (description);
Sean Callanan95e5c632012-02-17 00:53:45 +0000231 else
Greg Claytonba812f42012-05-10 02:52:23 +0000232 {
233 m_comment.append(", ");
234 m_comment.append(description);
235 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000236 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000237
Sean Callanan95e5c632012-02-17 00:53:45 +0000238 virtual void
Greg Claytonba812f42012-05-10 02:52:23 +0000239 CalculateMnemonicOperandsAndComment (const lldb_private::ExecutionContext *exe_ctx)
Sean Callanan95e5c632012-02-17 00:53:45 +0000240 {
Greg Claytonba812f42012-05-10 02:52:23 +0000241 DataExtractor data;
242 const AddressClass address_class = GetAddressClass ();
243
Sean Callanancd4ae1a2012-08-07 01:44:58 +0000244 if (m_opcode.GetData(data))
Greg Claytonba812f42012-05-10 02:52:23 +0000245 {
246 char out_string[512];
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000247
Greg Clayton3faf47c2013-03-28 23:42:53 +0000248 DisassemblerLLVMC &llvm_disasm = GetDisassemblerLLVMC();
249
Jim Ingham0f063ba2013-03-02 00:26:47 +0000250 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000251
Greg Claytonba812f42012-05-10 02:52:23 +0000252 if (address_class == eAddressClassCodeAlternateISA)
Greg Clayton3faf47c2013-03-28 23:42:53 +0000253 mc_disasm_ptr = llvm_disasm.m_alternate_disasm_ap.get();
Greg Claytonba812f42012-05-10 02:52:23 +0000254 else
Greg Clayton3faf47c2013-03-28 23:42:53 +0000255 mc_disasm_ptr = llvm_disasm.m_disasm_ap.get();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000256
Greg Clayton3faf47c2013-03-28 23:42:53 +0000257 lldb::addr_t pc = m_address.GetFileAddress();
258 m_using_file_addr = true;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000259
Greg Clayton3faf47c2013-03-28 23:42:53 +0000260 const bool data_from_file = GetDisassemblerLLVMC().m_data_from_file;
Daniel Malead79ae052013-08-07 21:54:09 +0000261 bool use_hex_immediates = true;
262 Disassembler::HexImmediateStyle hex_style = Disassembler::eHexStyleC;
263
264 if (exe_ctx)
Greg Claytonba812f42012-05-10 02:52:23 +0000265 {
Daniel Malead79ae052013-08-07 21:54:09 +0000266 Target *target = exe_ctx->GetTargetPtr();
267 if (target)
Greg Clayton3faf47c2013-03-28 23:42:53 +0000268 {
Daniel Malead79ae052013-08-07 21:54:09 +0000269 use_hex_immediates = target->GetUseHexImmediates();
270 hex_style = target->GetHexImmediateStyle();
271
272 if (!data_from_file)
Greg Clayton3faf47c2013-03-28 23:42:53 +0000273 {
274 const lldb::addr_t load_addr = m_address.GetLoadAddress(target);
275 if (load_addr != LLDB_INVALID_ADDRESS)
276 {
277 pc = load_addr;
278 m_using_file_addr = false;
279 }
280 }
281 }
Greg Claytonba812f42012-05-10 02:52:23 +0000282 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000283
Greg Clayton3faf47c2013-03-28 23:42:53 +0000284 llvm_disasm.Lock(this, exe_ctx);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000285
Greg Clayton3faf47c2013-03-28 23:42:53 +0000286 const uint8_t *opcode_data = data.GetDataStart();
Greg Claytonba812f42012-05-10 02:52:23 +0000287 const size_t opcode_data_len = data.GetByteSize();
Jim Ingham0f063ba2013-03-02 00:26:47 +0000288 llvm::MCInst inst;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000289 size_t inst_size = mc_disasm_ptr->GetMCInst (opcode_data,
290 opcode_data_len,
291 pc,
292 inst);
Daniel Malead79ae052013-08-07 21:54:09 +0000293
Jim Ingham0f063ba2013-03-02 00:26:47 +0000294 if (inst_size > 0)
Daniel Malead79ae052013-08-07 21:54:09 +0000295 {
296 mc_disasm_ptr->SetStyle(use_hex_immediates, hex_style);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000297 mc_disasm_ptr->PrintMCInst(inst, out_string, sizeof(out_string));
Daniel Malead79ae052013-08-07 21:54:09 +0000298 }
299
Greg Clayton3faf47c2013-03-28 23:42:53 +0000300 llvm_disasm.Unlock();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000301
Greg Claytonba812f42012-05-10 02:52:23 +0000302 if (inst_size == 0)
303 {
304 m_comment.assign ("unknown opcode");
305 inst_size = m_opcode.GetByteSize();
306 StreamString mnemonic_strm;
Greg Claytonc7bece562013-01-25 18:06:21 +0000307 lldb::offset_t offset = 0;
Ed Maste90359962013-12-09 19:45:33 +0000308 lldb::ByteOrder byte_order = data.GetByteOrder();
Greg Claytonba812f42012-05-10 02:52:23 +0000309 switch (inst_size)
310 {
311 case 1:
312 {
313 const uint8_t uval8 = data.GetU8 (&offset);
Ed Maste90359962013-12-09 19:45:33 +0000314 m_opcode.SetOpcode8 (uval8, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000315 m_opcode_name.assign (".byte");
316 mnemonic_strm.Printf("0x%2.2x", uval8);
317 }
318 break;
319 case 2:
320 {
321 const uint16_t uval16 = data.GetU16(&offset);
Ed Maste90359962013-12-09 19:45:33 +0000322 m_opcode.SetOpcode16(uval16, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000323 m_opcode_name.assign (".short");
324 mnemonic_strm.Printf("0x%4.4x", uval16);
325 }
326 break;
327 case 4:
328 {
329 const uint32_t uval32 = data.GetU32(&offset);
Ed Maste90359962013-12-09 19:45:33 +0000330 m_opcode.SetOpcode32(uval32, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000331 m_opcode_name.assign (".long");
332 mnemonic_strm.Printf("0x%8.8x", uval32);
333 }
334 break;
335 case 8:
336 {
337 const uint64_t uval64 = data.GetU64(&offset);
Ed Maste90359962013-12-09 19:45:33 +0000338 m_opcode.SetOpcode64(uval64, byte_order);
Greg Claytonba812f42012-05-10 02:52:23 +0000339 m_opcode_name.assign (".quad");
Daniel Malead01b2952012-11-29 21:49:15 +0000340 mnemonic_strm.Printf("0x%16.16" PRIx64, uval64);
Greg Claytonba812f42012-05-10 02:52:23 +0000341 }
342 break;
343 default:
344 if (inst_size == 0)
345 return;
346 else
347 {
348 const uint8_t *bytes = data.PeekData(offset, inst_size);
349 if (bytes == NULL)
350 return;
351 m_opcode_name.assign (".byte");
352 m_opcode.SetOpcodeBytes(bytes, inst_size);
353 mnemonic_strm.Printf("0x%2.2x", bytes[0]);
354 for (uint32_t i=1; i<inst_size; ++i)
355 mnemonic_strm.Printf(" 0x%2.2x", bytes[i]);
356 }
357 break;
358 }
Jim Ingham0f063ba2013-03-02 00:26:47 +0000359 m_mnemonics.swap(mnemonic_strm.GetString());
Greg Claytonba812f42012-05-10 02:52:23 +0000360 return;
361 }
362 else
363 {
364 if (m_does_branch == eLazyBoolCalculate)
365 {
Greg Clayton3faf47c2013-03-28 23:42:53 +0000366 const bool can_branch = mc_disasm_ptr->CanBranch(inst);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000367 if (can_branch)
Greg Claytonba812f42012-05-10 02:52:23 +0000368 m_does_branch = eLazyBoolYes;
369 else
370 m_does_branch = eLazyBoolNo;
Jim Ingham0f063ba2013-03-02 00:26:47 +0000371
Greg Claytonba812f42012-05-10 02:52:23 +0000372 }
373 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000374
Greg Claytondf96dd72013-08-27 19:53:47 +0000375 static RegularExpression s_regex("[ \t]*([^ ^\t]+)[ \t]*([^ ^\t].*)?", REG_EXTENDED);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000376
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000377 RegularExpression::Match matches(3);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000378
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000379 if (s_regex.Execute(out_string, &matches))
Greg Claytonba812f42012-05-10 02:52:23 +0000380 {
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000381 matches.GetMatchAtIndex(out_string, 1, m_opcode_name);
382 matches.GetMatchAtIndex(out_string, 2, m_mnemonics);
Greg Claytonba812f42012-05-10 02:52:23 +0000383 }
384 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000385 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000386
Sean Callanan95e5c632012-02-17 00:53:45 +0000387 bool
Greg Clayton3faf47c2013-03-28 23:42:53 +0000388 IsValid () const
Sean Callanan95e5c632012-02-17 00:53:45 +0000389 {
390 return m_is_valid;
391 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000392
Greg Clayton3faf47c2013-03-28 23:42:53 +0000393 bool
394 UsingFileAddress() const
395 {
396 return m_using_file_addr;
397 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000398 size_t
Greg Clayton3faf47c2013-03-28 23:42:53 +0000399 GetByteSize () const
Sean Callanan95e5c632012-02-17 00:53:45 +0000400 {
401 return m_opcode.GetByteSize();
402 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000403
Greg Clayton3faf47c2013-03-28 23:42:53 +0000404 DisassemblerLLVMC &
405 GetDisassemblerLLVMC ()
406 {
407 return *(DisassemblerLLVMC *)m_disasm_sp.get();
408 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000409protected:
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000410
Sean Callanan7e6d4e52012-08-01 18:50:59 +0000411 DisassemblerSP m_disasm_sp; // for ownership
Sean Callanan7725a462012-03-02 23:22:53 +0000412 LazyBool m_does_branch;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000413 bool m_is_valid;
414 bool m_using_file_addr;
Sean Callanan95e5c632012-02-17 00:53:45 +0000415};
416
Greg Claytondf96dd72013-08-27 19:53:47 +0000417
Sean Callanan95e5c632012-02-17 00:53:45 +0000418
Jim Ingham0f063ba2013-03-02 00:26:47 +0000419DisassemblerLLVMC::LLVMCDisassembler::LLVMCDisassembler (const char *triple, unsigned flavor, DisassemblerLLVMC &owner):
420 m_is_valid(true)
421{
422 std::string Error;
423 const llvm::Target *curr_target = llvm::TargetRegistry::lookupTarget(triple, Error);
424 if (!curr_target)
425 {
426 m_is_valid = false;
427 return;
428 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000429
Jim Ingham0f063ba2013-03-02 00:26:47 +0000430 m_instr_info_ap.reset(curr_target->createMCInstrInfo());
431 m_reg_info_ap.reset (curr_target->createMCRegInfo(triple));
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000432
Jim Ingham0f063ba2013-03-02 00:26:47 +0000433 std::string features_str;
434
435 m_subtarget_info_ap.reset(curr_target->createMCSubtargetInfo(triple, "",
436 features_str));
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000437
Jean-Daniel Dupasc6f26f82013-12-29 20:17:26 +0000438 std::unique_ptr<llvm::MCRegisterInfo> reg_info(curr_target->createMCRegInfo(triple));
439 m_asm_info_ap.reset(curr_target->createMCAsmInfo(*reg_info, triple));
Sylvestre Ledru7a89d6f2013-05-13 13:41:13 +0000440
Jim Ingham0f063ba2013-03-02 00:26:47 +0000441 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)
442 {
Matt Kopec787d1622013-03-12 17:45:38 +0000443 m_is_valid = false;
Jim Ingham0f063ba2013-03-02 00:26:47 +0000444 return;
445 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000446
Bill Wendlingd63d0a82013-06-18 07:50:43 +0000447 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 +0000448
Sylvestre Ledru5ac35ae2014-04-15 12:07:25 +0000449 m_disasm_ap.reset(curr_target->createMCDisassembler(*m_subtarget_info_ap.get(), *m_context_ap.get()));
Ashok Thirumurthi78059352013-05-24 15:55:54 +0000450 if (m_disasm_ap.get() && m_context_ap.get())
Jim Ingham0f063ba2013-03-02 00:26:47 +0000451 {
Ahmed Charles8f926ad2014-03-07 04:45:22 +0000452 std::unique_ptr<llvm::MCRelocationInfo> RelInfo(curr_target->createMCRelocationInfo(triple, *m_context_ap.get()));
Ashok Thirumurthi78059352013-05-24 15:55:54 +0000453 if (!RelInfo)
454 {
455 m_is_valid = false;
456 return;
457 }
Jason Molenda64a68d62014-05-17 00:27:44 +0000458 std::unique_ptr<llvm::MCSymbolizer> symbolizer_up(curr_target->createMCSymbolizer(triple, NULL,
459 DisassemblerLLVMC::SymbolLookupCallback,
460 (void *) &owner,
461 m_context_ap.get(), RelInfo.release()));
462 m_disasm_ap->setSymbolizer(std::move(symbolizer_up));
Greg Clayton3434b572014-04-14 21:33:38 +0000463
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000464
Jim Ingham0f063ba2013-03-02 00:26:47 +0000465 unsigned asm_printer_variant;
466 if (flavor == ~0U)
467 asm_printer_variant = m_asm_info_ap->getAssemblerDialect();
468 else
469 {
470 asm_printer_variant = flavor;
471 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000472
Jim Ingham0f063ba2013-03-02 00:26:47 +0000473 m_instr_printer_ap.reset(curr_target->createMCInstPrinter(asm_printer_variant,
474 *m_asm_info_ap.get(),
475 *m_instr_info_ap.get(),
476 *m_reg_info_ap.get(),
477 *m_subtarget_info_ap.get()));
478 if (m_instr_printer_ap.get() == NULL)
479 {
480 m_disasm_ap.reset();
481 m_is_valid = false;
482 }
483 }
484 else
485 m_is_valid = false;
486}
487
Greg Claytone01e07b2013-04-18 18:10:51 +0000488DisassemblerLLVMC::LLVMCDisassembler::~LLVMCDisassembler()
489{
490}
491
Jim Ingham0f063ba2013-03-02 00:26:47 +0000492uint64_t
Greg Clayton3faf47c2013-03-28 23:42:53 +0000493DisassemblerLLVMC::LLVMCDisassembler::GetMCInst (const uint8_t *opcode_data,
494 size_t opcode_data_len,
495 lldb::addr_t pc,
496 llvm::MCInst &mc_inst)
Jim Ingham0f063ba2013-03-02 00:26:47 +0000497{
Rafael Espindola97ae14e2014-11-07 04:24:12 +0000498 llvm::StringRef data((const char*)opcode_data, opcode_data_len);
499 llvm::StringRefMemoryObject memory_object(data, pc);
Jim Ingham0f063ba2013-03-02 00:26:47 +0000500 llvm::MCDisassembler::DecodeStatus status;
501
502 uint64_t new_inst_size;
503 status = m_disasm_ap->getInstruction(mc_inst,
504 new_inst_size,
505 memory_object,
506 pc,
507 llvm::nulls(),
508 llvm::nulls());
509 if (status == llvm::MCDisassembler::Success)
510 return new_inst_size;
511 else
512 return 0;
513}
514
515uint64_t
Greg Clayton3faf47c2013-03-28 23:42:53 +0000516DisassemblerLLVMC::LLVMCDisassembler::PrintMCInst (llvm::MCInst &mc_inst,
517 char *dst,
518 size_t dst_len)
Jim Ingham0f063ba2013-03-02 00:26:47 +0000519{
520 llvm::StringRef unused_annotations;
521 llvm::SmallString<64> inst_string;
522 llvm::raw_svector_ostream inst_stream(inst_string);
523 m_instr_printer_ap->printInst (&mc_inst, inst_stream, unused_annotations);
524 inst_stream.flush();
Greg Clayton3faf47c2013-03-28 23:42:53 +0000525 const size_t output_size = std::min(dst_len - 1, inst_string.size());
526 std::memcpy(dst, inst_string.data(), output_size);
527 dst[output_size] = '\0';
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000528
Jim Ingham0f063ba2013-03-02 00:26:47 +0000529 return output_size;
530}
531
Daniel Malead79ae052013-08-07 21:54:09 +0000532void
533DisassemblerLLVMC::LLVMCDisassembler::SetStyle (bool use_hex_immed, HexImmediateStyle hex_style)
534{
535 m_instr_printer_ap->setPrintImmHex(use_hex_immed);
536 switch(hex_style)
537 {
538 case eHexStyleC: m_instr_printer_ap->setPrintImmHex(llvm::HexStyle::C); break;
539 case eHexStyleAsm: m_instr_printer_ap->setPrintImmHex(llvm::HexStyle::Asm); break;
540 }
541}
542
Jim Ingham0f063ba2013-03-02 00:26:47 +0000543bool
544DisassemblerLLVMC::LLVMCDisassembler::CanBranch (llvm::MCInst &mc_inst)
545{
546 return m_instr_info_ap->get(mc_inst.getOpcode()).mayAffectControlFlow(mc_inst, *m_reg_info_ap.get());
547}
548
549bool
550DisassemblerLLVMC::FlavorValidForArchSpec (const lldb_private::ArchSpec &arch, const char *flavor)
551{
552 llvm::Triple triple = arch.GetTriple();
553 if (flavor == NULL || strcmp (flavor, "default") == 0)
554 return true;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000555
Jim Ingham0f063ba2013-03-02 00:26:47 +0000556 if (triple.getArch() == llvm::Triple::x86 || triple.getArch() == llvm::Triple::x86_64)
557 {
558 if (strcmp (flavor, "intel") == 0 || strcmp (flavor, "att") == 0)
559 return true;
560 else
561 return false;
562 }
563 else
564 return false;
565}
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000566
Jim Ingham0f063ba2013-03-02 00:26:47 +0000567
Sean Callanan95e5c632012-02-17 00:53:45 +0000568Disassembler *
Jim Ingham0f063ba2013-03-02 00:26:47 +0000569DisassemblerLLVMC::CreateInstance (const ArchSpec &arch, const char *flavor)
Sean Callanan95e5c632012-02-17 00:53:45 +0000570{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000571 if (arch.GetTriple().getArch() != llvm::Triple::UnknownArch)
572 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000573 std::unique_ptr<DisassemblerLLVMC> disasm_ap (new DisassemblerLLVMC(arch, flavor));
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000574
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000575 if (disasm_ap.get() && disasm_ap->IsValid())
576 return disasm_ap.release();
577 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000578 return NULL;
579}
580
Jim Ingham0f063ba2013-03-02 00:26:47 +0000581DisassemblerLLVMC::DisassemblerLLVMC (const ArchSpec &arch, const char *flavor_string) :
582 Disassembler(arch, flavor_string),
Greg Claytonba812f42012-05-10 02:52:23 +0000583 m_exe_ctx (NULL),
Greg Clayton3faf47c2013-03-28 23:42:53 +0000584 m_inst (NULL),
585 m_data_from_file (false)
Sean Callanan95e5c632012-02-17 00:53:45 +0000586{
Jim Ingham0f063ba2013-03-02 00:26:47 +0000587 if (!FlavorValidForArchSpec (arch, m_flavor.c_str()))
588 {
589 m_flavor.assign("default");
590 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000591
Jim Ingham0f063ba2013-03-02 00:26:47 +0000592 const char *triple = arch.GetTriple().getTriple().c_str();
593 unsigned flavor = ~0U;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000594
Jim Ingham0f063ba2013-03-02 00:26:47 +0000595 // So far the only supported flavor is "intel" on x86. The base class will set this
596 // correctly coming in.
597 if (arch.GetTriple().getArch() == llvm::Triple::x86
598 || arch.GetTriple().getArch() == llvm::Triple::x86_64)
599 {
600 if (m_flavor == "intel")
601 {
602 flavor = 1;
603 }
604 else if (m_flavor == "att")
605 {
606 flavor = 0;
607 }
608 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000609
Jason Molenda01aa5342013-07-22 22:11:53 +0000610 ArchSpec thumb_arch(arch);
Sean Callanan95e5c632012-02-17 00:53:45 +0000611 if (arch.GetTriple().getArch() == llvm::Triple::arm)
612 {
Greg Claytona80ea122013-05-03 01:05:04 +0000613 std::string thumb_arch_name (thumb_arch.GetTriple().getArchName().str());
614 // Replace "arm" with "thumb" so we get all thumb variants correct
615 if (thumb_arch_name.size() > 3)
616 {
617 thumb_arch_name.erase(0,3);
618 thumb_arch_name.insert(0, "thumb");
619 }
620 else
621 {
622 thumb_arch_name = "thumbv7";
623 }
624 thumb_arch.GetTriple().setArchName(llvm::StringRef(thumb_arch_name.c_str()));
Jason Molenda01aa5342013-07-22 22:11:53 +0000625 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000626
627 // Cortex-M3 devices (e.g. armv7m) can only execute thumb (T2) instructions,
Jason Molendaa3a04522013-09-27 23:21:54 +0000628 // so hardcode the primary disassembler to thumb mode. Same for Cortex-M4 (armv7em).
629 //
630 // Handle the Cortex-M0 (armv6m) the same; the ISA is a subset of the T and T32
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000631 // instructions defined in ARMv7-A.
Jason Molendaa3a04522013-09-27 23:21:54 +0000632
Jason Molenda01aa5342013-07-22 22:11:53 +0000633 if (arch.GetTriple().getArch() == llvm::Triple::arm
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000634 && (arch.GetCore() == ArchSpec::Core::eCore_arm_armv7m
Jason Molendaa3a04522013-09-27 23:21:54 +0000635 || arch.GetCore() == ArchSpec::Core::eCore_arm_armv7em
636 || arch.GetCore() == ArchSpec::Core::eCore_arm_armv6m))
Jason Molenda01aa5342013-07-22 22:11:53 +0000637 {
638 triple = thumb_arch.GetTriple().getTriple().c_str();
639 }
640
641 m_disasm_ap.reset (new LLVMCDisassembler(triple, flavor, *this));
642 if (!m_disasm_ap->IsValid())
643 {
644 // We use m_disasm_ap.get() to tell whether we are valid or not, so if this isn't good for some reason,
645 // we reset it, and then we won't be valid and FindPlugin will fail and we won't get used.
646 m_disasm_ap.reset();
647 }
648
649 // For arm CPUs that can execute arm or thumb instructions, also create a thumb instruction disassembler.
650 if (arch.GetTriple().getArch() == llvm::Triple::arm)
651 {
Greg Claytonba812f42012-05-10 02:52:23 +0000652 std::string thumb_triple(thumb_arch.GetTriple().getTriple());
Jim Ingham0f063ba2013-03-02 00:26:47 +0000653 m_alternate_disasm_ap.reset(new LLVMCDisassembler(thumb_triple.c_str(), flavor, *this));
654 if (!m_alternate_disasm_ap->IsValid())
655 {
656 m_disasm_ap.reset();
657 m_alternate_disasm_ap.reset();
658 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000659 }
660}
661
662DisassemblerLLVMC::~DisassemblerLLVMC()
663{
664}
665
666size_t
667DisassemblerLLVMC::DecodeInstructions (const Address &base_addr,
668 const DataExtractor& data,
Greg Claytonc7bece562013-01-25 18:06:21 +0000669 lldb::offset_t data_offset,
670 size_t num_instructions,
Greg Clayton3faf47c2013-03-28 23:42:53 +0000671 bool append,
672 bool data_from_file)
Sean Callanan95e5c632012-02-17 00:53:45 +0000673{
674 if (!append)
675 m_instruction_list.Clear();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000676
Sean Callanan95e5c632012-02-17 00:53:45 +0000677 if (!IsValid())
678 return 0;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000679
Greg Clayton3faf47c2013-03-28 23:42:53 +0000680 m_data_from_file = data_from_file;
Sean Callanan95e5c632012-02-17 00:53:45 +0000681 uint32_t data_cursor = data_offset;
Greg Claytonba812f42012-05-10 02:52:23 +0000682 const size_t data_byte_size = data.GetByteSize();
Sean Callanan95e5c632012-02-17 00:53:45 +0000683 uint32_t instructions_parsed = 0;
Greg Claytonba812f42012-05-10 02:52:23 +0000684 Address inst_addr(base_addr);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000685
Greg Claytonba812f42012-05-10 02:52:23 +0000686 while (data_cursor < data_byte_size && instructions_parsed < num_instructions)
Sean Callanan95e5c632012-02-17 00:53:45 +0000687 {
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000688
Greg Claytonba812f42012-05-10 02:52:23 +0000689 AddressClass address_class = eAddressClassCode;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000690
Jim Ingham0f063ba2013-03-02 00:26:47 +0000691 if (m_alternate_disasm_ap.get() != NULL)
Greg Claytonba812f42012-05-10 02:52:23 +0000692 address_class = inst_addr.GetAddressClass ();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000693
Sean Callanan95e5c632012-02-17 00:53:45 +0000694 InstructionSP inst_sp(new InstructionLLVMC(*this,
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000695 inst_addr,
Sean Callanan95e5c632012-02-17 00:53:45 +0000696 address_class));
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000697
Sean Callanan95e5c632012-02-17 00:53:45 +0000698 if (!inst_sp)
Greg Claytonba812f42012-05-10 02:52:23 +0000699 break;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000700
Sean Callanan95e5c632012-02-17 00:53:45 +0000701 uint32_t inst_size = inst_sp->Decode(*this, data, data_cursor);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000702
Greg Claytonba812f42012-05-10 02:52:23 +0000703 if (inst_size == 0)
704 break;
705
Sean Callanan95e5c632012-02-17 00:53:45 +0000706 m_instruction_list.Append(inst_sp);
Sean Callanan95e5c632012-02-17 00:53:45 +0000707 data_cursor += inst_size;
Greg Claytonba812f42012-05-10 02:52:23 +0000708 inst_addr.Slide(inst_size);
Sean Callanan95e5c632012-02-17 00:53:45 +0000709 instructions_parsed++;
710 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000711
Sean Callanan95e5c632012-02-17 00:53:45 +0000712 return data_cursor - data_offset;
713}
714
715void
716DisassemblerLLVMC::Initialize()
717{
718 PluginManager::RegisterPlugin (GetPluginNameStatic(),
Jason Molendaa3329782014-03-29 18:54:20 +0000719 "Disassembler that uses LLVM MC to disassemble i386, x86_64, ARM, and ARM64.",
Sean Callanan95e5c632012-02-17 00:53:45 +0000720 CreateInstance);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000721
Sean Callanan95e5c632012-02-17 00:53:45 +0000722 llvm::InitializeAllTargetInfos();
723 llvm::InitializeAllTargetMCs();
724 llvm::InitializeAllAsmParsers();
725 llvm::InitializeAllDisassemblers();
726}
727
728void
729DisassemblerLLVMC::Terminate()
730{
731 PluginManager::UnregisterPlugin (CreateInstance);
732}
733
734
Greg Clayton57abc5d2013-05-10 21:47:16 +0000735ConstString
Sean Callanan95e5c632012-02-17 00:53:45 +0000736DisassemblerLLVMC::GetPluginNameStatic()
737{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000738 static ConstString g_name("llvm-mc");
739 return g_name;
Sean Callanan95e5c632012-02-17 00:53:45 +0000740}
741
Greg Claytonba812f42012-05-10 02:52:23 +0000742int DisassemblerLLVMC::OpInfoCallback (void *disassembler,
743 uint64_t pc,
744 uint64_t offset,
745 uint64_t size,
746 int tag_type,
747 void *tag_bug)
Sean Callanan95e5c632012-02-17 00:53:45 +0000748{
Greg Claytonba812f42012-05-10 02:52:23 +0000749 return static_cast<DisassemblerLLVMC*>(disassembler)->OpInfo (pc,
750 offset,
751 size,
752 tag_type,
753 tag_bug);
Sean Callanan95e5c632012-02-17 00:53:45 +0000754}
755
Greg Claytonba812f42012-05-10 02:52:23 +0000756const char *DisassemblerLLVMC::SymbolLookupCallback (void *disassembler,
757 uint64_t value,
758 uint64_t *type,
759 uint64_t pc,
760 const char **name)
Sean Callanan95e5c632012-02-17 00:53:45 +0000761{
Greg Claytonba812f42012-05-10 02:52:23 +0000762 return static_cast<DisassemblerLLVMC*>(disassembler)->SymbolLookup(value,
763 type,
764 pc,
765 name);
Sean Callanan95e5c632012-02-17 00:53:45 +0000766}
767
768int DisassemblerLLVMC::OpInfo (uint64_t PC,
769 uint64_t Offset,
770 uint64_t Size,
Greg Claytonba812f42012-05-10 02:52:23 +0000771 int tag_type,
772 void *tag_bug)
Sean Callanan95e5c632012-02-17 00:53:45 +0000773{
Greg Claytonba812f42012-05-10 02:52:23 +0000774 switch (tag_type)
Sean Callanan95e5c632012-02-17 00:53:45 +0000775 {
776 default:
777 break;
778 case 1:
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000779 memset (tag_bug, 0, sizeof(::LLVMOpInfo1));
Sean Callanan95e5c632012-02-17 00:53:45 +0000780 break;
781 }
782 return 0;
783}
784
Greg Claytonba812f42012-05-10 02:52:23 +0000785const char *DisassemblerLLVMC::SymbolLookup (uint64_t value,
786 uint64_t *type_ptr,
787 uint64_t pc,
788 const char **name)
Sean Callanan95e5c632012-02-17 00:53:45 +0000789{
Greg Claytonba812f42012-05-10 02:52:23 +0000790 if (*type_ptr)
791 {
792 if (m_exe_ctx && m_inst)
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000793 {
Greg Claytonba812f42012-05-10 02:52:23 +0000794 //std::string remove_this_prior_to_checkin;
Greg Claytonba812f42012-05-10 02:52:23 +0000795 Target *target = m_exe_ctx ? m_exe_ctx->GetTargetPtr() : NULL;
Greg Clayton3faf47c2013-03-28 23:42:53 +0000796 Address value_so_addr;
797 if (m_inst->UsingFileAddress())
Greg Claytonba812f42012-05-10 02:52:23 +0000798 {
799 ModuleSP module_sp(m_inst->GetAddress().GetModule());
800 if (module_sp)
Greg Clayton3faf47c2013-03-28 23:42:53 +0000801 module_sp->ResolveFileAddress(value, value_so_addr);
Greg Claytonba812f42012-05-10 02:52:23 +0000802 }
Greg Clayton3faf47c2013-03-28 23:42:53 +0000803 else if (target && !target->GetSectionLoadList().IsEmpty())
804 {
805 target->GetSectionLoadList().ResolveLoadAddress(value, value_so_addr);
806 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000807
Greg Clayton3faf47c2013-03-28 23:42:53 +0000808 if (value_so_addr.IsValid() && value_so_addr.GetSection())
Sean Callanan95e5c632012-02-17 00:53:45 +0000809 {
Sean Callanan95e5c632012-02-17 00:53:45 +0000810 StreamString ss;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000811
Greg Clayton3faf47c2013-03-28 23:42:53 +0000812 value_so_addr.Dump (&ss,
813 target,
Jason Molendaaff1b352014-10-10 23:07:36 +0000814 Address::DumpStyleResolvedDescriptionNoFunctionArguments,
Greg Clayton3faf47c2013-03-28 23:42:53 +0000815 Address::DumpStyleSectionNameOffset);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000816
Sean Callanan745af462012-03-22 20:04:23 +0000817 if (!ss.GetString().empty())
Greg Claytonba812f42012-05-10 02:52:23 +0000818 {
Jason Molendaaff1b352014-10-10 23:07:36 +0000819 // If Address::Dump returned a multi-line description, most commonly seen when we
820 // have multiple levels of inlined functions at an address, only show the first line.
821 std::string &str(ss.GetString());
822 size_t first_eol_char = str.find_first_of ("\r\n");
823 if (first_eol_char != std::string::npos)
824 {
825 str.erase (first_eol_char);
826 }
Greg Claytonba812f42012-05-10 02:52:23 +0000827 m_inst->AppendComment(ss.GetString());
828 }
Sean Callanan95e5c632012-02-17 00:53:45 +0000829 }
830 }
831 }
Greg Claytonba812f42012-05-10 02:52:23 +0000832
833 *type_ptr = LLVMDisassembler_ReferenceType_InOut_None;
834 *name = NULL;
835 return NULL;
Sean Callanan95e5c632012-02-17 00:53:45 +0000836}
837
838//------------------------------------------------------------------
839// PluginInterface protocol
840//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000841ConstString
Sean Callanan95e5c632012-02-17 00:53:45 +0000842DisassemblerLLVMC::GetPluginName()
843{
Sean Callanan95e5c632012-02-17 00:53:45 +0000844 return GetPluginNameStatic();
845}
846
847uint32_t
848DisassemblerLLVMC::GetPluginVersion()
849{
850 return 1;
851}