LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
Instruction.hpp
Go to the documentation of this file.
1/* Copyright 2022 - 2026 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_ASM_INST_H
16#define LIEF_ASM_INST_H
17#include "LIEF/visibility.h"
18#include "LIEF/iterators.hpp"
19#include "LIEF/errors.hpp"
20
21#include <ostream>
22#include <memory>
23#include <string>
24
25namespace llvm {
26class MCInst;
27}
28
29namespace LIEF {
30namespace assembly {
31
32namespace details {
33class Instruction;
34class InstructionIt;
35}
36
38class LIEF_API Instruction {
39 public:
41 class Iterator final
42 : public iterator_facade_base<Iterator, std::forward_iterator_tag, Instruction,
43 std::ptrdiff_t, const Instruction*,
44 const Instruction&> {
45 public:
46 using implementation = details::InstructionIt;
47 using iterator_facade_base::operator++;
48
50
51 LIEF_API Iterator(std::unique_ptr<details::InstructionIt> impl);
54
56 LIEF_API Iterator& operator=(Iterator&&) noexcept;
57
59
60 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
61 LIEF_API Iterator& operator++();
62
63 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS);
64
65 friend bool operator!=(const Iterator& LHS, const Iterator& RHS) {
66 return !(LHS == RHS);
67 }
68
69 LIEF_API const Instruction& operator*() const;
70
71 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
72 LIEF_API const Instruction* operator->() const;
73
76 LIEF_API std::unique_ptr<Instruction> yield();
77
78 private:
79 void load() const;
80
81 std::unique_ptr<details::InstructionIt> impl_;
82 mutable std::unique_ptr<Instruction> cached_;
83 };
84
85 public:
87 enum class MemoryAccess : uint8_t {
88 NONE = 0,
89 READ = 1 << 0,
90 WRITE = 1 << 1,
91 READ_WRITE = READ | WRITE,
92 };
93
95 uint64_t address() const;
96
98 size_t size() const;
99
101 const std::vector<uint8_t>& raw() const;
102
104 std::string mnemonic() const;
105
107 std::string to_string(bool with_address = true) const;
108
110 bool is_call() const;
111
113 bool is_terminator() const;
114
116 bool is_branch() const;
117
119 bool is_syscall() const;
120
122 bool is_memory_access() const;
123
125 bool is_move_reg() const;
126
128 bool is_add() const;
129
134 bool is_trap() const;
135
139 bool is_barrier() const;
140
142 bool is_return() const;
143
148 bool is_indirect_branch() const;
149
153
157
159 bool is_compare() const;
160
162 bool is_move_immediate() const;
163
165 bool is_bitcast() const;
166
169
173
178 const llvm::MCInst& mcinst() const;
179
188 template<class T>
189 const T* as() const {
190 static_assert(std::is_base_of<Instruction, T>::value,
191 "Require Instruction inheritance");
192 if (T::classof(this)) {
193 return static_cast<const T*>(this);
194 }
195 return nullptr;
196 }
197
198 friend LIEF_API std::ostream& operator<<(std::ostream& os,
199 const Instruction& inst) {
200 os << inst.to_string();
201 return os;
202 }
203
204 virtual ~Instruction();
205
207 static LIEF_LOCAL std::unique_ptr<Instruction>
208 create(std::unique_ptr<details::Instruction> impl);
209
211 LIEF_LOCAL const details::Instruction& impl() const {
212 assert(impl_ != nullptr);
213 return *impl_;
214 }
215
217 LIEF_LOCAL details::Instruction& impl() {
218 assert(impl_ != nullptr);
219 return *impl_;
220 }
221
222 protected:
223 LIEF_LOCAL Instruction(std::unique_ptr<details::Instruction> impl);
224 std::unique_ptr<details::Instruction> impl_;
225};
226}
227}
228#endif
const Instruction & operator*() const
std::unique_ptr< Instruction > yield()
Transfer ownership of the instruction at the current position to the caller. Returns nullptr if the i...
const Instruction * operator->() const
Iterator & operator=(const Iterator &)
details::InstructionIt implementation
Definition Instruction.hpp:46
Iterator(std::unique_ptr< details::InstructionIt > impl)
MemoryAccess memory_access() const
Memory access flags.
bool is_move_reg() const
True if the instruction is a register to register move.
bool is_terminator() const
True if the instruction marks the end of a basic block.
const T * as() const
This function can be used to down cast an Instruction instance:
Definition Instruction.hpp:189
bool is_compare() const
True if the instruction is a comparison.
bool is_conditional_branch() const
True if the instruction is conditionally jumping to the next instruction or an instruction into some ...
bool is_move_immediate() const
True if the instruction is moving an immediate.
bool is_syscall() const
True if the instruction is a syscall.
bool is_indirect_branch() const
True if the instruction is an indirect branch.
const llvm::MCInst & mcinst() const
Return the underlying llvm::MCInst implementation.
MemoryAccess
Memory operation flags.
Definition Instruction.hpp:87
bool is_branch() const
True if the instruction is a branch.
std::string mnemonic() const
Instruction mnemonic (e.g. br).
std::string to_string(bool with_address=true) const
Representation of the current instruction in a pretty assembly way.
bool is_bitcast() const
True if the instruction is doing a bitcast.
bool is_trap() const
True if the instruction is a trap.
bool is_unconditional_branch() const
True if the instruction is jumping (unconditionally) to some other basic block.
bool is_return() const
True if the instruction is a return.
uint64_t address() const
Address of the instruction.
bool is_add() const
True if the instruction performs an arithmetic addition.
bool is_call() const
True if the instruction is a call.
const std::vector< uint8_t > & raw() const
Raw bytes of the current instruction.
friend std::ostream & operator<<(std::ostream &os, const Instruction &inst)
Definition Instruction.hpp:198
bool is_memory_access() const
True if the instruction performs a memory access.
bool is_barrier() const
True if the instruction prevents executing the instruction that immediately follows the current....
result< uint64_t > branch_target() const
Given a is_branch() instruction, try to evaluate the address of the destination.
size_t size() const
Size of the instruction in bytes.
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition iterators.hpp:729
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:77
Definition Engine.hpp:31
Namespace related to assembly/disassembly support.
Definition Abstract/Binary.hpp:47
LIEF namespace.
Definition Abstract/Binary.hpp:40
Definition Abstract/Binary.hpp:35
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46