LIEF: Library to Instrument Executable Formats Version 1.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
18#include <map>
20#include "LIEF/visibility.h"
21
24
26#include "LIEF/COFF/Header.hpp"
27
28namespace LIEF {
29namespace COFF {
30class Binary;
31class Section;
32class String;
33class Symbol;
34
35class Parser {
36 public:
39 static LIEF_API std::unique_ptr<Binary>
40 parse(std::unique_ptr<BinaryStream> stream,
42
44 static std::unique_ptr<Binary>
45 parse(const std::string& file,
46 const ParserConfig& config = ParserConfig::default_conf()) {
47 if (auto strm = VectorStream::from_file(file)) {
48 return parse(
49 std::unique_ptr<VectorStream>(new VectorStream(std::move(*strm))), config
50 );
51 }
52 return nullptr;
53 }
54
56 struct SymSec {
57 size_t sec_idx = 0;
58 Symbol* symbol = nullptr;
59
60 friend bool operator<(const SymSec& lhs, const SymSec& rhs) {
61 return lhs.sec_idx < rhs.sec_idx;
62 }
63
64 friend bool operator==(const SymSec& lhs, const SymSec& rhs) {
65 return lhs.sec_idx == rhs.sec_idx && lhs.symbol == rhs.symbol;
66 }
67
68 friend bool operator!=(const SymSec& lhs, const SymSec& rhs) {
69 return !(lhs == rhs);
70 }
71 };
72
74 using SymSecMap = std::vector<SymSec>;
75
77 LIEF_LOCAL void memoize(String str);
78
80 LIEF_LOCAL String* find_coff_string(uint32_t offset) const;
81
83
84 private:
85 Parser(std::unique_ptr<BinaryStream> stream, const ParserConfig& config,
86 Header::KIND kind) :
87 stream_(std::move(stream)),
88 kind_(kind),
89 config_(config) {}
90
91 ok_error_t process();
92 ok_error_t parse_header();
93 ok_error_t parse_optional_header();
94 ok_error_t parse_sections();
95 ok_error_t parse_relocations(Section& section);
96 ok_error_t parse_symbols();
97 ok_error_t parse_string_table();
98
99 std::unique_ptr<BinaryStream> stream_;
100 std::unique_ptr<Binary> bin_;
102
103 std::map<uint32_t, size_t> memoize_coff_str_;
104 std::map<size_t, Symbol*> symbol_idx_;
105 SymSecMap symsec_;
106
108};
109}
110}
111#endif
Class that represents a COFF Binary.
Definition COFF/Binary.hpp:44
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:74
static std::unique_ptr< Binary > parse(const std::string &file, const ParserConfig &config=ParserConfig::default_conf())
Parse the COFF binary pointed by the file argument with the given config.
Definition COFF/Parser.hpp:45
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
Definition VectorStream.hpp:29
static result< VectorStream > from_file(const std::string &file)
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:114
#define LIEF_MAYBE_UNUSED
Definition compiler_attributes.hpp:61
Definition AuxiliarySymbol.hpp:29
LIEF namespace.
Definition Abstract/Binary.hpp:40
Definition string.h:155
#define LIEF_API
Definition visibility.h:43
#define LIEF_LOCAL
Definition visibility.h:44