LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
COFF/Binary.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 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_COFF_BINARY_H
16#define LIEF_COFF_BINARY_H
17#include "LIEF/iterators.hpp"
18#include "LIEF/span.hpp"
19#include "LIEF/visibility.h"
20
21#include "LIEF/COFF/String.hpp"
22
24
25#include <string_view>
26#include <unordered_map>
27#include <memory>
28#include <string>
29#include <vector>
30
31namespace LIEF {
32
33namespace assembly {
34class Engine;
35}
36
37namespace COFF {
38class Header;
39class Parser;
40class Section;
41class Relocation;
42class Symbol;
43
45class LIEF_API Binary {
46 public:
47 friend class Parser;
48
50 using sections_t = std::vector<std::unique_ptr<Section>>;
51
54
57
59 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
60
63
67
69 using strings_table_t = std::vector<String>;
70
73
76
78 using symbols_t = std::vector<std::unique_ptr<Symbol>>;
79
82
85
88
91
94
96 const Header& header() const {
97 return *header_;
98 }
99
101 return *header_;
102 }
103
106 return sections_;
107 }
108
110 return sections_;
111 }
112
115 return relocations_;
116 }
117
119 return relocations_;
120 }
121
124 return symbols_;
125 }
126
128 return symbols_;
129 }
130
133 return strings_table_;
134 }
135
137 return strings_table_;
138 }
139
145 auto it = std::find_if(strings_table_.begin(), strings_table_.end(),
146 [offset](const String& item) {
147 return offset == item.offset();
148 });
149 return it == strings_table_.end() ? nullptr : &*it;
150 }
151
152 const String* find_string(uint32_t offset) const LIEF_LIFETIMEBOUND {
153 return const_cast<Binary*>(this)->find_string(offset);
154 }
155
158
160
162 const Symbol* find_function(std::string_view name) const LIEF_LIFETIMEBOUND;
163
164 Symbol* find_function(std::string_view name) LIEF_LIFETIMEBOUND {
165 return const_cast<Symbol*>(
166 static_cast<const Binary*>(this)->find_function(name)
167 );
168 }
169
171 const Symbol*
172 find_demangled_function(std::string_view name) const LIEF_LIFETIMEBOUND;
173
175 return const_cast<Symbol*>(
176 static_cast<const Binary*>(this)->find_demangled_function(name)
177 );
178 }
179
192
203 instructions_it disassemble(std::string_view symbol) const LIEF_LIFETIMEBOUND;
204
212 size_t size,
213 uint64_t address = 0) const LIEF_LIFETIMEBOUND;
214
215
221 disassemble(const std::vector<uint8_t>& buffer LIEF_LIFETIMEBOUND,
222 uint64_t address = 0) const LIEF_LIFETIMEBOUND {
223 return disassemble(buffer.data(), buffer.size(), address);
224 }
225
227 uint64_t address = 0) const LIEF_LIFETIMEBOUND {
228 return disassemble(buffer.data(), buffer.size(), address);
229 }
230
232 uint64_t address = 0) const LIEF_LIFETIMEBOUND {
233 return disassemble(buffer.data(), buffer.size(), address);
234 }
235
251 Section* get_section(std::string_view name) LIEF_LIFETIMEBOUND {
252 return const_cast<Section*>(
253 static_cast<const Binary*>(this)->get_section(name)
254 );
255 }
256
257 const Section* get_section(std::string_view name) const LIEF_LIFETIMEBOUND;
258
259 std::string to_string() const;
260
261 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Binary& bin) {
262 os << bin.to_string();
263 return os;
264 }
265
267
268 private:
269 Binary();
270 std::unique_ptr<Header> header_;
271 sections_t sections_;
272 relocations_t relocations_;
273 strings_table_t strings_table_;
274 symbols_t symbols_;
275
276 mutable std::unordered_map<uint32_t, std::unique_ptr<assembly::Engine>> engines_;
277
278 assembly::Engine* get_engine(uint64_t address) const;
279
280 template<uint32_t Key, class F>
281 LIEF_LOCAL assembly::Engine* get_cache_engine(uint64_t address, F&& f) const;
282};
283
284}
285}
286#endif
std::vector< std::unique_ptr< Symbol > > symbols_t
Internal container used to store COFF's symbols.
Definition COFF/Binary.hpp:78
ref_iterator< relocations_t &, Relocation * > it_relocations
Iterator that outputs Relocation& object.
Definition COFF/Binary.hpp:62
const Header & header() const
The COFF header.
Definition COFF/Binary.hpp:96
it_symbols symbols()
Iterator over the COFF's symbols.
Definition COFF/Binary.hpp:123
const Symbol * find_demangled_function(std::string_view name) const
Try to find the function (symbol) with the given demangled name.
const_ref_iterator< const relocations_t &, const Relocation * > it_const_relocations
Iterator that outputs const Relocation& object.
Definition COFF/Binary.hpp:65
const String * find_string(uint32_t offset) const
Definition COFF/Binary.hpp:152
iterator_range< assembly::Instruction::Iterator > instructions_it
Instruction iterator.
Definition COFF/Binary.hpp:87
instructions_it disassemble(LIEF::span< uint8_t > buffer, uint64_t address=0) const
Definition COFF/Binary.hpp:231
instructions_it disassemble(LIEF::span< const uint8_t > buffer, uint64_t address=0) const
Definition COFF/Binary.hpp:226
const_ref_iterator< const strings_table_t & > it_const_strings_table
Iterator that outputs const String& object.
Definition COFF/Binary.hpp:75
it_sections sections()
Iterator over the different sections located in this COFF binary.
Definition COFF/Binary.hpp:105
it_const_symbols symbols() const
Definition COFF/Binary.hpp:127
it_const_strings_table string_table() const
Iterator over the COFF's strings.
Definition COFF/Binary.hpp:132
ref_iterator< strings_table_t & > it_strings_table
Iterator that outputs String& object.
Definition COFF/Binary.hpp:72
filter_iterator< symbols_t &, Symbol * > it_functions
Iterator which outputs COFF symbols representing functions.
Definition COFF/Binary.hpp:90
const_filter_iterator< const symbols_t &, const Symbol * > it_const_function
Iterator which outputs COFF symbols representing functions.
Definition COFF/Binary.hpp:93
ref_iterator< symbols_t &, Symbol * > it_symbols
Iterator that outputs Symbol& object.
Definition COFF/Binary.hpp:81
const_ref_iterator< const sections_t &, const Section * > it_const_sections
Iterator that outputs const Section& object.
Definition COFF/Binary.hpp:56
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.
const_ref_iterator< const symbols_t &, const Symbol * > it_const_symbols
Iterator that outputs Symbol& object.
Definition COFF/Binary.hpp:84
const Section * get_section(std::string_view name) const
it_strings_table string_table()
Definition COFF/Binary.hpp:136
it_const_relocations relocations() const
Definition COFF/Binary.hpp:118
Symbol * find_demangled_function(std::string_view name)
Definition COFF/Binary.hpp:174
it_const_function functions() const
Iterator over the functions implemented in this COFF.
const Symbol * find_function(std::string_view name) const
Try to find the function (symbol) with the given name.
friend class Parser
Definition COFF/Binary.hpp:47
instructions_it disassemble(std::string_view symbol) const
Disassemble code for the given symbol name.
std::string to_string() const
it_const_sections sections() const
Definition COFF/Binary.hpp:109
Section * get_section(std::string_view name)
Return the section matching the given name or a nullptr if it can't be found.
Definition COFF/Binary.hpp:251
Header & header()
Definition COFF/Binary.hpp:100
ref_iterator< sections_t &, Section * > it_sections
Iterator that outputs Section& object.
Definition COFF/Binary.hpp:53
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:144
instructions_it disassemble(const Symbol &symbol) const
Disassemble code for the given symbol.
std::vector< String > strings_table_t
Internal container used to store COFF's strings.
Definition COFF/Binary.hpp:69
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container used to store COFF's relocations.
Definition COFF/Binary.hpp:59
friend std::ostream & operator<<(std::ostream &os, const Binary &bin)
Definition COFF/Binary.hpp:261
std::vector< std::unique_ptr< Section > > sections_t
Internal container used to store COFF's section.
Definition COFF/Binary.hpp:50
it_relocations relocations()
Iterator over all the relocations used by this COFF binary.
Definition COFF/Binary.hpp:114
Class that represents the COFF header. It is subclassed by LIEF::COFF::RegularHeader and LIEF::COFF::...
Definition COFF/Header.hpp:32
Definition COFF/Parser.hpp:36
This class represents a COFF relocation.
Definition COFF/Relocation.hpp:33
This class represents a COFF section.
Definition COFF/Section.hpp:40
This class represents a string located in the COFF string table.
Definition String.hpp:34
This class represents a COFF symbol.
Definition COFF/Symbol.hpp:36
This class interfaces the assembler/disassembler support.
Definition Engine.hpp:38
Iterator which returns a ref on container's values given predicates.
Definition iterators.hpp:392
Definition iterators.hpp:680
Iterator which returns reference on container's values.
Definition iterators.hpp:120
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition AuxiliarySymbol.hpp:29
Namespace related to assembly/disassembly support.
Definition Abstract/Binary.hpp:48
LIEF namespace.
Definition Abstract/Binary.hpp:41
filter_iterator< CT, U, typename decay_t< CT >::const_iterator > const_filter_iterator
Iterator which returns a const ref on container's values given predicates.
Definition iterators.hpp:672
tcb::span< ElementType, Extent > span
Definition span.hpp:22
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which returns a const ref on container's values.
Definition iterators.hpp:385
#define LIEF_API
Definition visibility.h:44
#define LIEF_LOCAL
Definition visibility.h:45