LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
Instruction.hpp
Go to the documentation of this file.
1/* Copyright 2022 - 2024 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}
39 public: class Iterator final :
42 public iterator_facade_base<Iterator, std::forward_iterator_tag, std::unique_ptr<Instruction>,
43 std::ptrdiff_t, Instruction*, std::unique_ptr<Instruction>>
44 {
45 public:
46 using implementation = details::InstructionIt;
47
49
50 LIEF_API Iterator(std::unique_ptr<details::InstructionIt> impl);
51 LIEF_API Iterator(const Iterator&);
52 LIEF_API Iterator& operator=(const Iterator&);
53
54 LIEF_API Iterator(Iterator&&) noexcept;
55 LIEF_API Iterator& operator=(Iterator&&) noexcept;
56
57 LIEF_API ~Iterator();
58
59 LIEF_API Iterator& operator++();
60
61 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS);
62
63 friend bool operator!=(const Iterator& LHS, const Iterator& RHS) {
64 return !(LHS == RHS);
65 }
66 LIEF_API std::unique_ptr<Instruction> operator*() const;
70
71 private:
72 std::unique_ptr<details::InstructionIt> impl_;
73 };
74 public: enum class MemoryAccess : uint8_t {
77 NONE = 0,
78 READ = 1 << 0,
79 WRITE = 1 << 1,
80 READ_WRITE = READ | WRITE,
81 };
82 uint64_t address() const;
85 size_t size() const;
88 const std::vector<uint8_t>& raw() const;
91 std::string mnemonic() const;
94 std::string to_string(bool with_address = true) const;
97 bool is_call() const;
100 bool is_terminator() const;
103 bool is_branch() const;
106 bool is_syscall() const;
109 bool is_memory_access() const;
112 bool is_move_reg() const;
115 bool is_add() const;
118 bool is_trap() const;
124 bool is_barrier() const;
129 bool is_return() const;
132 bool is_indirect_branch() const;
138 bool is_conditional_branch() const;
142 bool is_unconditional_branch() const;
146 bool is_compare() const;
149 bool is_move_immediate() const;
152 bool is_bitcast() const;
155 MemoryAccess memory_access() const;
158 result<uint64_t> branch_target() const;
162 const llvm::MCInst& mcinst() const;
168 template<class T>
178 const T* as() const {
179 static_assert(std::is_base_of<Instruction, T>::value,
180 "Require Instruction inheritance");
181 if (T::classof(this)) {
182 return static_cast<const T*>(this);
183 }
184 return nullptr;
185 }
186
187 friend LIEF_API std::ostream& operator<<(std::ostream& os, const Instruction& inst) {
188 os << inst.to_string();
189 return os;
190 }
191
192 virtual ~Instruction();
193 static LIEF_LOCAL std::unique_ptr<Instruction>
196 create(std::unique_ptr<details::Instruction> impl);
197 LIEF_LOCAL const details::Instruction& impl() const {
200 assert(impl_ != nullptr);
201 return *impl_;
202 }
203 LIEF_LOCAL details::Instruction& impl() {
206 assert(impl_ != nullptr);
207 return *impl_;
208 }
209
210 protected:
211 LIEF_LOCAL Instruction(std::unique_ptr<details::Instruction> impl);
212 std::unique_ptr<details::Instruction> impl_;
213};
214}
215}
216#endif
LIEF::assembly::Instruction::Iterator::Iterator
Iterator()
LIEF::assembly::Instruction::Iterator::operator=
Iterator & operator=(const Iterator &)
LIEF::assembly::Instruction::Iterator::Iterator
Iterator(Iterator &&) noexcept
LIEF::assembly::Instruction::Iterator::Iterator
Iterator(const Iterator &)
LIEF::assembly::Instruction::Iterator::operator*
std::unique_ptr< Instruction > operator*() const
Disassemble and output an Instruction at the current iterator's position.
LIEF::assembly::Instruction::Iterator::Iterator
Iterator(std::unique_ptr< details::InstructionIt > impl)
LIEF::assembly::Instruction
This class represents an assembly instruction.
Definition Instruction.hpp:38
LIEF::assembly::Instruction::memory_access
MemoryAccess memory_access() const
Memory access flags.
LIEF::assembly::Instruction::is_move_reg
bool is_move_reg() const
True if the instruction is a register to register move.
LIEF::assembly::Instruction::is_terminator
bool is_terminator() const
True if the instruction marks the end of a basic block.
LIEF::assembly::Instruction::as
const T * as() const
This function can be used to down cast an Instruction instance:
Definition Instruction.hpp:178
LIEF::assembly::Instruction::is_compare
bool is_compare() const
True if the instruction is a comparison.
LIEF::assembly::Instruction::is_conditional_branch
bool is_conditional_branch() const
True if the instruction is conditionally jumping to the next instruction or an instruction into some ...
LIEF::assembly::Instruction::is_move_immediate
bool is_move_immediate() const
True if the instruction is moving an immediate.
LIEF::assembly::Instruction::is_syscall
bool is_syscall() const
True if the instruction is a syscall.
LIEF::assembly::Instruction::is_indirect_branch
bool is_indirect_branch() const
True if the instruction is and indirect branch.
LIEF::assembly::Instruction::~Instruction
virtual ~Instruction()
LIEF::assembly::Instruction::mcinst
const llvm::MCInst & mcinst() const
Return the underlying llvm::MCInst implementation.
LIEF::assembly::Instruction::is_branch
bool is_branch() const
True if the instruction is a branch.
LIEF::assembly::Instruction::mnemonic
std::string mnemonic() const
Instruction mnemonic (e.g. br)
LIEF::assembly::Instruction::to_string
std::string to_string(bool with_address=true) const
Representation of the current instruction in a pretty assembly way.
LIEF::assembly::Instruction::is_bitcast
bool is_bitcast() const
True if the instruction is doing a bitcast.
LIEF::assembly::Instruction::is_trap
bool is_trap() const
True if the instruction is a trap.
LIEF::assembly::Instruction::is_unconditional_branch
bool is_unconditional_branch() const
True if the instruction is jumping (unconditionally) to some other basic block.
LIEF::assembly::Instruction::is_return
bool is_return() const
True if the instruction is a return.
LIEF::assembly::Instruction::address
uint64_t address() const
Address of the instruction.
LIEF::assembly::Instruction::is_add
bool is_add() const
True if the instruction performs an arithmetic addition.
LIEF::assembly::Instruction::is_call
bool is_call() const
True if the instruction is a call.
LIEF::assembly::Instruction::raw
const std::vector< uint8_t > & raw() const
Raw bytes of the current instruction.
LIEF::assembly::Instruction::operator<<
friend std::ostream & operator<<(std::ostream &os, const Instruction &inst)
Definition Instruction.hpp:187
LIEF::assembly::Instruction::is_memory_access
bool is_memory_access() const
True if the instruction performs a memory access.
LIEF::assembly::Instruction::is_barrier
bool is_barrier() const
True if the instruction prevents executing the instruction that immediatly follows the current....
LIEF::assembly::Instruction::branch_target
result< uint64_t > branch_target() const
Given a is_branch() instruction, try to evaluate the address of the destination.
LIEF::assembly::Instruction::size
size_t size() const
Size of the instruction in bytes.
errors.hpp
iterators.hpp
LIEF::assembly::details
Definition Engine.hpp:29
LIEF::assembly
Namespace related to assembly/disassembly support.
Definition Abstract/Binary.hpp:43
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
llvm
Definition Instruction.hpp:25
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42