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
|
//===-EDDisassembler.cpp - LLVM Enhanced Disassembler ---------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the Enhanced Disassembly library's disassembler class.
// The disassembler is responsible for vending individual instructions according
// to a given architecture and disassembly syntax.
//
//===----------------------------------------------------------------------===//
#include "EDDisassembler.h"
#include "EDInst.h"
#include "llvm/MC/EDInstInfo.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCDisassembler.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCInstPrinter.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCParser/AsmLexer.h"
#include "llvm/MC/MCParser/MCAsmParser.h"
#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Target/TargetAsmLexer.h"
#include "llvm/Target/TargetAsmParser.h"
#include "llvm/Target/TargetRegistry.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetRegisterInfo.h"
#include "llvm/Target/TargetSelect.h"
using namespace llvm;
bool EDDisassembler::sInitialized = false;
EDDisassembler::DisassemblerMap_t EDDisassembler::sDisassemblers;
struct TripleMap {
Triple::ArchType Arch;
const char *String;
};
static struct TripleMap triplemap[] = {
{ Triple::x86, "i386-unknown-unknown" },
{ Triple::x86_64, "x86_64-unknown-unknown" },
{ Triple::arm, "arm-unknown-unknown" },
{ Triple::thumb, "thumb-unknown-unknown" },
{ Triple::InvalidArch, NULL, }
};
/// infoFromArch - Returns the TripleMap corresponding to a given architecture,
/// or NULL if there is an error
///
/// @arg arch - The Triple::ArchType for the desired architecture
static const char *tripleFromArch(Triple::ArchType arch) {
unsigned int infoIndex;
for (infoIndex = 0; triplemap[infoIndex].String != NULL; ++infoIndex) {
if (arch == triplemap[infoIndex].Arch)
return triplemap[infoIndex].String;
}
return NULL;
}
/// getLLVMSyntaxVariant - gets the constant to use to get an assembly printer
/// for the desired assembly syntax, suitable for passing to
/// Target::createMCInstPrinter()
///
/// @arg arch - The target architecture
/// @arg syntax - The assembly syntax in sd form
static int getLLVMSyntaxVariant(Triple::ArchType arch,
EDDisassembler::AssemblySyntax syntax) {
switch (syntax) {
default:
return -1;
// Mappings below from X86AsmPrinter.cpp
case EDDisassembler::kEDAssemblySyntaxX86ATT:
if (arch == Triple::x86 || arch == Triple::x86_64)
return 0;
else
return -1;
case EDDisassembler::kEDAssemblySyntaxX86Intel:
if (arch == Triple::x86 || arch == Triple::x86_64)
return 1;
else
return -1;
case EDDisassembler::kEDAssemblySyntaxARMUAL:
if (arch == Triple::arm || arch == Triple::thumb)
return 0;
else
return -1;
}
}
void EDDisassembler::initialize() {
if (sInitialized)
return;
sInitialized = true;
InitializeAllTargetInfos();
InitializeAllTargets();
InitializeAllAsmPrinters();
InitializeAllAsmParsers();
InitializeAllDisassemblers();
}
#undef BRINGUP_TARGET
EDDisassembler *EDDisassembler::getDisassembler(Triple::ArchType arch,
AssemblySyntax syntax) {
CPUKey key;
key.Arch = arch;
key.Syntax = syntax;
EDDisassembler::DisassemblerMap_t::iterator i = sDisassemblers.find(key);
if (i != sDisassemblers.end()) {
return i->second;
} else {
EDDisassembler* sdd = new EDDisassembler(key);
if (!sdd->valid()) {
delete sdd;
return NULL;
}
sDisassemblers[key] = sdd;
return sdd;
}
return NULL;
}
EDDisassembler *EDDisassembler::getDisassembler(StringRef str,
AssemblySyntax syntax) {
return getDisassembler(Triple(str).getArch(), syntax);
}
EDDisassembler::EDDisassembler(CPUKey &key) :
Valid(false),
HasSemantics(false),
ErrorStream(nulls()),
Key(key) {
const char *triple = tripleFromArch(key.Arch);
if (!triple)
return;
LLVMSyntaxVariant = getLLVMSyntaxVariant(key.Arch, key.Syntax);
if (LLVMSyntaxVariant < 0)
return;
std::string tripleString(triple);
std::string errorString;
Tgt = TargetRegistry::lookupTarget(tripleString,
errorString);
if (!Tgt)
return;
std::string featureString;
TargetMachine.reset(Tgt->createTargetMachine(tripleString,
featureString));
const TargetRegisterInfo *registerInfo = TargetMachine->getRegisterInfo();
if (!registerInfo)
return;
initMaps(*registerInfo);
AsmInfo.reset(Tgt->createAsmInfo(tripleString));
if (!AsmInfo)
return;
Disassembler.reset(Tgt->createMCDisassembler());
if (!Disassembler)
return;
InstInfos = Disassembler->getEDInfo();
InstString.reset(new std::string);
InstStream.reset(new raw_string_ostream(*InstString));
InstPrinter.reset(Tgt->createMCInstPrinter(*TargetMachine, LLVMSyntaxVariant,
*AsmInfo));
if (!InstPrinter)
return;
GenericAsmLexer.reset(new AsmLexer(*AsmInfo));
SpecificAsmLexer.reset(Tgt->createAsmLexer(*AsmInfo));
SpecificAsmLexer->InstallLexer(*GenericAsmLexer);
initMaps(*TargetMachine->getRegisterInfo());
Valid = true;
}
EDDisassembler::~EDDisassembler() {
if (!valid())
return;
}
namespace {
/// EDMemoryObject - a subclass of MemoryObject that allows use of a callback
/// as provided by the sd interface. See MemoryObject.
class EDMemoryObject : public llvm::MemoryObject {
private:
EDByteReaderCallback Callback;
void *Arg;
public:
EDMemoryObject(EDByteReaderCallback callback,
void *arg) : Callback(callback), Arg(arg) { }
~EDMemoryObject() { }
uint64_t getBase() const { return 0x0; }
uint64_t getExtent() const { return (uint64_t)-1; }
int readByte(uint64_t address, uint8_t *ptr) const {
if (!Callback)
return -1;
if (Callback(ptr, address, Arg))
return -1;
return 0;
}
};
}
EDInst *EDDisassembler::createInst(EDByteReaderCallback byteReader,
uint64_t address,
void *arg) {
EDMemoryObject memoryObject(byteReader, arg);
MCInst* inst = new MCInst;
uint64_t byteSize;
if (!Disassembler->getInstruction(*inst,
byteSize,
memoryObject,
address,
ErrorStream)) {
delete inst;
return NULL;
} else {
const llvm::EDInstInfo *thisInstInfo;
thisInstInfo = &InstInfos[inst->getOpcode()];
EDInst* sdInst = new EDInst(inst, byteSize, *this, thisInstInfo);
return sdInst;
}
}
void EDDisassembler::initMaps(const TargetRegisterInfo ®isterInfo) {
unsigned numRegisters = registerInfo.getNumRegs();
unsigned registerIndex;
for (registerIndex = 0; registerIndex < numRegisters; ++registerIndex) {
const char* registerName = registerInfo.get(registerIndex).Name;
RegVec.push_back(registerName);
RegRMap[registerName] = registerIndex;
}
switch (Key.Arch) {
default:
break;
case Triple::x86:
case Triple::x86_64:
stackPointers.insert(registerIDWithName("SP"));
stackPointers.insert(registerIDWithName("ESP"));
stackPointers.insert(registerIDWithName("RSP"));
programCounters.insert(registerIDWithName("IP"));
programCounters.insert(registerIDWithName("EIP"));
programCounters.insert(registerIDWithName("RIP"));
break;
case Triple::arm:
case Triple::thumb:
stackPointers.insert(registerIDWithName("SP"));
programCounters.insert(registerIDWithName("PC"));
break;
}
}
const char *EDDisassembler::nameWithRegisterID(unsigned registerID) const {
if (registerID >= RegVec.size())
return NULL;
else
return RegVec[registerID].c_str();
}
unsigned EDDisassembler::registerIDWithName(const char *name) const {
regrmap_t::const_iterator iter = RegRMap.find(std::string(name));
if (iter == RegRMap.end())
return 0;
else
return (*iter).second;
}
bool EDDisassembler::registerIsStackPointer(unsigned registerID) {
return (stackPointers.find(registerID) != stackPointers.end());
}
bool EDDisassembler::registerIsProgramCounter(unsigned registerID) {
return (programCounters.find(registerID) != programCounters.end());
}
int EDDisassembler::printInst(std::string &str, MCInst &inst) {
PrinterMutex.acquire();
InstPrinter->printInst(&inst, *InstStream);
InstStream->flush();
str = *InstString;
InstString->clear();
PrinterMutex.release();
return 0;
}
int EDDisassembler::parseInst(SmallVectorImpl<MCParsedAsmOperand*> &operands,
SmallVectorImpl<AsmToken> &tokens,
const std::string &str) {
int ret = 0;
switch (Key.Arch) {
default:
return -1;
case Triple::x86:
case Triple::x86_64:
case Triple::arm:
case Triple::thumb:
break;
}
const char *cStr = str.c_str();
MemoryBuffer *buf = MemoryBuffer::getMemBuffer(cStr, cStr + strlen(cStr));
StringRef instName;
SMLoc instLoc;
SourceMgr sourceMgr;
sourceMgr.AddNewSourceBuffer(buf, SMLoc()); // ownership of buf handed over
MCContext context(*AsmInfo, NULL);
OwningPtr<MCStreamer> streamer(createNullStreamer(context));
OwningPtr<MCAsmParser> genericParser(createMCAsmParser(*Tgt, sourceMgr,
context, *streamer,
*AsmInfo));
OwningPtr<TargetAsmParser> TargetParser(Tgt->createAsmParser(*genericParser,
*TargetMachine));
AsmToken OpcodeToken = genericParser->Lex();
AsmToken NextToken = genericParser->Lex(); // consume next token, because specificParser expects us to
if (OpcodeToken.is(AsmToken::Identifier)) {
instName = OpcodeToken.getString();
instLoc = OpcodeToken.getLoc();
if (NextToken.isNot(AsmToken::Eof) &&
TargetParser->ParseInstruction(instName, instLoc, operands))
ret = -1;
} else {
ret = -1;
}
ParserMutex.acquire();
if (!ret) {
GenericAsmLexer->setBuffer(buf);
while (SpecificAsmLexer->Lex(),
SpecificAsmLexer->isNot(AsmToken::Eof) &&
SpecificAsmLexer->isNot(AsmToken::EndOfStatement)) {
if (SpecificAsmLexer->is(AsmToken::Error)) {
ret = -1;
break;
}
tokens.push_back(SpecificAsmLexer->getTok());
}
}
ParserMutex.release();
return ret;
}
int EDDisassembler::llvmSyntaxVariant() const {
return LLVMSyntaxVariant;
}
|