1 //===- Disassembler.cpp - Disassembler for hex strings --------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This class implements the disassembler of strings of bytes written in
10 // hexadecimal, from standard input or from a file.
12 //===----------------------------------------------------------------------===//
14 #include "Disassembler.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/MC/MCAsmInfo.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCObjectFileInfo.h"
21 #include "llvm/MC/MCRegisterInfo.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include "llvm/Support/TargetRegistry.h"
27 #include "llvm/Support/raw_ostream.h"
31 typedef std::pair
<std::vector
<unsigned char>, std::vector
<const char *>>
34 static bool PrintInsts(const MCDisassembler
&DisAsm
,
35 const ByteArrayTy
&Bytes
,
36 SourceMgr
&SM
, raw_ostream
&Out
,
37 MCStreamer
&Streamer
, bool InAtomicBlock
,
38 const MCSubtargetInfo
&STI
) {
39 ArrayRef
<uint8_t> Data(Bytes
.first
.data(), Bytes
.first
.size());
41 // Disassemble it to strings.
45 for (Index
= 0; Index
< Bytes
.first
.size(); Index
+= Size
) {
48 MCDisassembler::DecodeStatus S
;
49 S
= DisAsm
.getInstruction(Inst
, Size
, Data
.slice(Index
), Index
, nulls());
51 case MCDisassembler::Fail
:
52 SM
.PrintMessage(SMLoc::getFromPointer(Bytes
.second
[Index
]),
53 SourceMgr::DK_Warning
,
54 "invalid instruction encoding");
55 // Don't try to resynchronise the stream in a block
60 Size
= 1; // skip illegible bytes
64 case MCDisassembler::SoftFail
:
65 SM
.PrintMessage(SMLoc::getFromPointer(Bytes
.second
[Index
]),
66 SourceMgr::DK_Warning
,
67 "potentially undefined instruction encoding");
70 case MCDisassembler::Success
:
71 Streamer
.emitInstruction(Inst
, STI
);
79 static bool SkipToToken(StringRef
&Str
) {
84 // Strip horizontal whitespace and commas.
85 if (size_t Pos
= Str
.find_first_not_of(" \t\r\n,")) {
86 Str
= Str
.substr(Pos
);
90 // If this is the start of a comment, remove the rest of the line.
92 Str
= Str
.substr(Str
.find_first_of('\n'));
100 static bool ByteArrayFromString(ByteArrayTy
&ByteArray
,
103 while (SkipToToken(Str
)) {
104 // Handled by higher level
105 if (Str
[0] == '[' || Str
[0] == ']')
108 // Get the current token.
109 size_t Next
= Str
.find_first_of(" \t\n\r,#[]");
110 StringRef Value
= Str
.substr(0, Next
);
112 // Convert to a byte and add to the byte vector.
114 if (Value
.getAsInteger(0, ByteVal
) || ByteVal
> 255) {
115 // If we have an error, print it and skip to the end of line.
116 SM
.PrintMessage(SMLoc::getFromPointer(Value
.data()), SourceMgr::DK_Error
,
117 "invalid input token");
118 Str
= Str
.substr(Str
.find('\n'));
119 ByteArray
.first
.clear();
120 ByteArray
.second
.clear();
124 ByteArray
.first
.push_back(ByteVal
);
125 ByteArray
.second
.push_back(Value
.data());
126 Str
= Str
.substr(Next
);
132 int Disassembler::disassemble(const Target
&T
, const std::string
&Triple
,
133 MCSubtargetInfo
&STI
, MCStreamer
&Streamer
,
134 MemoryBuffer
&Buffer
, SourceMgr
&SM
,
135 MCContext
&Ctx
, raw_ostream
&Out
,
136 const MCTargetOptions
&MCOptions
) {
138 std::unique_ptr
<const MCRegisterInfo
> MRI(T
.createMCRegInfo(Triple
));
140 errs() << "error: no register info for target " << Triple
<< "\n";
144 std::unique_ptr
<const MCAsmInfo
> MAI(
145 T
.createMCAsmInfo(*MRI
, Triple
, MCOptions
));
147 errs() << "error: no assembly info for target " << Triple
<< "\n";
151 std::unique_ptr
<const MCDisassembler
> DisAsm(
152 T
.createMCDisassembler(STI
, Ctx
));
154 errs() << "error: no disassembler for target " << Triple
<< "\n";
158 // Set up initial section manually here
159 Streamer
.InitSections(false);
161 bool ErrorOccurred
= false;
163 // Convert the input to a vector for disassembly.
164 ByteArrayTy ByteArray
;
165 StringRef Str
= Buffer
.getBuffer();
166 bool InAtomicBlock
= false;
168 while (SkipToToken(Str
)) {
169 ByteArray
.first
.clear();
170 ByteArray
.second
.clear();
174 SM
.PrintMessage(SMLoc::getFromPointer(Str
.data()), SourceMgr::DK_Error
,
175 "nested atomic blocks make no sense");
176 ErrorOccurred
= true;
178 InAtomicBlock
= true;
179 Str
= Str
.drop_front();
181 } else if (Str
[0] == ']') {
182 if (!InAtomicBlock
) {
183 SM
.PrintMessage(SMLoc::getFromPointer(Str
.data()), SourceMgr::DK_Error
,
184 "attempt to close atomic block without opening");
185 ErrorOccurred
= true;
187 InAtomicBlock
= false;
188 Str
= Str
.drop_front();
192 // It's a real token, get the bytes and emit them
193 ErrorOccurred
|= ByteArrayFromString(ByteArray
, Str
, SM
);
195 if (!ByteArray
.first
.empty())
196 ErrorOccurred
|= PrintInsts(*DisAsm
, ByteArray
, SM
, Out
, Streamer
,
201 SM
.PrintMessage(SMLoc::getFromPointer(Str
.data()), SourceMgr::DK_Error
,
202 "unclosed atomic block");
203 ErrorOccurred
= true;
206 return ErrorOccurred
;