LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
COFF/Binary.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 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;
42
44class LIEF_API Binary {
45 public:
46 friend class Parser;
47
49 using sections_t = std::vector<std::unique_ptr<Section>>;
50
53
56
58 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
59
62
66
68 using strings_table_t = std::vector<String>;
69
72
75
77 using symbols_t = std::vector<std::unique_ptr<Symbol>>;
78
81
84
87
90
93
95 const Header& header() const {
96 return *header_;
97 }
98
100 return *header_;
101 }
102
105 return sections_;
106 }
107
109 return sections_;
110 }
111
114 return relocations_;
115 }
116
118 return relocations_;
119 }
120
123 return symbols_;
124 }
125
127 return symbols_;
128 }
129
132 return strings_table_;
133 }
134
136 return strings_table_;
137 }
138
143 String* find_string(uint32_t offset) {
144 auto it = std::find_if(strings_table_.begin(), strings_table_.end(),
145 [offset](const String& item) {
146 return offset == item.offset();
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
157
159
161 const Symbol* find_function(const std::string& name) const;
162
163 Symbol* find_function(const std::string& name) {
164 return const_cast<Symbol*>(
165 static_cast<const Binary*>(this)->find_function(name)
166 );
167 }
168
170 const Symbol* find_demangled_function(const std::string& name) const;
171
172 Symbol* find_demangled_function(const std::string& name) {
173 return const_cast<Symbol*>(
174 static_cast<const Binary*>(this)->find_demangled_function(name)
175 );
176 }
177
189 instructions_it disassemble(const Symbol& symbol) const;
190
201 instructions_it disassemble(const std::string& symbol) const;
202
207 instructions_it disassemble(const uint8_t* buffer, size_t size,
208 uint64_t address = 0) const;
209
210
215 instructions_it disassemble(const std::vector<uint8_t>& buffer,
216 uint64_t address = 0) const {
217 return disassemble(buffer.data(), buffer.size(), address);
218 }
219
221 uint64_t address = 0) const {
222 return disassemble(buffer.data(), buffer.size(), address);
223 }
224
226 uint64_t address = 0) const {
227 return disassemble(buffer.data(), buffer.size(), address);
228 }
229
230 std::string to_string() const;
231
232 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Binary& bin) {
233 os << bin.to_string();
234 return os;
235 }
236
238
239 private:
240 Binary();
241 std::unique_ptr<Header> header_;
242 sections_t sections_;
243 relocations_t relocations_;
244 strings_table_t strings_table_;
245 symbols_t symbols_;
246
247 mutable std::unordered_map<uint32_t, std::unique_ptr<assembly::Engine>> engines_;
248
249 assembly::Engine* get_engine(uint64_t address) const;
250
251 template<uint32_t Key, class F>
252 LIEF_LOCAL assembly::Engine* get_cache_engine(uint64_t address, F&& f) const;
253};
254
255}
256}
257#endif
std::vector< std::unique_ptr< Symbol > > symbols_t
Internal container used to store COFF's symbols.
Definition COFF/Binary.hpp:77
ref_iterator< relocations_t &, Relocation * > it_relocations
Iterator that outputs Relocation& object.
Definition COFF/Binary.hpp:61
const Header & header() const
The COFF header.
Definition COFF/Binary.hpp:95
it_symbols symbols()
Iterator over the COFF's symbols.
Definition COFF/Binary.hpp:122
const_ref_iterator< const relocations_t &, const Relocation * > it_const_relocations
Iterator that outputs const Relocation& object.
Definition COFF/Binary.hpp:64
const String * find_string(uint32_t offset) const
Definition COFF/Binary.hpp:151
iterator_range< assembly::Instruction::Iterator > instructions_it
Instruction iterator.
Definition COFF/Binary.hpp:86
instructions_it disassemble(LIEF::span< uint8_t > buffer, uint64_t address=0) const
Definition COFF/Binary.hpp:225
instructions_it disassemble(LIEF::span< const uint8_t > buffer, uint64_t address=0) const
Definition COFF/Binary.hpp:220
const_ref_iterator< const strings_table_t & > it_const_strings_table
Iterator that outputs const String& object.
Definition COFF/Binary.hpp:74
Symbol * find_function(const std::string &name)
Definition COFF/Binary.hpp:163
it_sections sections()
Iterator over the different sections located in this COFF binary.
Definition COFF/Binary.hpp:104
it_const_symbols symbols() const
Definition COFF/Binary.hpp:126
it_const_strings_table string_table() const
Iterator over the COFF's strings.
Definition COFF/Binary.hpp:131
ref_iterator< strings_table_t & > it_strings_table
Iterator that outputs String& object.
Definition COFF/Binary.hpp:71
Symbol * find_demangled_function(const std::string &name)
Definition COFF/Binary.hpp:172
filter_iterator< symbols_t &, Symbol * > it_functions
Iterator which outputs COFF symbols representing functions.
Definition COFF/Binary.hpp:89
const_filter_iterator< const symbols_t &, const Symbol * > it_const_function
Iterator which outputs COFF symbols representing functions.
Definition COFF/Binary.hpp:92
ref_iterator< symbols_t &, Symbol * > it_symbols
Iterator that outputs Symbol& object.
Definition COFF/Binary.hpp:80
const_ref_iterator< const sections_t &, const Section * > it_const_sections
Iterator that outputs const Section& object.
Definition COFF/Binary.hpp:55
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:83
it_strings_table string_table()
Definition COFF/Binary.hpp:135
it_const_relocations relocations() const
Definition COFF/Binary.hpp:117
it_const_function functions() const
Iterator over the functions implemented in this COFF.
const Symbol * find_demangled_function(const std::string &name) const
Try to find the function (symbol) with the given demangled name.
friend class Parser
Definition COFF/Binary.hpp:46
std::string to_string() const
it_const_sections sections() const
Definition COFF/Binary.hpp:108
Header & header()
Definition COFF/Binary.hpp:99
ref_iterator< sections_t &, Section * > it_sections
Iterator that outputs Section& object.
Definition COFF/Binary.hpp:52
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:215
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:143
it_functions functions()
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:68
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container used to store COFF's relocations.
Definition COFF/Binary.hpp:58
const Symbol * find_function(const std::string &name) const
Try to find the function (symbol) with the given name.
instructions_it disassemble(const std::string &symbol) const
Disassemble code for the given symbol name.
friend std::ostream & operator<<(std::ostream &os, const Binary &bin)
Definition COFF/Binary.hpp:232
std::vector< std::unique_ptr< Section > > sections_t
Internal container used to store COFF's section.
Definition COFF/Binary.hpp:49
it_relocations relocations()
Iterator over all the relocations used by this COFF binary.
Definition COFF/Binary.hpp:113
Class that represents the COFF header. It is subclassed by LIEF::COFF::RegularHeader and LIEF::COFF::...
Definition COFF/Header.hpp:33
Definition COFF/Parser.hpp:35
This class represents a COFF relocation.
Definition COFF/Relocation.hpp:34
This class represents a COFF section.
Definition COFF/Section.hpp:39
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:35
This class interfaces the assembler/disassembler support.
Definition Engine.hpp:36
Iterator which return a ref on container's values given predicates.
Definition iterators.hpp:293
Definition iterators.hpp:567
Iterator which returns reference on container's values.
Definition iterators.hpp:45
Definition AuxiliarySymbol.hpp:29
Namespace related to assembly/disassembly support.
Definition Abstract/Binary.hpp:47
LIEF namespace.
Definition Abstract/Binary.hpp:40
filter_iterator< CT, U, typename decay_t< CT >::const_iterator > const_filter_iterator
Iterator which return a const ref on container's values given predicates.
Definition iterators.hpp:559
tcb::span< ElementType, Extent > span
Definition span.hpp:22
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which return const ref on container's values.
Definition iterators.hpp:286
#define LIEF_API
Definition visibility.h:43
#define LIEF_LOCAL
Definition visibility.h:44