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 *
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_PARSER_H
16#define LIEF_COFF_PARSER_H
19#include "LIEF/errors.hpp"
20#include "LIEF/path.hpp"
21#include "LIEF/visibility.h"
22
23#include <string_view>
24#include <map>
25
26#include "LIEF/COFF/Header.hpp"
28
29
30namespace LIEF::COFF {
31class Binary;
32class Section;
33class String;
34class Symbol;
35
36class Parser {
37 public:
38 static constexpr size_t MAX_NB_SECTIONS = 1 << 20;
39
42 static LIEF_API std::unique_ptr<Binary>
43 parse(std::unique_ptr<BinaryStream> stream,
45
47 static LIEF_API std::unique_ptr<Binary>
48 parse(std::string_view file,
50
53 template<class PathT, enable_if_path_t<PathT> = 0>
54 static std::unique_ptr<Binary>
55 parse(const PathT& file,
56 const ParserConfig& config = ParserConfig::default_conf()) {
57 return parse(file.string(), config);
58 }
59
61 struct SymSec {
62 size_t sec_idx = 0;
63 Symbol* symbol = nullptr;
64
65 friend bool operator<(SymSec lhs, SymSec rhs) {
66 return lhs.sec_idx < rhs.sec_idx;
67 }
68
69 friend bool operator==(SymSec lhs, SymSec rhs) {
70 return lhs.sec_idx == rhs.sec_idx && lhs.symbol == rhs.symbol;
71 }
72
73 friend bool operator!=(SymSec lhs, SymSec rhs) {
74 return !(lhs == rhs);
75 }
76 };
77
79 using SymSecMap = std::vector<SymSec>;
80
82 LIEF_LOCAL void memoize(String str);
83
85 LIEF_LOCAL String* find_coff_string(uint32_t offset) const;
86
88
89 private:
90 Parser(std::unique_ptr<BinaryStream> stream, const ParserConfig& config,
91 Header::KIND kind);
92
93 ok_error_t process();
94 ok_error_t parse_header();
95 ok_error_t parse_optional_header();
96 ok_error_t parse_sections();
97 ok_error_t parse_relocations(Section& section);
98 ok_error_t parse_symbols();
99 ok_error_t parse_string_table();
100
101 std::unique_ptr<BinaryStream> stream_;
102 std::unique_ptr<Binary> bin_;
104
105 std::map<uint32_t, size_t> memoize_coff_str_;
106 std::map<size_t, Symbol*> symbol_idx_;
107 SymSecMap symsec_;
108
110};
111}
112
113#endif
Class that represents a COFF Binary.
Definition COFF/Binary.hpp:45
KIND
Definition COFF/Header.hpp:34
@ UNKNOWN
Definition COFF/Header.hpp:35
Class used to configure the COFF parser.
Definition COFF/ParserConfig.hpp:23
static const ParserConfig & default_conf()
Definition COFF/ParserConfig.hpp:25
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:79
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:55
static constexpr size_t MAX_NB_SECTIONS
Definition COFF/Parser.hpp:38
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: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
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:118
#define LIEF_MAYBE_UNUSED
Definition compiler_attributes.hpp:62
Definition AuxiliarySymbol.hpp:29
#define LIEF_API
Definition visibility.h:44
#define LIEF_LOCAL
Definition visibility.h:45