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/MC/MCAsmInfo.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCObjectFileInfo.h"
20 #include "llvm/MC/MCRegisterInfo.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCSubtargetInfo.h"
23 #include "llvm/MC/TargetRegistry.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/TargetParser/Triple.h"
31 typedef std::pair
<std::vector
<unsigned char>, std::vector
<const char *>>
34 static bool PrintInsts(const MCDisassembler
&DisAsm
, const ByteArrayTy
&Bytes
,
35 SourceMgr
&SM
, MCStreamer
&Streamer
, bool InAtomicBlock
,
36 const MCSubtargetInfo
&STI
) {
37 ArrayRef
<uint8_t> Data(Bytes
.first
.data(), Bytes
.first
.size());
39 // Disassemble it to strings.
43 for (Index
= 0; Index
< Bytes
.first
.size(); Index
+= Size
) {
46 MCDisassembler::DecodeStatus S
;
47 S
= DisAsm
.getInstruction(Inst
, Size
, Data
.slice(Index
), Index
, nulls());
49 case MCDisassembler::Fail
:
50 SM
.PrintMessage(SMLoc::getFromPointer(Bytes
.second
[Index
]),
51 SourceMgr::DK_Warning
,
52 "invalid instruction encoding");
53 // Don't try to resynchronise the stream in a block
58 Size
= 1; // skip illegible bytes
62 case MCDisassembler::SoftFail
:
63 SM
.PrintMessage(SMLoc::getFromPointer(Bytes
.second
[Index
]),
64 SourceMgr::DK_Warning
,
65 "potentially undefined instruction encoding");
68 case MCDisassembler::Success
:
69 Streamer
.emitInstruction(Inst
, STI
);
77 static bool SkipToToken(StringRef
&Str
) {
82 // Strip horizontal whitespace and commas.
83 if (size_t Pos
= Str
.find_first_not_of(" \t\r\n,")) {
84 Str
= Str
.substr(Pos
);
88 // If this is the start of a comment, remove the rest of the line.
90 Str
= Str
.substr(Str
.find_first_of('\n'));
98 static bool ByteArrayFromString(ByteArrayTy
&ByteArray
,
101 while (SkipToToken(Str
)) {
102 // Handled by higher level
103 if (Str
[0] == '[' || Str
[0] == ']')
106 // Get the current token.
107 size_t Next
= Str
.find_first_of(" \t\n\r,#[]");
108 StringRef Value
= Str
.substr(0, Next
);
110 // Convert to a byte and add to the byte vector.
112 if (Value
.getAsInteger(0, ByteVal
) || ByteVal
> 255) {
113 // If we have an error, print it and skip to the end of line.
114 SM
.PrintMessage(SMLoc::getFromPointer(Value
.data()), SourceMgr::DK_Error
,
115 "invalid input token");
116 Str
= Str
.substr(Str
.find('\n'));
117 ByteArray
.first
.clear();
118 ByteArray
.second
.clear();
122 ByteArray
.first
.push_back(ByteVal
);
123 ByteArray
.second
.push_back(Value
.data());
124 Str
= Str
.substr(Next
);
130 int Disassembler::disassemble(const Target
&T
, const std::string
&Triple
,
131 MCSubtargetInfo
&STI
, MCStreamer
&Streamer
,
132 MemoryBuffer
&Buffer
, SourceMgr
&SM
,
134 const MCTargetOptions
&MCOptions
) {
136 std::unique_ptr
<const MCRegisterInfo
> MRI(T
.createMCRegInfo(Triple
));
138 errs() << "error: no register info for target " << Triple
<< "\n";
142 std::unique_ptr
<const MCAsmInfo
> MAI(
143 T
.createMCAsmInfo(*MRI
, Triple
, MCOptions
));
145 errs() << "error: no assembly info for target " << Triple
<< "\n";
149 std::unique_ptr
<const MCDisassembler
> DisAsm(
150 T
.createMCDisassembler(STI
, Ctx
));
152 errs() << "error: no disassembler for target " << Triple
<< "\n";
156 // Set up initial section manually here
157 Streamer
.initSections(false, STI
);
159 bool ErrorOccurred
= false;
161 // Convert the input to a vector for disassembly.
162 ByteArrayTy ByteArray
;
163 StringRef Str
= Buffer
.getBuffer();
164 bool InAtomicBlock
= false;
166 while (SkipToToken(Str
)) {
167 ByteArray
.first
.clear();
168 ByteArray
.second
.clear();
172 SM
.PrintMessage(SMLoc::getFromPointer(Str
.data()), SourceMgr::DK_Error
,
173 "nested atomic blocks make no sense");
174 ErrorOccurred
= true;
176 InAtomicBlock
= true;
177 Str
= Str
.drop_front();
179 } else if (Str
[0] == ']') {
180 if (!InAtomicBlock
) {
181 SM
.PrintMessage(SMLoc::getFromPointer(Str
.data()), SourceMgr::DK_Error
,
182 "attempt to close atomic block without opening");
183 ErrorOccurred
= true;
185 InAtomicBlock
= false;
186 Str
= Str
.drop_front();
190 // It's a real token, get the bytes and emit them
191 ErrorOccurred
|= ByteArrayFromString(ByteArray
, Str
, SM
);
193 if (!ByteArray
.first
.empty())
195 PrintInsts(*DisAsm
, ByteArray
, SM
, Streamer
, InAtomicBlock
, STI
);
199 SM
.PrintMessage(SMLoc::getFromPointer(Str
.data()), SourceMgr::DK_Error
,
200 "unclosed atomic block");
201 ErrorOccurred
= true;
204 return ErrorOccurred
;