This repository was archived by the owner on Dec 16, 2022. It is now read-only.
forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdisasm.cpp
210 lines (179 loc) · 6.95 KB
/
disasm.cpp
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
//===------------- Disassembler for in-memory function --------------------===//
//
// Modified for use in The Julia Language from code in the llvm-mc project:
// llvm-mc.cpp and Disassembler.cpp
//
// Original copyright:
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This class implements a disassembler of a memory block, given a function
// pointer and size.
//
//===----------------------------------------------------------------------===//
#include "llvm/MC/MCDisassembler.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCInstPrinter.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/system_error.h"
#include <string>
#include <iostream>
using namespace llvm;
namespace {
class FuncMCView : public MemoryObject {
private:
const char* Fptr;
size_t Fsize;
public:
FuncMCView(const void* fptr, size_t size) : Fptr((const char*)fptr), Fsize(size) {}
const char* operator[] (const size_t idx) { return (Fptr+idx); }
uint64_t getBase() const { return 0; }
uint64_t getExtent() const { return Fsize; }
int readByte(uint64_t Addr, uint8_t *Byte) const {
if (Addr >= getExtent())
return -1;
*Byte = Fptr[Addr];
return 0;
}
};
}
extern "C"
void jl_dump_function_asm(void* Fptr, size_t Fsize,
std::vector<JITEvent_EmittedFunctionDetails::LineStart> lineinfo,
formatted_raw_ostream &stream) {
// Initialize targets and assembly printers/parsers.
// Avoids hard-coded targets - will generally be only host CPU anyway.
llvm::InitializeNativeTargetAsmParser();
llvm::InitializeNativeTargetDisassembler();
// Get the host information
std::string TripleName;
if (TripleName.empty())
TripleName = sys::getDefaultTargetTriple();
Triple TheTriple(Triple::normalize(TripleName));
std::string MCPU = sys::getHostCPUName();
SubtargetFeatures Features;
Features.getDefaultSubtargetFeatures(TheTriple);
std::string err;
const Target* TheTarget = TargetRegistry::lookupTarget(TripleName, err);
// Set up Subtarget and Disassembler
OwningPtr<MCSubtargetInfo>
STI(TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString()));
OwningPtr<const MCDisassembler> DisAsm(TheTarget->createMCDisassembler(*STI));
if (!DisAsm) {
JL_PRINTF(JL_STDERR, "error: no disassembler for target", TripleName.c_str(), "\n");
return;
}
// Set up required helpers and streamer
OwningPtr<MCStreamer> Streamer;
SourceMgr SrcMgr;
#ifdef LLVM34
llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*TheTarget->createMCRegInfo(TripleName),TripleName));
#else
llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TripleName));
#endif
assert(MAI && "Unable to create target asm info!");
llvm::OwningPtr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
assert(MRI && "Unable to create target register info!");
OwningPtr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
#ifdef LLVM34
MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
#else
MCContext Ctx(*MAI, *MRI, MOFI.get(), &SrcMgr);
#endif
MOFI->InitMCObjectFileInfo(TripleName, Reloc::Default, CodeModel::Default, Ctx);
unsigned OutputAsmVariant = 1;
bool ShowEncoding = false;
bool ShowInst = false;
OwningPtr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
MCInstPrinter* IP =
TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI, *MCII, *MRI, *STI);
MCCodeEmitter *CE = 0;
MCAsmBackend *MAB = 0;
if (ShowEncoding) {
CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
#ifdef LLVM34
MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, MCPU);
#else
MAB = TheTarget->createMCAsmBackend(TripleName, MCPU);
#endif
}
Streamer.reset(TheTarget->createAsmStreamer(Ctx, stream, /*asmverbose*/true,
#ifndef LLVM35
/*useLoc*/ true,
#endif
/*useCFI*/ true,
/*useDwarfDirectory*/ true,
IP, CE, MAB, ShowInst));
Streamer->InitSections();
// Make the MemoryObject wrapper
FuncMCView memoryObject(Fptr, Fsize);
uint64_t Size;
uint64_t Index;
uint64_t absAddr;
// Set up the line info
typedef std::vector<JITEvent_EmittedFunctionDetails::LineStart> LInfoVec;
LInfoVec::iterator lineIter = lineinfo.begin();
lineIter = lineinfo.begin();
uint64_t nextLineAddr = -1;
DISubprogram debugscope;
if (lineIter != lineinfo.end()) {
nextLineAddr = (*lineIter).Address;
debugscope = DISubprogram((*lineIter).Loc.getScope(jl_LLVMContext));
stream << "Filename: " << debugscope.getFilename().data() << "\n";
stream << "Source line: " << (*lineIter).Loc.getLine() << "\n";
}
// Do the disassembly
for (Index = 0, absAddr = (uint64_t)Fptr;
Index < memoryObject.getExtent(); Index += Size, absAddr += Size) {
if (nextLineAddr != (uint64_t)-1 && absAddr == nextLineAddr) {
stream << "Source line: " << (*lineIter).Loc.getLine() << "\n";
nextLineAddr = (*++lineIter).Address;
}
MCInst Inst;
MCDisassembler::DecodeStatus S;
S = DisAsm->getInstruction(Inst, Size, memoryObject, Index,
/*REMOVE*/ nulls(), nulls());
switch (S) {
case MCDisassembler::Fail:
SrcMgr.PrintMessage(SMLoc::getFromPointer(memoryObject[Index]),
SourceMgr::DK_Warning,
"invalid instruction encoding");
if (Size == 0)
Size = 1; // skip illegible bytes
break;
case MCDisassembler::SoftFail:
SrcMgr.PrintMessage(SMLoc::getFromPointer(memoryObject[Index]),
SourceMgr::DK_Warning,
"potentially undefined instruction encoding");
// Fall through
case MCDisassembler::Success:
#ifdef LLVM35
Streamer->EmitInstruction(Inst, *STI);
#else
Streamer->EmitInstruction(Inst);
#endif
break;
}
}
}