LIEF: Library to Instrument Executable Formats Version 2.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
18#include "LIEF/errors.hpp"
19#include "LIEF/iterators.hpp"
20#include "LIEF/visibility.h"
21
22#include <string_view>
23#include <memory>
24#include <ostream>
25#include <string>
26
27namespace llvm {
28class MCInst;
29}
30
31
32namespace LIEF::assembly {
33
34namespace details {
35class Instruction;
36class InstructionIt;
37}
38
40class LIEF_API Instruction {
41 public:
43 class Iterator final
44 : public iterator_facade_base<Iterator, std::forward_iterator_tag, Instruction,
45 std::ptrdiff_t, const Instruction*,
46 const Instruction&> {
47 public:
48 using implementation = details::InstructionIt;
49 using iterator_facade_base::operator++;
50
52
53 LIEF_API Iterator(std::unique_ptr<details::InstructionIt> impl);
56
58 LIEF_API Iterator& operator=(Iterator&&) noexcept;
59
61
62 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
63 LIEF_API Iterator& operator++();
64
65 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS);
66
67 friend bool operator!=(const Iterator& LHS, const Iterator& RHS) {
68 return !(LHS == RHS);
69 }
70
71 LIEF_API const Instruction& operator*() const LIEF_LIFETIMEBOUND;
72
73 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
74 LIEF_API const Instruction* operator->() const LIEF_LIFETIMEBOUND;
75
78 LIEF_API std::unique_ptr<Instruction> yield();
79
80 private:
81 void load() const;
82
83 std::unique_ptr<details::InstructionIt> impl_;
84 mutable std::unique_ptr<Instruction> cached_;
85 };
86
87 public:
89 enum class MemoryAccess : uint8_t {
90 NONE = 0,
91 READ = 1 << 0,
92 WRITE = 1 << 1,
93 READ_WRITE = READ | WRITE,
94 };
95
97 uint64_t address() const;
98
100 size_t size() const;
101
103 const std::vector<uint8_t>& raw() const LIEF_LIFETIMEBOUND;
104
106 std::string_view mnemonic() const LIEF_LIFETIMEBOUND;
107
109 std::string to_string(bool with_address = true) const;
110
112 bool is_call() const;
113
115 bool is_terminator() const;
116
118 bool is_branch() const;
119
121 bool is_syscall() const;
122
124 bool is_memory_access() const;
125
127 bool is_move_reg() const;
128
130 bool is_add() const;
131
136 bool is_trap() const;
137
141 bool is_barrier() const;
142
144 bool is_return() const;
145
150 bool is_indirect_branch() const;
151
155
159
161 bool is_compare() const;
162
164 bool is_move_immediate() const;
165
167 bool is_bitcast() const;
168
171
174 result<uint64_t> branch_target() const;
175
180 const llvm::MCInst& mcinst() const LIEF_LIFETIMEBOUND;
181
190 template<class T>
191 const T* as() const LIEF_LIFETIMEBOUND {
192 static_assert(std::is_base_of_v<Instruction, T>,
193 "Require Instruction inheritance");
194 if (T::classof(this)) {
195 return static_cast<const T*>(this);
196 }
197 return nullptr;
198 }
199
200 friend LIEF_API std::ostream& operator<<(std::ostream& os,
201 const Instruction& inst) {
202 os << inst.to_string();
203 return os;
204 }
205
206 virtual ~Instruction();
207
209 static LIEF_LOCAL std::unique_ptr<Instruction>
210 create(std::unique_ptr<details::Instruction> impl);
211
213 LIEF_LOCAL const details::Instruction& impl() const LIEF_LIFETIMEBOUND {
214 assert(impl_ != nullptr);
215 return *impl_;
216 }
217
219 LIEF_LOCAL details::Instruction& impl() LIEF_LIFETIMEBOUND {
220 assert(impl_ != nullptr);
221 return *impl_;
222 }
223
224 protected:
225 LIEF_LOCAL Instruction(std::unique_ptr<details::Instruction> impl);
226 std::unique_ptr<details::Instruction> impl_;
227};
228}
229
230#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:48
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:191
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:89
bool is_branch() const
True if the instruction is a branch.
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.
std::string_view mnemonic() const
Instruction mnemonic (e.g. br).
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:200
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.
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:79
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition Engine.hpp:33
Namespace related to assembly/disassembly support.
Definition Abstract/Binary.hpp:49
Definition Abstract/Binary.hpp:37
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46