LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
COFF/Parser.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_PARSER_H
17#define LIEF_COFF_PARSER_H
20#include "LIEF/errors.hpp"
21#include "LIEF/path.hpp"
22#include "LIEF/visibility.h"
23
24#include <string_view>
25#include <map>
26
27#include "LIEF/COFF/Header.hpp"
29
30
31namespace LIEF::COFF {
32class Binary;
33class Section;
34class String;
35class Symbol;
36
37class Parser {
38 public:
39 static constexpr size_t MAX_NB_SECTIONS = 1 << 20;
40
43 static LIEF_API std::unique_ptr<Binary>
44 parse(std::unique_ptr<BinaryStream> stream,
46
48 static LIEF_API std::unique_ptr<Binary>
49 parse(std::string_view file,
51
54 template<class PathT, enable_if_path_t<PathT> = 0>
55 static std::unique_ptr<Binary>
56 parse(const PathT& file,
57 const ParserConfig& config = ParserConfig::default_conf()) {
58 return parse(file.string(), config);
59 }
60
62 struct SymSec {
63 size_t sec_idx = 0;
64 Symbol* symbol = nullptr;
65
66 friend bool operator<(SymSec lhs, SymSec rhs) {
67 return lhs.sec_idx < rhs.sec_idx;
68 }
69
70 friend bool operator==(SymSec lhs, SymSec rhs) {
71 return lhs.sec_idx == rhs.sec_idx && lhs.symbol == rhs.symbol;
72 }
73
74 friend bool operator!=(SymSec lhs, SymSec rhs) {
75 return !(lhs == rhs);
76 }
77 };
78
80 using SymSecMap = std::vector<SymSec>;
81
83 LIEF_LOCAL void memoize(String str);
84
86 LIEF_LOCAL String* find_coff_string(uint32_t offset) const;
87
89
90 private:
91 Parser(std::unique_ptr<BinaryStream> stream, const ParserConfig& config,
92 Header::KIND kind);
93
94 ok_error_t process();
95 ok_error_t parse_header();
96 ok_error_t parse_optional_header();
97 ok_error_t parse_sections();
98 ok_error_t parse_relocations(Section& section);
99 ok_error_t parse_symbols();
100 ok_error_t parse_string_table();
101
102 std::unique_ptr<BinaryStream> stream_;
103 std::unique_ptr<Binary> bin_;
105
106 std::map<uint32_t, size_t> memoize_coff_str_;
107 std::map<size_t, Symbol*> symbol_idx_;
108 SymSecMap symsec_;
109
111};
112}
113
114#endif
Class that represents a COFF Binary.
Definition COFF/Binary.hpp:46
KIND
Definition COFF/Header.hpp:35
@ UNKNOWN
Definition COFF/Header.hpp:36
Class used to configure the COFF parser.
Definition COFF/ParserConfig.hpp:24
static const ParserConfig & default_conf()
Definition COFF/ParserConfig.hpp:26
static std::unique_ptr< Binary > parse(std::unique_ptr< BinaryStream > stream, const ParserConfig &config=ParserConfig::default_conf())
Parse the COFF binary referenced by the stream argument with the given config.
std::vector< SymSec > SymSecMap
<=> std::unordered_multimap<section index, Symbol*>
Definition COFF/Parser.hpp:80
static std::unique_ptr< Binary > parse(const PathT &file, const ParserConfig &config=ParserConfig::default_conf())
Same as parse(std::string_view, const ParserConfig&) but the file is given as a std::filesystem::path...
Definition COFF/Parser.hpp:56
static constexpr size_t MAX_NB_SECTIONS
Definition COFF/Parser.hpp:39
static std::unique_ptr< Binary > parse(std::string_view file, const ParserConfig &config=ParserConfig::default_conf())
Parse the COFF binary pointed by the file argument with the given config.
This class represents a COFF section.
Definition COFF/Section.hpp:41
This class represents a string located in the COFF string table.
Definition String.hpp:35
This class represents a COFF symbol.
Definition COFF/Symbol.hpp:37
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:119
#define LIEF_MAYBE_UNUSED
Definition compiler_attributes.hpp:62
Definition AuxiliarySymbol.hpp:30
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46