llvm-project
203 строки · 6.2 Кб
1//===- Disassembler.cpp - Disassembler for hex strings --------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This class implements the disassembler of strings of bytes written in
10// hexadecimal, from standard input or from a file.
11//
12//===----------------------------------------------------------------------===//
13
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/MCRegisterInfo.h"
20#include "llvm/MC/MCStreamer.h"
21#include "llvm/MC/MCSubtargetInfo.h"
22#include "llvm/MC/TargetRegistry.h"
23#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/SourceMgr.h"
25#include "llvm/Support/raw_ostream.h"
26#include "llvm/TargetParser/Triple.h"
27
28using namespace llvm;
29
30typedef std::pair<std::vector<unsigned char>, std::vector<const char *>>
31ByteArrayTy;
32
33static bool PrintInsts(const MCDisassembler &DisAsm, const ByteArrayTy &Bytes,
34SourceMgr &SM, raw_ostream &Out, MCStreamer &Streamer,
35bool InAtomicBlock, const MCSubtargetInfo &STI) {
36ArrayRef<uint8_t> Data(Bytes.first.data(), Bytes.first.size());
37
38// Disassemble it to strings.
39uint64_t Size;
40uint64_t Index;
41
42for (Index = 0; Index < Bytes.first.size(); Index += Size) {
43MCInst Inst;
44
45MCDisassembler::DecodeStatus S;
46S = DisAsm.getInstruction(Inst, Size, Data.slice(Index), Index, nulls());
47switch (S) {
48case MCDisassembler::Fail:
49SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
50SourceMgr::DK_Warning, "invalid instruction encoding");
51// Don't try to resynchronise the stream in a block
52if (InAtomicBlock)
53return true;
54
55if (Size == 0)
56Size = 1; // skip illegible bytes
57
58break;
59
60case MCDisassembler::SoftFail:
61SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
62SourceMgr::DK_Warning,
63"potentially undefined instruction encoding");
64[[fallthrough]];
65
66case MCDisassembler::Success:
67Streamer.emitInstruction(Inst, STI);
68break;
69}
70}
71
72return false;
73}
74
75static bool SkipToToken(StringRef &Str) {
76for (;;) {
77if (Str.empty())
78return false;
79
80// Strip horizontal whitespace and commas.
81if (size_t Pos = Str.find_first_not_of(" \t\r\n,")) {
82Str = Str.substr(Pos);
83continue;
84}
85
86// If this is the start of a comment, remove the rest of the line.
87if (Str[0] == '#') {
88Str = Str.substr(Str.find_first_of('\n'));
89continue;
90}
91return true;
92}
93}
94
95static bool ByteArrayFromString(ByteArrayTy &ByteArray, StringRef &Str,
96SourceMgr &SM) {
97while (SkipToToken(Str)) {
98// Handled by higher level
99if (Str[0] == '[' || Str[0] == ']')
100return false;
101
102// Get the current token.
103size_t Next = Str.find_first_of(" \t\n\r,#[]");
104StringRef Value = Str.substr(0, Next);
105
106// Convert to a byte and add to the byte vector.
107unsigned ByteVal;
108if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
109// If we have an error, print it and skip to the end of line.
110SM.PrintMessage(SMLoc::getFromPointer(Value.data()), SourceMgr::DK_Error,
111"invalid input token");
112Str = Str.substr(Str.find('\n'));
113ByteArray.first.clear();
114ByteArray.second.clear();
115continue;
116}
117
118ByteArray.first.push_back(ByteVal);
119ByteArray.second.push_back(Value.data());
120Str = Str.substr(Next);
121}
122
123return false;
124}
125
126int Disassembler::disassemble(const Target &T, const std::string &TripleName,
127MCSubtargetInfo &STI, MCStreamer &Streamer,
128MemoryBuffer &Buffer, SourceMgr &SM,
129raw_ostream &Out) {
130std::unique_ptr<const MCRegisterInfo> MRI(T.createMCRegInfo(TripleName));
131if (!MRI) {
132errs() << "error: no register info for target " << TripleName << "\n";
133return -1;
134}
135
136MCTargetOptions MCOptions;
137std::unique_ptr<const MCAsmInfo> MAI(
138T.createMCAsmInfo(*MRI, TripleName, MCOptions));
139if (!MAI) {
140errs() << "error: no assembly info for target " << TripleName << "\n";
141return -1;
142}
143
144// Set up the MCContext for creating symbols and MCExpr's.
145MCContext Ctx(Triple(TripleName), MAI.get(), MRI.get(), &STI);
146
147std::unique_ptr<const MCDisassembler> DisAsm(
148T.createMCDisassembler(STI, Ctx));
149if (!DisAsm) {
150errs() << "error: no disassembler for target " << TripleName << "\n";
151return -1;
152}
153
154// Set up initial section manually here
155Streamer.initSections(false, STI);
156
157bool ErrorOccurred = false;
158
159// Convert the input to a vector for disassembly.
160ByteArrayTy ByteArray;
161StringRef Str = Buffer.getBuffer();
162bool InAtomicBlock = false;
163
164while (SkipToToken(Str)) {
165ByteArray.first.clear();
166ByteArray.second.clear();
167
168if (Str[0] == '[') {
169if (InAtomicBlock) {
170SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
171"nested atomic blocks make no sense");
172ErrorOccurred = true;
173}
174InAtomicBlock = true;
175Str = Str.drop_front();
176continue;
177} else if (Str[0] == ']') {
178if (!InAtomicBlock) {
179SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
180"attempt to close atomic block without opening");
181ErrorOccurred = true;
182}
183InAtomicBlock = false;
184Str = Str.drop_front();
185continue;
186}
187
188// It's a real token, get the bytes and emit them
189ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
190
191if (!ByteArray.first.empty())
192ErrorOccurred |=
193PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer, InAtomicBlock, STI);
194}
195
196if (InAtomicBlock) {
197SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
198"unclosed atomic block");
199ErrorOccurred = true;
200}
201
202return ErrorOccurred;
203}
204