blob: 16ab99548adfc9bb1536e3adf2aa95d3128e37c4 [file] [log] [blame]
Chris Lattnerb257d242009-12-22 22:50:29 +00001//===- Disassembler.cpp - Disassembler for hex strings --------------------===//
Sean Callanan7e645502009-12-17 01:49:59 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sean Callanan7e645502009-12-17 01:49:59 +00006//
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
Chris Lattnerb257d242009-12-22 22:50:29 +000014#include "Disassembler.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000015#include "llvm/ADT/Triple.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000016#include "llvm/MC/MCAsmInfo.h"
17#include "llvm/MC/MCContext.h"
Benjamin Kramerf57c1972016-01-26 16:44:37 +000018#include "llvm/MC/MCDisassembler/MCDisassembler.h"
Sean Callanan7e645502009-12-17 01:49:59 +000019#include "llvm/MC/MCInst.h"
Thomas Lively2cb27072019-10-15 18:28:22 +000020#include "llvm/MC/MCObjectFileInfo.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000021#include "llvm/MC/MCRegisterInfo.h"
Richard Bartondef81b92012-04-16 11:32:10 +000022#include "llvm/MC/MCStreamer.h"
James Molloy4c493e82011-09-07 17:24:38 +000023#include "llvm/MC/MCSubtargetInfo.h"
Sean Callanan7e645502009-12-17 01:49:59 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattner88799182009-12-22 06:45:48 +000025#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000026#include "llvm/Support/TargetRegistry.h"
27#include "llvm/Support/raw_ostream.h"
Richard Bartondef81b92012-04-16 11:32:10 +000028
Sean Callanan7e645502009-12-17 01:49:59 +000029using namespace llvm;
30
Rafael Espindola45a753a2014-11-07 17:59:05 +000031typedef std::pair<std::vector<unsigned char>, std::vector<const char *>>
32 ByteArrayTy;
Sean Callanan7e645502009-12-17 01:49:59 +000033
Daniel Dunbard9d5b312010-03-20 22:36:35 +000034static bool PrintInsts(const MCDisassembler &DisAsm,
Richard Bartondef81b92012-04-16 11:32:10 +000035 const ByteArrayTy &Bytes,
36 SourceMgr &SM, raw_ostream &Out,
David Woodhousee6c13e42014-01-28 23:12:42 +000037 MCStreamer &Streamer, bool InAtomicBlock,
38 const MCSubtargetInfo &STI) {
Rafael Espindola7fc5b872014-11-12 02:04:27 +000039 ArrayRef<uint8_t> Data(Bytes.first.data(), Bytes.first.size());
Jim Grosbach112a2de2011-05-09 20:05:25 +000040
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000041 // Disassemble it to strings.
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000042 uint64_t Size;
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000043 uint64_t Index;
Jim Grosbach112a2de2011-05-09 20:05:25 +000044
Rafael Espindola45a753a2014-11-07 17:59:05 +000045 for (Index = 0; Index < Bytes.first.size(); Index += Size) {
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000046 MCInst Inst;
Jim Grosbach112a2de2011-05-09 20:05:25 +000047
Owen Andersona4043c42011-08-17 17:44:15 +000048 MCDisassembler::DecodeStatus S;
Fangrui Song6fdd6a72020-01-11 12:36:13 -080049 S = DisAsm.getInstruction(Inst, Size, Data.slice(Index), Index, nulls());
Owen Andersona4043c42011-08-17 17:44:15 +000050 switch (S) {
51 case MCDisassembler::Fail:
Rafael Espindola45a753a2014-11-07 17:59:05 +000052 SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
Chris Lattner03b80a42011-10-16 05:43:57 +000053 SourceMgr::DK_Warning,
54 "invalid instruction encoding");
Tim Northover48cf6cc2013-07-19 10:05:04 +000055 // Don't try to resynchronise the stream in a block
56 if (InAtomicBlock)
57 return true;
58
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000059 if (Size == 0)
60 Size = 1; // skip illegible bytes
Tim Northover48cf6cc2013-07-19 10:05:04 +000061
Owen Andersona4043c42011-08-17 17:44:15 +000062 break;
63
64 case MCDisassembler::SoftFail:
Rafael Espindola45a753a2014-11-07 17:59:05 +000065 SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
Chris Lattner03b80a42011-10-16 05:43:57 +000066 SourceMgr::DK_Warning,
67 "potentially undefined instruction encoding");
Justin Bognercd1d5aa2016-08-17 20:30:52 +000068 LLVM_FALLTHROUGH;
Owen Andersona4043c42011-08-17 17:44:15 +000069
70 case MCDisassembler::Success:
Fangrui Songbcd24b22020-02-13 21:58:16 -080071 Streamer.emitInstruction(Inst, STI);
Owen Andersona4043c42011-08-17 17:44:15 +000072 break;
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000073 }
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000074 }
Jim Grosbach112a2de2011-05-09 20:05:25 +000075
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000076 return false;
Sean Callanan7e645502009-12-17 01:49:59 +000077}
78
Tim Northover48cf6cc2013-07-19 10:05:04 +000079static bool SkipToToken(StringRef &Str) {
Colin LeMahieueb4675f2014-11-11 21:03:09 +000080 for (;;) {
81 if (Str.empty())
82 return false;
Jim Grosbach112a2de2011-05-09 20:05:25 +000083
Colin LeMahieueb4675f2014-11-11 21:03:09 +000084 // Strip horizontal whitespace and commas.
85 if (size_t Pos = Str.find_first_not_of(" \t\r\n,")) {
86 Str = Str.substr(Pos);
Sean Callanan2d03d3a2010-04-12 19:43:00 +000087 continue;
88 }
Tim Northover48cf6cc2013-07-19 10:05:04 +000089
Colin LeMahieueb4675f2014-11-11 21:03:09 +000090 // If this is the start of a comment, remove the rest of the line.
91 if (Str[0] == '#') {
92 Str = Str.substr(Str.find_first_of('\n'));
93 continue;
94 }
95 return true;
96 }
Tim Northover48cf6cc2013-07-19 10:05:04 +000097}
98
99
100static bool ByteArrayFromString(ByteArrayTy &ByteArray,
101 StringRef &Str,
102 SourceMgr &SM) {
103 while (SkipToToken(Str)) {
104 // Handled by higher level
105 if (Str[0] == '[' || Str[0] == ']')
106 return false;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000107
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000108 // Get the current token.
Tim Northover48cf6cc2013-07-19 10:05:04 +0000109 size_t Next = Str.find_first_of(" \t\n\r,#[]");
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000110 StringRef Value = Str.substr(0, Next);
Jim Grosbach112a2de2011-05-09 20:05:25 +0000111
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000112 // Convert to a byte and add to the byte vector.
113 unsigned ByteVal;
114 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
115 // If we have an error, print it and skip to the end of line.
Chris Lattner03b80a42011-10-16 05:43:57 +0000116 SM.PrintMessage(SMLoc::getFromPointer(Value.data()), SourceMgr::DK_Error,
117 "invalid input token");
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000118 Str = Str.substr(Str.find('\n'));
Rafael Espindola45a753a2014-11-07 17:59:05 +0000119 ByteArray.first.clear();
120 ByteArray.second.clear();
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000121 continue;
122 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000123
Rafael Espindola45a753a2014-11-07 17:59:05 +0000124 ByteArray.first.push_back(ByteVal);
125 ByteArray.second.push_back(Value.data());
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000126 Str = Str.substr(Next);
127 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000128
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000129 return false;
130}
131
Thomas Lively2cb27072019-10-15 18:28:22 +0000132int Disassembler::disassemble(const Target &T, const std::string &Triple,
133 MCSubtargetInfo &STI, MCStreamer &Streamer,
134 MemoryBuffer &Buffer, SourceMgr &SM,
Mirko Brkusanin4b63ca12019-10-23 12:24:35 +0200135 MCContext &Ctx, raw_ostream &Out,
136 const MCTargetOptions &MCOptions) {
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000137
138 std::unique_ptr<const MCRegisterInfo> MRI(T.createMCRegInfo(Triple));
139 if (!MRI) {
140 errs() << "error: no register info for target " << Triple << "\n";
141 return -1;
142 }
143
Mirko Brkusanin4b63ca12019-10-23 12:24:35 +0200144 std::unique_ptr<const MCAsmInfo> MAI(
145 T.createMCAsmInfo(*MRI, Triple, MCOptions));
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000146 if (!MAI) {
147 errs() << "error: no assembly info for target " << Triple << "\n";
148 return -1;
149 }
150
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000151 std::unique_ptr<const MCDisassembler> DisAsm(
152 T.createMCDisassembler(STI, Ctx));
Chris Lattner72cbaa22009-12-22 06:24:00 +0000153 if (!DisAsm) {
154 errs() << "error: no disassembler for target " << Triple << "\n";
Sean Callanan7e645502009-12-17 01:49:59 +0000155 return -1;
156 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000157
Richard Bartondef81b92012-04-16 11:32:10 +0000158 // Set up initial section manually here
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000159 Streamer.InitSections(false);
Jim Grosbach112a2de2011-05-09 20:05:25 +0000160
Chris Lattnerdd0c01b2009-12-22 06:56:51 +0000161 bool ErrorOccurred = false;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000162
Sean Callanan7e645502009-12-17 01:49:59 +0000163 // Convert the input to a vector for disassembly.
Chris Lattnerdd0c01b2009-12-22 06:56:51 +0000164 ByteArrayTy ByteArray;
Chris Lattner55ddc302010-04-09 04:24:20 +0000165 StringRef Str = Buffer.getBuffer();
Tim Northover48cf6cc2013-07-19 10:05:04 +0000166 bool InAtomicBlock = false;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000167
Tim Northover48cf6cc2013-07-19 10:05:04 +0000168 while (SkipToToken(Str)) {
Rafael Espindola45a753a2014-11-07 17:59:05 +0000169 ByteArray.first.clear();
170 ByteArray.second.clear();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000171
Tim Northover48cf6cc2013-07-19 10:05:04 +0000172 if (Str[0] == '[') {
173 if (InAtomicBlock) {
174 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
175 "nested atomic blocks make no sense");
176 ErrorOccurred = true;
177 }
178 InAtomicBlock = true;
179 Str = Str.drop_front();
180 continue;
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;
186 }
187 InAtomicBlock = false;
188 Str = Str.drop_front();
189 continue;
190 }
191
192 // It's a real token, get the bytes and emit them
193 ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
194
Rafael Espindola45a753a2014-11-07 17:59:05 +0000195 if (!ByteArray.first.empty())
Tim Northover48cf6cc2013-07-19 10:05:04 +0000196 ErrorOccurred |= PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer,
David Woodhousee6c13e42014-01-28 23:12:42 +0000197 InAtomicBlock, STI);
Tim Northover48cf6cc2013-07-19 10:05:04 +0000198 }
199
200 if (InAtomicBlock) {
201 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
202 "unclosed atomic block");
203 ErrorOccurred = true;
204 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000205
Chris Lattnerdd0c01b2009-12-22 06:56:51 +0000206 return ErrorOccurred;
Sean Callanan7e645502009-12-17 01:49:59 +0000207}