1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
|
//===-- DisassemblerLLVMC.cpp -----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "DisassemblerLLVMC.h"
#include "llvm-c/Disassembler.h"
#include "llvm/Support/TargetSelect.h"
#include "lldb/Core/Address.h"
#include "lldb/Core/DataExtractor.h"
#include "lldb/Core/Stream.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/StackFrame.h"
#include <regex.h>
using namespace lldb;
using namespace lldb_private;
class InstructionLLVMC : public lldb_private::Instruction
{
public:
InstructionLLVMC (DisassemblerLLVMC &disasm,
const lldb_private::Address &address,
lldb_private::AddressClass addr_class) :
Instruction(address, addr_class),
m_disasm(disasm),
m_is_valid(false),
m_no_comments(true),
m_comment_stream(),
m_does_branch(eLazyBoolCalculate)
{
}
virtual
~InstructionLLVMC ()
{
}
static void
PadToWidth (lldb_private::StreamString &ss,
int new_width)
{
int old_width = ss.GetSize();
if (old_width < new_width)
{
ss.Printf("%*s", new_width - old_width, "");
}
}
virtual void
Dump (lldb_private::Stream *s,
uint32_t max_opcode_byte_size,
bool show_address,
bool show_bytes,
const lldb_private::ExecutionContext* exe_ctx,
bool raw)
{
const size_t opcode_column_width = 7;
const size_t operand_column_width = 25;
StreamString ss;
ExecutionContextScope *exe_scope = NULL;
if ((!raw) && exe_ctx)
{
exe_scope = exe_ctx->GetBestExecutionContextScope();
DataExtractor extractor(m_raw_bytes.data(),
m_raw_bytes.size(),
m_disasm.GetArchitecture().GetByteOrder(),
m_disasm.GetArchitecture().GetAddressByteSize());
Parse <true> (m_address,
m_address_class,
extractor,
0,
exe_scope);
}
if (show_address)
{
m_address.Dump(&ss,
exe_scope,
Address::DumpStyleLoadAddress,
Address::DumpStyleModuleWithFileAddress,
0);
ss.PutCString(": ");
}
if (show_bytes)
{
if (m_opcode.GetType() == Opcode::eTypeBytes)
{
// x86_64 and i386 are the only ones that use bytes right now so
// pad out the byte dump to be able to always show 15 bytes (3 chars each)
// plus a space
if (max_opcode_byte_size > 0)
m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1);
else
m_opcode.Dump (&ss, 15 * 3 + 1);
}
else
{
// Else, we have ARM which can show up to a uint32_t 0x00000000 (10 spaces)
// plus two for padding...
if (max_opcode_byte_size > 0)
m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1);
else
m_opcode.Dump (&ss, 12);
}
}
int size_before_inst = ss.GetSize();
ss.PutCString(m_opcode_name.c_str());
PadToWidth(ss, size_before_inst + opcode_column_width);
ss.PutCString(m_mnemocics.c_str());
PadToWidth(ss, size_before_inst + opcode_column_width + operand_column_width);
if (!m_comment.empty())
{
ss.PutCString(" ; ");
ss.PutCString(m_comment.c_str());
}
ss.Flush();
s->PutCString(ss.GetData());
}
virtual bool
DoesBranch () const
{
return m_does_branch == eLazyBoolYes;
}
virtual size_t
Decode (const lldb_private::Disassembler &disassembler,
const lldb_private::DataExtractor &data,
uint32_t data_offset)
{
Parse <false> (m_address,
m_address_class,
data,
data_offset,
NULL);
return m_opcode.GetByteSize();
}
void
AddReferencedAddress (std::string &description)
{
if (m_no_comments)
m_comment_stream.PutCString(", ");
else
m_no_comments = true;
m_comment_stream.PutCString(description.c_str());
}
virtual void
CalculateMnemonicOperandsAndComment (lldb_private::ExecutionContextScope *exe_scope)
{
DataExtractor extractor(m_raw_bytes.data(),
m_raw_bytes.size(),
m_disasm.GetArchitecture().GetByteOrder(),
m_disasm.GetArchitecture().GetAddressByteSize());
Parse <true> (m_address,
m_address_class,
extractor,
0,
exe_scope);
}
bool
IsValid ()
{
return m_is_valid;
}
size_t
GetByteSize ()
{
return m_opcode.GetByteSize();
}
protected:
void PopulateOpcode (const DataExtractor &extractor,
uint32_t offset,
size_t inst_size)
{
llvm::Triple::ArchType arch = m_disasm.GetArchitecture().GetMachine();
switch (arch)
{
default:
case llvm::Triple::x86:
case llvm::Triple::x86_64:
m_opcode.SetOpcodeBytes(extractor.PeekData(offset, inst_size), inst_size);
break;
case llvm::Triple::arm:
case llvm::Triple::thumb:
switch (inst_size)
{
case 2:
{
m_opcode.SetOpcode16 (extractor.GetU16 (&offset));
break;
}
break;
case 4:
{
if (arch == llvm::Triple::arm &&
m_address_class == eAddressClassCodeAlternateISA)
{
// If it is a 32-bit THUMB instruction, we need to swap the upper & lower halves.
uint32_t orig_bytes = extractor.GetU32 (&offset);
uint16_t upper_bits = (orig_bytes >> 16) & ((1u << 16) - 1);
uint16_t lower_bits = orig_bytes & ((1u << 16) - 1);
uint32_t swapped = (lower_bits << 16) | upper_bits;
m_opcode.SetOpcode32 (swapped);
}
else
{
m_opcode.SetOpcode32 (extractor.GetU32 (&offset));
}
}
break;
default:
assert (!"Invalid ARM opcode size");
break;
}
break;
}
}
bool StringRepresentsBranch (const char *data, size_t size)
{
const char *cursor = data;
bool inWhitespace = true;
while (inWhitespace && cursor < data + size)
{
switch (*cursor)
{
default:
inWhitespace = false;
break;
case ' ':
break;
case '\t':
break;
}
if (inWhitespace)
++cursor;
}
if (cursor >= data + size)
return false;
llvm::Triple::ArchType arch = m_disasm.GetArchitecture().GetMachine();
switch (arch)
{
default:
return false;
case llvm::Triple::x86:
case llvm::Triple::x86_64:
switch (cursor[0])
{
default:
return false;
case 'j':
return true;
case 'c':
if (cursor[1] == 'a' &&
cursor[2] == 'l' &&
cursor[3] == 'l')
return true;
else
return false;
}
case llvm::Triple::arm:
case llvm::Triple::thumb:
switch (cursor[0])
{
default:
return false;
case 'b':
{
switch (cursor[1])
{
default:
return false;
case 'l':
case 'x':
case ' ':
case '\t':
return true;
}
return false;
}
case 'c':
{
switch (cursor[1])
{
default:
return false;
case 'b':
return true;
}
}
}
}
return false;
}
template <bool Reparse> bool Parse (const lldb_private::Address &address,
lldb_private::AddressClass addr_class,
const DataExtractor &extractor,
uint32_t data_offset,
lldb_private::ExecutionContextScope *exe_scope)
{
std::vector<char> out_string(256);
const uint8_t *data_start = extractor.GetDataStart();
m_disasm.Lock(this, exe_scope);
::LLVMDisasmContextRef disasm_context;
if (addr_class == eAddressClassCodeAlternateISA)
disasm_context = m_disasm.m_alternate_disasm_context;
else
disasm_context = m_disasm.m_disasm_context;
m_comment_stream.Clear();
size_t inst_size = ::LLVMDisasmInstruction(disasm_context,
const_cast<uint8_t*>(data_start) + data_offset,
extractor.GetByteSize() - data_offset,
address.GetFileAddress(),
out_string.data(),
out_string.size());
if (m_does_branch == eLazyBoolCalculate)
m_does_branch = (StringRepresentsBranch (out_string.data(), out_string.size()) ?
eLazyBoolYes : eLazyBoolNo);
m_comment_stream.Flush();
m_no_comments = false;
m_comment.swap(m_comment_stream.GetString());
m_disasm.Unlock();
if (Reparse)
{
if (inst_size != m_raw_bytes.size())
return false;
}
else
{
if (!inst_size)
return false;
PopulateOpcode(extractor, data_offset, inst_size);
m_raw_bytes.resize(inst_size);
memcpy(m_raw_bytes.data(), data_start + data_offset, inst_size);
if (!s_regex_compiled)
{
::regcomp(&s_regex, "[ \t]*([^ ^\t]+)[ \t]*([^ ^\t].*)?", REG_EXTENDED);
s_regex_compiled = true;
}
::regmatch_t matches[3];
const char *out_data = out_string.data();
if (!::regexec(&s_regex, out_data, sizeof(matches) / sizeof(::regmatch_t), matches, 0))
{
if (matches[1].rm_so != -1)
m_opcode_name.assign(out_data + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so);
if (matches[2].rm_so != -1)
m_mnemocics.assign(out_data + matches[2].rm_so, matches[2].rm_eo - matches[2].rm_so);
}
m_is_valid = true;
}
return true;
}
bool m_is_valid;
DisassemblerLLVMC &m_disasm;
std::vector<uint8_t> m_raw_bytes;
bool m_no_comments;
StreamString m_comment_stream;
LazyBool m_does_branch;
static bool s_regex_compiled;
static ::regex_t s_regex;
};
bool InstructionLLVMC::s_regex_compiled = false;
::regex_t InstructionLLVMC::s_regex;
Disassembler *
DisassemblerLLVMC::CreateInstance (const ArchSpec &arch)
{
std::auto_ptr<DisassemblerLLVMC> disasm_ap (new DisassemblerLLVMC(arch));
if (disasm_ap.get() && disasm_ap->IsValid())
return disasm_ap.release();
return NULL;
}
DisassemblerLLVMC::DisassemblerLLVMC (const ArchSpec &arch) :
Disassembler(arch),
m_disasm_context(NULL),
m_alternate_disasm_context(NULL)
{
m_disasm_context = ::LLVMCreateDisasm(arch.GetTriple().getTriple().c_str(),
(void*)this,
/*TagType=*/1,
NULL,
DisassemblerLLVMC::SymbolLookupCallback);
if (arch.GetTriple().getArch() == llvm::Triple::arm)
{
m_alternate_disasm_context = ::LLVMCreateDisasm("thumbv7-apple-darwin",
(void*)this,
/*TagType=*/1,
NULL,
DisassemblerLLVMC::SymbolLookupCallback);
}
}
DisassemblerLLVMC::~DisassemblerLLVMC()
{
}
size_t
DisassemblerLLVMC::DecodeInstructions (const Address &base_addr,
const DataExtractor& data,
uint32_t data_offset,
uint32_t num_instructions,
bool append)
{
if (!append)
m_instruction_list.Clear();
if (!IsValid())
return 0;
uint32_t data_cursor = data_offset;
size_t data_byte_size = data.GetByteSize();
uint32_t instructions_parsed = 0;
uint64_t instruction_pointer = base_addr.GetFileAddress();
std::vector<char> out_string(256);
while (data_offset < data_byte_size && instructions_parsed < num_instructions)
{
Address instr_address = base_addr;
instr_address.Slide(data_cursor);
AddressClass address_class = eAddressClassUnknown;
if (m_alternate_disasm_context)
address_class = instr_address.GetAddressClass ();
InstructionSP inst_sp(new InstructionLLVMC(*this,
instr_address,
address_class));
if (!inst_sp)
return data_cursor - data_offset;
uint32_t inst_size = inst_sp->Decode(*this, data, data_cursor);
if (!inst_size)
return data_cursor - data_offset;
m_instruction_list.Append(inst_sp);
instruction_pointer += inst_size;
data_cursor += inst_size;
instructions_parsed++;
}
return data_cursor - data_offset;
}
void
DisassemblerLLVMC::Initialize()
{
PluginManager::RegisterPlugin (GetPluginNameStatic(),
GetPluginDescriptionStatic(),
CreateInstance);
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmParsers();
llvm::InitializeAllDisassemblers();
}
void
DisassemblerLLVMC::Terminate()
{
PluginManager::UnregisterPlugin (CreateInstance);
}
const char *
DisassemblerLLVMC::GetPluginNameStatic()
{
return "llvm";
}
const char *
DisassemblerLLVMC::GetPluginDescriptionStatic()
{
return "Disassembler that uses LLVM opcode tables to disassemble i386, x86_64 and ARM.";
}
int DisassemblerLLVMC::OpInfoCallback (void *DisInfo,
uint64_t PC,
uint64_t Offset,
uint64_t Size,
int TagType,
void *TagBug)
{
return static_cast<DisassemblerLLVMC*>(DisInfo)->OpInfo(PC,
Offset,
Size,
TagType,
TagBug);
}
const char *DisassemblerLLVMC::SymbolLookupCallback(void *DisInfo,
uint64_t ReferenceValue,
uint64_t *ReferenceType,
uint64_t ReferencePC,
const char **ReferenceName)
{
return static_cast<DisassemblerLLVMC*>(DisInfo)->SymbolLookup(ReferenceValue,
ReferenceType,
ReferencePC,
ReferenceName);
}
int DisassemblerLLVMC::OpInfo (uint64_t PC,
uint64_t Offset,
uint64_t Size,
int TagType,
void *TagBug)
{
switch (TagType)
{
default:
break;
case 1:
bzero (TagBug, sizeof(::LLVMOpInfo1));
break;
}
return 0;
}
const char *DisassemblerLLVMC::SymbolLookup (uint64_t ReferenceValue,
uint64_t *ReferenceType,
uint64_t ReferencePC,
const char **ReferenceName)
{
const char *result_name = NULL;
uint64_t result_reference_type = LLVMDisassembler_ReferenceType_InOut_None;
const char *result_referred_name = NULL;
if (m_exe_scope && m_inst)
{
Address reference_address;
TargetSP target_sp (m_exe_scope->CalculateTarget());
Target *target = target_sp.get();
if (target)
{
if (!target->GetSectionLoadList().IsEmpty())
target->GetSectionLoadList().ResolveLoadAddress(ReferenceValue, reference_address);
else
target->GetImages().ResolveFileAddress(ReferenceValue, reference_address);
if (reference_address.IsValid() && reference_address.GetSection())
{
StreamString ss;
reference_address.Dump (&ss,
target,
Address::DumpStyleResolvedDescriptionNoModule,
Address::DumpStyleSectionNameOffset);
m_inst->AddReferencedAddress(ss.GetString());
}
}
}
*ReferenceType = result_reference_type;
*ReferenceName = result_referred_name;
return result_name;
}
//------------------------------------------------------------------
// PluginInterface protocol
//------------------------------------------------------------------
const char *
DisassemblerLLVMC::GetPluginName()
{
return "DisassemblerLLVMC";
}
const char *
DisassemblerLLVMC::GetShortPluginName()
{
return GetPluginNameStatic();
}
uint32_t
DisassemblerLLVMC::GetPluginVersion()
{
return 1;
}
|