blob: 59e6d9a4bb48f44773895c2a43c4b7d60d3f20bf [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- DisassemblerLLVM.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 "DisassemblerLLVM.h"
11
12#include "llvm-c/EnhancedDisassembly.h"
13
14#include "lldb/Core/Address.h"
15#include "lldb/Core/DataExtractor.h"
16#include "lldb/Core/Disassembler.h"
17#include "lldb/Core/Module.h"
18#include "lldb/Core/PluginManager.h"
19#include "lldb/Core/Stream.h"
20#include "lldb/Core/StreamString.h"
21#include "lldb/Symbol/SymbolContext.h"
22
23#include "lldb/Target/ExecutionContext.h"
24#include "lldb/Target/Process.h"
25#include "lldb/Target/RegisterContext.h"
26#include "lldb/Target/Target.h"
27
Greg Claytonb01000f2011-01-17 03:46:26 +000028#include <assert.h>
Chris Lattner24943d22010-06-08 16:52:24 +000029
30using namespace lldb;
31using namespace lldb_private;
32
33
Greg Claytonb1888f22011-03-19 01:12:21 +000034static int
Greg Clayton7bc39082011-03-24 23:53:38 +000035DataExtractorByteReader (uint8_t *byte, uint64_t address, void *arg)
Chris Lattner24943d22010-06-08 16:52:24 +000036{
37 DataExtractor &extractor = *((DataExtractor *)arg);
38
39 if (extractor.ValidOffset(address))
40 {
41 *byte = *(extractor.GetDataStart() + address);
42 return 0;
43 }
44 else
45 {
46 return -1;
47 }
48}
49
50namespace {
51 struct RegisterReaderArg {
52 const lldb::addr_t instructionPointer;
53 const EDDisassemblerRef disassembler;
54
55 RegisterReaderArg(lldb::addr_t ip,
56 EDDisassemblerRef dis) :
57 instructionPointer(ip),
58 disassembler(dis)
59 {
60 }
61 };
62}
63
64static int IPRegisterReader(uint64_t *value, unsigned regID, void* arg)
65{
66 uint64_t instructionPointer = ((RegisterReaderArg*)arg)->instructionPointer;
67 EDDisassemblerRef disassembler = ((RegisterReaderArg*)arg)->disassembler;
68
Greg Claytonb1888f22011-03-19 01:12:21 +000069 if (EDRegisterIsProgramCounter(disassembler, regID)) {
Chris Lattner24943d22010-06-08 16:52:24 +000070 *value = instructionPointer;
71 return 0;
72 }
73
74 return -1;
75}
76
Greg Clayton889fbd02011-03-26 19:14:58 +000077DisassemblerLLVM::InstructionLLVM::InstructionLLVM (const Address &addr,
78 AddressClass addr_class,
79 EDDisassemblerRef disassembler) :
80 Instruction (addr, addr_class),
Chris Lattner24943d22010-06-08 16:52:24 +000081 m_disassembler (disassembler)
82{
83}
84
Greg Clayton5c4c7462010-10-06 03:09:58 +000085DisassemblerLLVM::InstructionLLVM::~InstructionLLVM()
Chris Lattner24943d22010-06-08 16:52:24 +000086{
87}
88
89static void
90PadString(Stream *s, const std::string &str, size_t width)
91{
92 int diff = width - str.length();
93
94 if (diff > 0)
95 s->Printf("%s%*.*s", str.c_str(), diff, diff, "");
96 else
97 s->Printf("%s ", str.c_str());
98}
99
100void
Greg Clayton5c4c7462010-10-06 03:09:58 +0000101DisassemblerLLVM::InstructionLLVM::Dump
Chris Lattner24943d22010-06-08 16:52:24 +0000102(
103 Stream *s,
Greg Clayton889fbd02011-03-26 19:14:58 +0000104 uint32_t max_opcode_byte_size,
Greg Clayton5c4c7462010-10-06 03:09:58 +0000105 bool show_address,
Greg Clayton149731c2011-03-25 18:03:16 +0000106 bool show_bytes,
Greg Clayton5c4c7462010-10-06 03:09:58 +0000107 const lldb_private::ExecutionContext* exe_ctx,
Chris Lattner24943d22010-06-08 16:52:24 +0000108 bool raw
109)
110{
111 const size_t opcodeColumnWidth = 7;
112 const size_t operandColumnWidth = 25;
113
Greg Clayton5c4c7462010-10-06 03:09:58 +0000114 ExecutionContextScope *exe_scope = NULL;
115 if (exe_ctx)
116 exe_scope = exe_ctx->GetBestExecutionContextScope();
117
Chris Lattner24943d22010-06-08 16:52:24 +0000118 // If we have an address, print it out
Sean Callanan91557b02010-11-10 01:38:28 +0000119 if (GetAddress().IsValid() && show_address)
Greg Clayton70436352010-06-30 23:03:03 +0000120 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000121 if (GetAddress().Dump (s,
122 exe_scope,
123 Address::DumpStyleLoadAddress,
124 Address::DumpStyleModuleWithFileAddress,
125 0))
Greg Clayton70436352010-06-30 23:03:03 +0000126 s->PutCString(": ");
127 }
Chris Lattner24943d22010-06-08 16:52:24 +0000128
129 // If we are supposed to show bytes, "bytes" will be non-NULL.
Greg Clayton149731c2011-03-25 18:03:16 +0000130 if (show_bytes)
Chris Lattner24943d22010-06-08 16:52:24 +0000131 {
Greg Clayton149731c2011-03-25 18:03:16 +0000132 if (m_opcode.GetType() == Opcode::eTypeBytes)
Chris Lattner24943d22010-06-08 16:52:24 +0000133 {
Greg Clayton149731c2011-03-25 18:03:16 +0000134 // x86_64 and i386 are the only ones that use bytes right now so
135 // pad out the byte dump to be able to always show 15 bytes (3 chars each)
136 // plus a space
Greg Clayton889fbd02011-03-26 19:14:58 +0000137 if (max_opcode_byte_size > 0)
138 m_opcode.Dump (s, max_opcode_byte_size * 3 + 1);
139 else
140 m_opcode.Dump (s, 15 * 3 + 1);
Greg Clayton149731c2011-03-25 18:03:16 +0000141 }
142 else
143 {
144 // Else, we have ARM which can show up to a uint32_t 0x00000000 (10 spaces)
145 // plus two for padding...
Greg Clayton889fbd02011-03-26 19:14:58 +0000146 if (max_opcode_byte_size > 0)
147 m_opcode.Dump (s, max_opcode_byte_size * 3 + 1);
148 else
149 m_opcode.Dump (s, 12);
Chris Lattner24943d22010-06-08 16:52:24 +0000150 }
151 }
152
153 int numTokens = EDNumTokens(m_inst);
154
155 int currentOpIndex = -1;
156
Sean Callanan8541f2f2010-07-23 02:19:15 +0000157 std::auto_ptr<RegisterReaderArg> rra;
158
159 if (!raw)
160 {
161 addr_t base_addr = LLDB_INVALID_ADDRESS;
Greg Clayton5c4c7462010-10-06 03:09:58 +0000162 if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty())
163 base_addr = GetAddress().GetLoadAddress (exe_ctx->target);
Sean Callanan8541f2f2010-07-23 02:19:15 +0000164 if (base_addr == LLDB_INVALID_ADDRESS)
Greg Clayton5c4c7462010-10-06 03:09:58 +0000165 base_addr = GetAddress().GetFileAddress ();
Sean Callanan8541f2f2010-07-23 02:19:15 +0000166
167 rra.reset(new RegisterReaderArg(base_addr + EDInstByteSize(m_inst), m_disassembler));
168 }
Chris Lattner24943d22010-06-08 16:52:24 +0000169
170 bool printTokenized = false;
171
172 if (numTokens != -1)
173 {
174 printTokenized = true;
175
176 // Handle the opcode column.
177
178 StreamString opcode;
179
180 int tokenIndex = 0;
181
182 EDTokenRef token;
183 const char *tokenStr;
184
185 if (EDGetToken(&token, m_inst, tokenIndex))
186 printTokenized = false;
187
188 if (!printTokenized || !EDTokenIsOpcode(token))
189 printTokenized = false;
190
191 if (!printTokenized || EDGetTokenString(&tokenStr, token))
192 printTokenized = false;
193
194 // Put the token string into our opcode string
195 opcode.PutCString(tokenStr);
196
197 // If anything follows, it probably starts with some whitespace. Skip it.
198
199 tokenIndex++;
200
201 if (printTokenized && tokenIndex < numTokens)
202 {
203 if(!printTokenized || EDGetToken(&token, m_inst, tokenIndex))
204 printTokenized = false;
205
206 if(!printTokenized || !EDTokenIsWhitespace(token))
207 printTokenized = false;
208 }
209
210 tokenIndex++;
211
212 // Handle the operands and the comment.
213
214 StreamString operands;
215 StreamString comment;
216
217 if (printTokenized)
218 {
219 bool show_token;
220
221 for (; tokenIndex < numTokens; ++tokenIndex)
222 {
223 if (EDGetToken(&token, m_inst, tokenIndex))
224 return;
225
226 if (raw)
227 {
228 show_token = true;
229 }
230 else
231 {
232 int operandIndex = EDOperandIndexForToken(token);
233
234 if (operandIndex >= 0)
235 {
236 if (operandIndex != currentOpIndex)
237 {
238 show_token = true;
239
240 currentOpIndex = operandIndex;
241 EDOperandRef operand;
242
243 if (!EDGetOperand(&operand, m_inst, currentOpIndex))
244 {
245 if (EDOperandIsMemory(operand))
246 {
247 uint64_t operand_value;
248
Sean Callanan8541f2f2010-07-23 02:19:15 +0000249 if (!EDEvaluateOperand(&operand_value, operand, IPRegisterReader, rra.get()))
Chris Lattner24943d22010-06-08 16:52:24 +0000250 {
251 if (EDInstIsBranch(m_inst))
252 {
253 operands.Printf("0x%llx ", operand_value);
254 show_token = false;
255 }
256 else
257 {
258 // Put the address value into the comment
259 comment.Printf("0x%llx ", operand_value);
260 }
261
262 lldb_private::Address so_addr;
Greg Clayton5c4c7462010-10-06 03:09:58 +0000263 if (exe_ctx && exe_ctx->target && !exe_ctx->target->GetSectionLoadList().IsEmpty())
Chris Lattner24943d22010-06-08 16:52:24 +0000264 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000265 if (exe_ctx->target->GetSectionLoadList().ResolveLoadAddress (operand_value, so_addr))
Greg Claytoncf7f1ad2010-07-01 01:26:43 +0000266 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
Greg Clayton70436352010-06-30 23:03:03 +0000267 }
Greg Clayton5c4c7462010-10-06 03:09:58 +0000268 else
Greg Clayton70436352010-06-30 23:03:03 +0000269 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000270 Module *module = GetAddress().GetModule();
Greg Clayton70436352010-06-30 23:03:03 +0000271 if (module)
Chris Lattner24943d22010-06-08 16:52:24 +0000272 {
Greg Clayton70436352010-06-30 23:03:03 +0000273 if (module->ResolveFileAddress (operand_value, so_addr))
Greg Claytoncf7f1ad2010-07-01 01:26:43 +0000274 so_addr.Dump(&comment, exe_scope, Address::DumpStyleResolvedDescriptionNoModule, Address::DumpStyleSectionNameOffset);
Chris Lattner24943d22010-06-08 16:52:24 +0000275 }
276 }
Greg Clayton70436352010-06-30 23:03:03 +0000277
Chris Lattner24943d22010-06-08 16:52:24 +0000278 } // EDEvaluateOperand
279 } // EDOperandIsMemory
280 } // EDGetOperand
281 } // operandIndex != currentOpIndex
282 } // operandIndex >= 0
283 } // else(raw)
284
285 if (show_token)
286 {
287 if(EDGetTokenString(&tokenStr, token))
288 {
289 printTokenized = false;
290 break;
291 }
292
293 operands.PutCString(tokenStr);
294 }
295 } // for (tokenIndex)
296
297 if (printTokenized)
298 {
299 if (operands.GetString().empty())
300 {
301 s->PutCString(opcode.GetString().c_str());
302 }
303 else
304 {
305 PadString(s, opcode.GetString(), opcodeColumnWidth);
306
307 if (comment.GetString().empty())
308 {
309 s->PutCString(operands.GetString().c_str());
310 }
311 else
312 {
313 PadString(s, operands.GetString(), operandColumnWidth);
314
315 s->PutCString("; ");
316 s->PutCString(comment.GetString().c_str());
317 } // else (comment.GetString().empty())
318 } // else (operands.GetString().empty())
319 } // printTokenized
320 } // for (tokenIndex)
321 } // numTokens != -1
322
323 if (!printTokenized)
324 {
325 const char *str;
326
327 if (EDGetInstString(&str, m_inst))
328 return;
329 else
330 s->PutCString(str);
331 }
332}
333
334bool
Greg Clayton5c4c7462010-10-06 03:09:58 +0000335DisassemblerLLVM::InstructionLLVM::DoesBranch() const
Chris Lattner24943d22010-06-08 16:52:24 +0000336{
337 return EDInstIsBranch(m_inst);
338}
339
340size_t
Greg Clayton889fbd02011-03-26 19:14:58 +0000341DisassemblerLLVM::InstructionLLVM::Decode (const Disassembler &disassembler,
342 const lldb_private::DataExtractor &data,
343 uint32_t data_offset)
Chris Lattner24943d22010-06-08 16:52:24 +0000344{
345 if (EDCreateInsts(&m_inst, 1, m_disassembler, DataExtractorByteReader, data_offset, (void*)(&data)))
Greg Clayton7bc39082011-03-24 23:53:38 +0000346 {
347 const int byte_size = EDInstByteSize(m_inst);
348 uint32_t offset = data_offset;
349 // Make a copy of the opcode in m_opcode
350 switch (disassembler.GetArchitecture().GetMachine())
351 {
352 case llvm::Triple::x86:
353 case llvm::Triple::x86_64:
354 m_opcode.SetOpcodeBytes (data.PeekData (data_offset, byte_size), byte_size);
355 break;
356
357 case llvm::Triple::arm:
Greg Clayton7bc39082011-03-24 23:53:38 +0000358 case llvm::Triple::thumb:
Greg Clayton149731c2011-03-25 18:03:16 +0000359 switch (byte_size)
360 {
361 case 2:
362 m_opcode.SetOpcode16 (data.GetU16 (&offset));
363 break;
364
365 case 4:
Greg Clayton7bc39082011-03-24 23:53:38 +0000366 m_opcode.SetOpcode32 (data.GetU32 (&offset));
Greg Clayton149731c2011-03-25 18:03:16 +0000367 break;
368
369 default:
370 assert (!"Invalid ARM opcode size");
371 break;
372 }
Greg Clayton7bc39082011-03-24 23:53:38 +0000373 break;
374
375 default:
376 assert (!"This shouldn't happen since we control the architecture we allow DisassemblerLLVM to be created for");
377 break;
378 }
379 return byte_size;
380 }
Chris Lattner24943d22010-06-08 16:52:24 +0000381 else
382 return 0;
383}
384
Chris Lattner24943d22010-06-08 16:52:24 +0000385static inline EDAssemblySyntax_t
Greg Claytoncf015052010-06-11 03:25:34 +0000386SyntaxForArchSpec (const ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +0000387{
Greg Clayton940b1032011-02-23 00:35:02 +0000388 switch (arch.GetMachine ())
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000389 {
Greg Clayton940b1032011-02-23 00:35:02 +0000390 case llvm::Triple::x86:
391 case llvm::Triple::x86_64:
Chris Lattner24943d22010-06-08 16:52:24 +0000392 return kEDAssemblySyntaxX86ATT;
Sean Callanand151c8a2011-03-09 01:02:51 +0000393 case llvm::Triple::arm:
Greg Clayton889fbd02011-03-26 19:14:58 +0000394 case llvm::Triple::thumb:
Sean Callanand151c8a2011-03-09 01:02:51 +0000395 return kEDAssemblySyntaxARMUAL;
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000396 default:
397 break;
398 }
Greg Claytoncf015052010-06-11 03:25:34 +0000399 return (EDAssemblySyntax_t)0; // default
Chris Lattner24943d22010-06-08 16:52:24 +0000400}
401
402Disassembler *
403DisassemblerLLVM::CreateInstance(const ArchSpec &arch)
404{
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000405 std::auto_ptr<DisassemblerLLVM> disasm_ap (new DisassemblerLLVM(arch));
406
407 if (disasm_ap->IsValid())
408 return disasm_ap.release();
Chris Lattner24943d22010-06-08 16:52:24 +0000409
Greg Claytoncf015052010-06-11 03:25:34 +0000410 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000411}
412
413DisassemblerLLVM::DisassemblerLLVM(const ArchSpec &arch) :
Greg Claytonb01000f2011-01-17 03:46:26 +0000414 Disassembler (arch),
Greg Claytonb1888f22011-03-19 01:12:21 +0000415 m_disassembler (NULL),
416 m_disassembler_thumb (NULL) // For ARM only
Chris Lattner24943d22010-06-08 16:52:24 +0000417{
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000418 const std::string &arch_triple = arch.GetTriple().str();
419 if (!arch_triple.empty())
Greg Claytoncf015052010-06-11 03:25:34 +0000420 {
Greg Clayton5e4f4a22011-02-16 00:00:43 +0000421 if (EDGetDisassembler(&m_disassembler, arch_triple.c_str(), SyntaxForArchSpec (arch)))
422 m_disassembler = NULL;
Greg Claytonb1888f22011-03-19 01:12:21 +0000423 llvm::Triple::ArchType llvm_arch = arch.GetTriple().getArch();
Greg Clayton889fbd02011-03-26 19:14:58 +0000424 // Don't have the lldb::Triple::thumb architecture here. If someone specifies
425 // "thumb" as the architecture, we want a thumb only disassembler. But if any
426 // architecture starting with "arm" if specified, we want to auto detect the
427 // arm/thumb code automatically using the AddressClass from section offset
428 // addresses.
Greg Claytonb1888f22011-03-19 01:12:21 +0000429 if (llvm_arch == llvm::Triple::arm)
430 {
431 if (EDGetDisassembler(&m_disassembler_thumb, "thumb-apple-darwin", kEDAssemblySyntaxARMUAL))
432 m_disassembler_thumb = NULL;
433 }
Greg Claytoncf015052010-06-11 03:25:34 +0000434 }
Chris Lattner24943d22010-06-08 16:52:24 +0000435}
436
437DisassemblerLLVM::~DisassemblerLLVM()
438{
439}
440
441size_t
Greg Clayton70436352010-06-30 23:03:03 +0000442DisassemblerLLVM::DecodeInstructions
Chris Lattner24943d22010-06-08 16:52:24 +0000443(
Greg Clayton5c4c7462010-10-06 03:09:58 +0000444 const Address &base_addr,
Chris Lattner24943d22010-06-08 16:52:24 +0000445 const DataExtractor& data,
446 uint32_t data_offset,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000447 uint32_t num_instructions,
448 bool append
Chris Lattner24943d22010-06-08 16:52:24 +0000449)
450{
Greg Claytonb01000f2011-01-17 03:46:26 +0000451 if (m_disassembler == NULL)
452 return 0;
453
Chris Lattner24943d22010-06-08 16:52:24 +0000454 size_t total_inst_byte_size = 0;
455
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000456 if (!append)
457 m_instruction_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000458
459 while (data.ValidOffset(data_offset) && num_instructions)
460 {
Greg Clayton5c4c7462010-10-06 03:09:58 +0000461 Address inst_addr (base_addr);
462 inst_addr.Slide(data_offset);
Greg Claytonb1888f22011-03-19 01:12:21 +0000463
464 bool use_thumb = false;
465 // If we have a thumb disassembler, then we have an ARM architecture
466 // so we need to check what the instruction address class is to make
467 // sure we shouldn't be disassembling as thumb...
Greg Clayton889fbd02011-03-26 19:14:58 +0000468 AddressClass inst_address_class = eAddressClassInvalid;
Greg Claytonb1888f22011-03-19 01:12:21 +0000469 if (m_disassembler_thumb)
470 {
Greg Clayton889fbd02011-03-26 19:14:58 +0000471 inst_address_class = inst_addr.GetAddressClass ();
472 if (inst_address_class == eAddressClassCodeAlternateISA)
Greg Claytonb1888f22011-03-19 01:12:21 +0000473 use_thumb = true;
474 }
Greg Clayton7bc39082011-03-24 23:53:38 +0000475 InstructionSP inst_sp (new InstructionLLVM (inst_addr,
Greg Clayton889fbd02011-03-26 19:14:58 +0000476 inst_address_class,
Greg Clayton7bc39082011-03-24 23:53:38 +0000477 use_thumb ? m_disassembler_thumb : m_disassembler));
Chris Lattner24943d22010-06-08 16:52:24 +0000478
Greg Clayton889fbd02011-03-26 19:14:58 +0000479 size_t inst_byte_size = inst_sp->Decode (*this, data, data_offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000480
481 if (inst_byte_size == 0)
482 break;
483
Greg Clayton5c4c7462010-10-06 03:09:58 +0000484 m_instruction_list.Append (inst_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000485
486 total_inst_byte_size += inst_byte_size;
487 data_offset += inst_byte_size;
488 num_instructions--;
489 }
490
491 return total_inst_byte_size;
492}
493
494void
495DisassemblerLLVM::Initialize()
496{
497 PluginManager::RegisterPlugin (GetPluginNameStatic(),
498 GetPluginDescriptionStatic(),
499 CreateInstance);
500}
501
502void
503DisassemblerLLVM::Terminate()
504{
505 PluginManager::UnregisterPlugin (CreateInstance);
506}
507
508
509const char *
510DisassemblerLLVM::GetPluginNameStatic()
511{
Greg Clayton149731c2011-03-25 18:03:16 +0000512 return "llvm";
Chris Lattner24943d22010-06-08 16:52:24 +0000513}
514
515const char *
516DisassemblerLLVM::GetPluginDescriptionStatic()
517{
Greg Clayton149731c2011-03-25 18:03:16 +0000518 return "Disassembler that uses LLVM opcode tables to disassemble i386, x86_64 and ARM.";
Chris Lattner24943d22010-06-08 16:52:24 +0000519}
520
521//------------------------------------------------------------------
522// PluginInterface protocol
523//------------------------------------------------------------------
524const char *
525DisassemblerLLVM::GetPluginName()
526{
527 return "DisassemblerLLVM";
528}
529
530const char *
531DisassemblerLLVM::GetShortPluginName()
532{
533 return GetPluginNameStatic();
534}
535
536uint32_t
537DisassemblerLLVM::GetPluginVersion()
538{
539 return 1;
540}
541