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"
20#include "LIEF/errors.hpp"
21
22#include <ostream>
23#include <memory>
24#include <string>
25
26namespace llvm {
27class MCInst;
28}
29
30namespace LIEF {
31namespace assembly {
32
33namespace details {
34class Instruction;
35class InstructionIt;
36}
37
39class LIEF_API Instruction {
40 public:
42 class Iterator final
43 : public iterator_facade_base<Iterator, std::forward_iterator_tag, Instruction,
44 std::ptrdiff_t, const Instruction*,
45 const Instruction&> {
46 public:
47 using implementation = details::InstructionIt;
48 using iterator_facade_base::operator++;
49
51
52 LIEF_API Iterator(std::unique_ptr<details::InstructionIt> impl);
55
57 LIEF_API Iterator& operator=(Iterator&&) noexcept;
58
60
61 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
62 LIEF_API Iterator& operator++();
63
64 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS);
65
66 friend bool operator!=(const Iterator& LHS, const Iterator& RHS) {
67 return !(LHS == RHS);
68 }
69
70 LIEF_API const Instruction& operator*() const LIEF_LIFETIMEBOUND;
71
72 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
73 LIEF_API const Instruction* operator->() const LIEF_LIFETIMEBOUND;
74
77 LIEF_API std::unique_ptr<Instruction> yield();
78
79 private:
80 void load() const;
81
82 std::unique_ptr<details::InstructionIt> impl_;
83 mutable std::unique_ptr<Instruction> cached_;
84 };
85
86 public:
88 enum class MemoryAccess : uint8_t {
89 NONE = 0,
90 READ = 1 << 0,
91 WRITE = 1 << 1,
92 READ_WRITE = READ | WRITE,
93 };
94
96 uint64_t address() const;
97
99 size_t size() const;
100
102 const std::vector<uint8_t>& raw() const LIEF_LIFETIMEBOUND;
103
105 std::string mnemonic() const;
106
108 std::string to_string(bool with_address = true) const;
109
111 bool is_call() const;
112
114 bool is_terminator() const;
115
117 bool is_branch() const;
118
120 bool is_syscall() const;
121
123 bool is_memory_access() const;
124
126 bool is_move_reg() const;
127
129 bool is_add() const;
130
135 bool is_trap() const;
136
140 bool is_barrier() const;
141
143 bool is_return() const;
144
149 bool is_indirect_branch() const;
150
154
158
160 bool is_compare() const;
161
163 bool is_move_immediate() const;
164
166 bool is_bitcast() const;
167
170
173 result<uint64_t> branch_target() const;
174
179 const llvm::MCInst& mcinst() const LIEF_LIFETIMEBOUND;
180
189 template<class T>
190 const T* as() const LIEF_LIFETIMEBOUND {
191 static_assert(std::is_base_of<Instruction, T>::value,
192 "Require Instruction inheritance");
193 if (T::classof(this)) {
194 return static_cast<const T*>(this);
195 }
196 return nullptr;
197 }
198
199 friend LIEF_API std::ostream& operator<<(std::ostream& os,
200 const Instruction& inst) {
201 os << inst.to_string();
202 return os;
203 }
204
205 virtual ~Instruction();
206
208 static LIEF_LOCAL std::unique_ptr<Instruction>
209 create(std::unique_ptr<details::Instruction> impl);
210
212 LIEF_LOCAL const details::Instruction& impl() const LIEF_LIFETIMEBOUND {
213 assert(impl_ != nullptr);
214 return *impl_;
215 }
216
218 LIEF_LOCAL details::Instruction& impl() LIEF_LIFETIMEBOUND {
219 assert(impl_ != nullptr);
220 return *impl_;
221 }
222
223 protected:
224 LIEF_LOCAL Instruction(std::unique_ptr<details::Instruction> impl);
225 std::unique_ptr<details::Instruction> impl_;
226};
227}
228}
229#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...
Iterator & operator=(const Iterator &)
details::InstructionIt implementation
Definition Instruction.hpp:47
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:190
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:88
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:199
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:750
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:79
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition Engine.hpp:32
Namespace related to assembly/disassembly support.
Definition Abstract/Binary.hpp:48
LIEF namespace.
Definition Abstract/Binary.hpp:41
Definition Abstract/Binary.hpp:36
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46