LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
COFF/Binary.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_COFF_BINARY_H
17#define LIEF_COFF_BINARY_H
18#include "LIEF/visibility.h"
19#include "LIEF/iterators.hpp"
20#include "LIEF/span.hpp"
21
22#include "LIEF/COFF/String.hpp"
23
25
26#include <memory>
27#include <vector>
28#include <unordered_map>
29
30namespace LIEF {
31
32namespace assembly {
33class Engine;
34}
35
36namespace COFF {
37class Header;
38class Parser;
39class Section;
40class Relocation;
41class Symbol;
42class LIEF_API Binary {
45 public:
46 friend class Parser;
47 using sections_t = std::vector<std::unique_ptr<Section>>;
50 using it_sections = ref_iterator<sections_t&, Section*>;
53 using it_const_sections = const_ref_iterator<const sections_t&, const Section*>;
56 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
59 using it_relocations = ref_iterator<relocations_t&, Relocation*>;
62 using it_const_relocations = const_ref_iterator<const relocations_t&, const Relocation*>;
65 using strings_table_t = std::vector<String>;
68 using it_strings_table = ref_iterator<strings_table_t&>;
71 using it_const_strings_table = const_ref_iterator<const strings_table_t&>;
74 using symbols_t = std::vector<std::unique_ptr<Symbol>>;
77 using it_symbols = ref_iterator<symbols_t&, Symbol*>;
80 using it_const_symbols = const_ref_iterator<const symbols_t&, const Symbol*>;
83 using instructions_it = iterator_range<assembly::Instruction::Iterator>;
86 using it_functions = filter_iterator<symbols_t&, Symbol*>;
89 using it_const_function = const_filter_iterator<const symbols_t&, const Symbol*>;
92 const Header& header() const {
95 return *header_;
96 }
97
98 Header& header() {
99 return *header_;
100 }
101 it_sections sections() {
104 return sections_;
105 }
106
107 it_const_sections sections() const {
108 return sections_;
109 }
110 it_relocations relocations() {
113 return relocations_;
114 }
115
116 it_const_relocations relocations() const {
117 return relocations_;
118 }
119 it_symbols symbols() {
122 return symbols_;
123 }
124
125 it_const_symbols symbols() const {
126 return symbols_;
127 }
128 it_const_strings_table string_table() const {
131 return strings_table_;
132 }
133
134 it_strings_table string_table() {
135 return strings_table_;
136 }
137 String* find_string(uint32_t offset) {
143 auto it = std::find_if(strings_table_.begin(), strings_table_.end(),
144 [offset] (const String& item) {
145 return offset == item.offset();
146 }
147 );
148 return it == strings_table_.end() ? nullptr : &*it;
149 }
150
151 const String* find_string(uint32_t offset) const {
152 return const_cast<Binary*>(this)->find_string(offset);
153 }
154 it_const_function functions() const;
157
158 it_functions functions();
159 const Symbol* find_function(const std::string& name) const;
162
163 Symbol* find_function(const std::string& name) {
164 return const_cast<Symbol*>(static_cast<const Binary*>(this)->find_function(name));
165 }
166 const Symbol* find_demangled_function(const std::string& name) const;
169
170 Symbol* find_demangled_function(const std::string& name) {
171 return const_cast<Symbol*>(static_cast<const Binary*>(this)->find_demangled_function(name));
172 }
173 instructions_it disassemble(const Symbol& symbol) const;
186 instructions_it disassemble(const std::string& symbol) const;
198 instructions_it disassemble(const uint8_t* buffer, size_t size,
204 uint64_t address = 0) const;
205
206 instructions_it disassemble(const std::vector<uint8_t>& buffer,
212 uint64_t address = 0) const {
213 return disassemble(buffer.data(), buffer.size(), address);
214 }
215
216 instructions_it disassemble(LIEF::span<const uint8_t> buffer,
217 uint64_t address = 0) const {
218 return disassemble(buffer.data(), buffer.size(), address);
219 }
220
221 instructions_it disassemble(LIEF::span<uint8_t> buffer, uint64_t address = 0) const {
222 return disassemble(buffer.data(), buffer.size(), address);
223 }
224
225 std::string to_string() const;
226
227 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Binary& bin) {
228 os << bin.to_string();
229 return os;
230 }
231
232 ~Binary();
233
234 private:
235 Binary();
236 std::unique_ptr<Header> header_;
237 sections_t sections_;
238 relocations_t relocations_;
239 strings_table_t strings_table_;
240 symbols_t symbols_;
241
242 mutable std::unordered_map<uint32_t, std::unique_ptr<assembly::Engine>> engines_;
243
244 assembly::Engine* get_engine(uint64_t address) const;
245
246 template<uint32_t Key, class F>
247 LIEF_LOCAL assembly::Engine* get_cache_engine(uint64_t address, F&& f) const;
248};
249
250}
251}
252#endif
Instruction.hpp
String.hpp
LIEF::COFF::Binary
Class that represents a COFF Binary.
Definition COFF/Binary.hpp:44
LIEF::COFF::Binary::header
const Header & header() const
The COFF header.
Definition COFF/Binary.hpp:94
LIEF::COFF::Binary::symbols
it_symbols symbols()
Iterator over the COFF's symbols.
Definition COFF/Binary.hpp:121
LIEF::COFF::Binary::find_string
const String * find_string(uint32_t offset) const
Definition COFF/Binary.hpp:151
LIEF::COFF::Binary::disassemble
instructions_it disassemble(LIEF::span< uint8_t > buffer, uint64_t address=0) const
Definition COFF/Binary.hpp:221
LIEF::COFF::Binary::disassemble
instructions_it disassemble(LIEF::span< const uint8_t > buffer, uint64_t address=0) const
Definition COFF/Binary.hpp:216
LIEF::COFF::Binary::find_function
Symbol * find_function(const std::string &name)
Definition COFF/Binary.hpp:163
LIEF::COFF::Binary::sections
it_sections sections()
Iterator over the different sections located in this COFF binary.
Definition COFF/Binary.hpp:103
LIEF::COFF::Binary::symbols
it_const_symbols symbols() const
Definition COFF/Binary.hpp:125
LIEF::COFF::Binary::string_table
it_const_strings_table string_table() const
Iterator over the COFF's strings.
Definition COFF/Binary.hpp:130
LIEF::COFF::Binary::find_demangled_function
Symbol * find_demangled_function(const std::string &name)
Definition COFF/Binary.hpp:170
LIEF::COFF::Binary::~Binary
~Binary()
LIEF::COFF::Binary::disassemble
instructions_it disassemble(const uint8_t *buffer, size_t size, uint64_t address=0) const
Disassemble code provided by the given buffer at the specified address parameter.
LIEF::COFF::Binary::string_table
it_strings_table string_table()
Definition COFF/Binary.hpp:134
LIEF::COFF::Binary::relocations
it_const_relocations relocations() const
Definition COFF/Binary.hpp:116
LIEF::COFF::Binary::functions
it_const_function functions() const
Iterator over the functions implemented in this COFF.
LIEF::COFF::Binary::find_demangled_function
const Symbol * find_demangled_function(const std::string &name) const
Try to find the function (symbol) with the given demangled name.
LIEF::COFF::Binary::to_string
std::string to_string() const
LIEF::COFF::Binary::sections
it_const_sections sections() const
Definition COFF/Binary.hpp:107
LIEF::COFF::Binary::header
Header & header()
Definition COFF/Binary.hpp:98
LIEF::COFF::Binary::disassemble
instructions_it disassemble(const std::vector< uint8_t > &buffer, uint64_t address=0) const
Disassemble code provided by the given vector of bytes at the specified address parameter.
Definition COFF/Binary.hpp:211
LIEF::COFF::Binary::find_string
String * find_string(uint32_t offset)
Try to find the COFF string at the given offset in the COFF string table.
Definition COFF/Binary.hpp:142
LIEF::COFF::Binary::functions
it_functions functions()
LIEF::COFF::Binary::disassemble
instructions_it disassemble(const Symbol &symbol) const
Disassemble code for the given symbol.
LIEF::COFF::Binary::find_function
const Symbol * find_function(const std::string &name) const
Try to find the function (symbol) with the given name.
LIEF::COFF::Binary::disassemble
instructions_it disassemble(const std::string &symbol) const
Disassemble code for the given symbol name.
LIEF::COFF::Binary::operator<<
friend std::ostream & operator<<(std::ostream &os, const Binary &bin)
Definition COFF/Binary.hpp:227
LIEF::COFF::Binary::relocations
it_relocations relocations()
Iterator over all the relocations used by this COFF binary.
Definition COFF/Binary.hpp:112
LIEF::COFF::Header
Class that represents the COFF header. It is subclassed by LIEF::COFF::RegularHeader and LIEF::COFF::...
Definition COFF/Header.hpp:33
LIEF::COFF::Parser
Definition COFF/Parser.hpp:34
LIEF::COFF::Relocation
This class represents a COFF relocation.
Definition COFF/Relocation.hpp:34
LIEF::COFF::Section
This class represents a COFF section.
Definition COFF/Section.hpp:39
LIEF::COFF::Symbol
This class represents a COFF symbol.
Definition COFF/Symbol.hpp:35
LIEF::assembly::Engine
This class interfaces the assembler/disassembler support.
Definition Engine.hpp:34
iterators.hpp
LIEF::COFF
Definition AuxiliarySymbol.hpp:29
LIEF::assembly
Namespace related to assembly/disassembly support.
Definition Abstract/Binary.hpp:46
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:39
LIEF::span
tcb::span< ElementType, Extent > span
Definition span.hpp:22
span.hpp
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42