LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
Export.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_PE_EXPORT_H
17#define LIEF_PE_EXPORT_H
18
19#include <string_view>
20#include <memory>
21#include <ostream>
22#include <string>
23
24#include "LIEF/Object.hpp"
27#include "LIEF/iterators.hpp"
28#include "LIEF/visibility.h"
29
30
31namespace LIEF::PE {
32
33class Builder;
34class Parser;
35
36namespace details {
37struct pe_export_directory_table;
38}
39
41class LIEF_API Export : public Object {
42 friend class Builder;
43 friend class Parser;
44
45 public:
46 using entries_t = std::vector<std::unique_ptr<ExportEntry>>;
50
51 Export() = default;
52
53 Export(std::string name, const std::vector<ExportEntry>& entries) :
54 name_(std::move(name)) {
55 for (const ExportEntry& E : entries) {
56 add_entry(E);
57 }
58 }
59
60 Export(std::string name) :
61 Export(std::move(name), {}) {}
62
63 Export(const details::pe_export_directory_table& header);
64
65 Export(const Export&);
67
68 Export(Export&&) = default;
69 Export& operator=(Export&&) = default;
70
71 ~Export() override = default;
72
75 uint32_t export_flags() const {
76 return export_flags_;
77 }
78
80 uint32_t timestamp() const {
81 return timestamp_;
82 }
83
85 uint16_t major_version() const {
86 return major_version_;
87 }
88
90 uint16_t minor_version() const {
91 return minor_version_;
92 }
93
95 uint32_t ordinal_base() const {
96 return ordinal_base_;
97 }
98
100 std::string_view name() const LIEF_LIFETIMEBOUND {
101 return name_;
102 }
103
106 return entries_;
107 }
108
110 return entries_;
111 }
112
114 uint32_t name_rva() const {
115 return name_rva_;
116 }
117
119 uint32_t export_addr_table_rva() const {
120 return exp_addr_table_rva_;
121 }
122
124 uint32_t export_addr_table_cnt() const {
125 return exp_addr_table_cnt_;
126 }
127
129 uint32_t names_addr_table_rva() const {
130 return names_addr_table_rva_;
131 }
132
134 uint32_t names_addr_table_cnt() const {
135 return names_addr_table_cnt_;
136 }
137
139 uint32_t ord_addr_table_rva() const {
140 return ord_addr_table_rva_;
141 }
142
143 void export_flags(uint32_t flags) {
144 export_flags_ = flags;
145 }
146
147 void timestamp(uint32_t timestamp) {
148 timestamp_ = timestamp;
149 }
150
152 major_version_ = major_version;
153 }
154
156 minor_version_ = minor_version;
157 }
158
159 void ordinal_base(uint32_t ordinal_base) {
160 ordinal_base_ = ordinal_base;
161 }
162
163 void name(std::string name) {
164 name_ = std::move(name);
165 }
166
168 const ExportEntry* find_entry(const std::string& name) const LIEF_LIFETIMEBOUND;
169
171 return const_cast<ExportEntry*>(
172 static_cast<const Export*>(this)->find_entry(name)
173 );
174 }
175
177 const ExportEntry* find_entry(uint32_t ordinal) const LIEF_LIFETIMEBOUND;
178
180 return const_cast<ExportEntry*>(
181 static_cast<const Export*>(this)->find_entry(ordinal)
182 );
183 }
184
186 const ExportEntry* find_entry_at(uint32_t rva) const LIEF_LIFETIMEBOUND;
187
189 return const_cast<ExportEntry*>(
190 static_cast<const Export*>(this)->find_entry_at(rva)
191 );
192 }
193
196
197 ExportEntry& add_entry(std::string name, uint32_t rva) LIEF_LIFETIMEBOUND {
198 return add_entry(ExportEntry(std::move(name), rva));
199 }
200
202 bool remove_entry(const ExportEntry& exp);
203
205 bool remove_entry(const std::string& name) {
206 if (const ExportEntry* entry = find_entry(name)) {
207 return remove_entry(*entry);
208 }
209 return false;
210 }
211
213 bool remove_entry(uint32_t rva) {
214 if (const ExportEntry* entry = find_entry_at(rva)) {
215 return remove_entry(*entry);
216 }
217 return false;
218 }
219
220 void accept(Visitor& visitor) const override;
221
222 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Export& exp);
223
224 private:
225 uint32_t export_flags_ = 0;
226 uint32_t name_rva_ = 0;
227 uint32_t timestamp_ = 0;
228 uint16_t major_version_ = 0;
229 uint16_t minor_version_ = 0;
230 uint32_t ordinal_base_ = 0;
231 uint32_t exp_addr_table_rva_ = 0;
232 uint32_t exp_addr_table_cnt_ = 0;
233 uint32_t names_addr_table_rva_ = 0;
234 uint32_t names_addr_table_cnt_ = 0;
235 uint32_t ord_addr_table_rva_ = 0;
236 uint32_t max_ordinal_ = 0;
237 entries_t entries_;
238 std::string name_;
239};
240
241}
242
243
244#endif
Class that is used to rebuild a raw PE binary from a PE::Binary object.
Definition PE/Builder.hpp:44
Class which represents a PE Export entry (cf. PE::Export).
Definition ExportEntry.hpp:33
uint32_t ord_addr_table_rva() const
RVA to the list of exported ordinals.
Definition Export.hpp:139
uint32_t export_flags() const
According to the PE specifications this value is reserved and should be set to 0.
Definition Export.hpp:75
Export & operator=(const Export &)
void minor_version(uint16_t minor_version)
Definition Export.hpp:155
ExportEntry & add_entry(const ExportEntry &exp)
Add the given export and return the newly created and added export.
uint16_t major_version() const
The major version number (can be user-defined).
Definition Export.hpp:85
Export & operator=(Export &&)=default
bool remove_entry(uint32_t rva)
Remove the export entry with the given RVA.
Definition Export.hpp:213
uint16_t minor_version() const
The minor version number (can be user-defined).
Definition Export.hpp:90
Export()=default
Export(Export &&)=default
Export(const Export &)
uint32_t ordinal_base() const
The starting number for the exports. Usually this value is set to 1.
Definition Export.hpp:95
const ExportEntry * find_entry(const std::string &name) const
Find the export entry with the given name.
void name(std::string name)
Definition Export.hpp:163
ExportEntry & add_entry(std::string name, uint32_t rva)
Definition Export.hpp:197
void major_version(uint16_t major_version)
Definition Export.hpp:151
Export(const details::pe_export_directory_table &header)
const ExportEntry * find_entry(uint32_t ordinal) const
Find the export entry with the given ordinal number.
ref_iterator< entries_t &, ExportEntry * > it_entries
Definition Export.hpp:47
std::vector< std::unique_ptr< ExportEntry > > entries_t
Definition Export.hpp:46
Export(std::string name)
Definition Export.hpp:60
it_const_entries entries() const
Definition Export.hpp:109
void accept(Visitor &visitor) const override
void export_flags(uint32_t flags)
Definition Export.hpp:143
~Export() override=default
friend class Builder
Definition Export.hpp:42
Export(std::string name, const std::vector< ExportEntry > &entries)
Definition Export.hpp:53
uint32_t export_addr_table_cnt() const
Number of entries in the export address table.
Definition Export.hpp:124
const ExportEntry * find_entry_at(uint32_t rva) const
Find the export entry at the provided RVA.
uint32_t name_rva() const
Address of the ASCII DLL's name (RVA).
Definition Export.hpp:114
ExportEntry * find_entry(const std::string &name)
Definition Export.hpp:170
void ordinal_base(uint32_t ordinal_base)
Definition Export.hpp:159
std::string_view name() const
The name of the library exported (e.g. KERNEL32.dll).
Definition Export.hpp:100
friend class Parser
Definition Export.hpp:43
friend std::ostream & operator<<(std::ostream &os, const Export &exp)
bool remove_entry(const ExportEntry &exp)
Remove the given export entry.
uint32_t names_addr_table_rva() const
RVA to the list of exported names.
Definition Export.hpp:129
bool remove_entry(const std::string &name)
Remove the export entry with the given name.
Definition Export.hpp:205
it_entries entries()
Iterator over the ExportEntry.
Definition Export.hpp:105
uint32_t timestamp() const
The time and date that the export data was created.
Definition Export.hpp:80
const_ref_iterator< const entries_t &, const ExportEntry * > it_const_entries
Definition Export.hpp:48
uint32_t export_addr_table_rva() const
RVA of the export address table.
Definition Export.hpp:119
ExportEntry * find_entry_at(uint32_t rva)
Definition Export.hpp:188
void timestamp(uint32_t timestamp)
Definition Export.hpp:147
ExportEntry * find_entry(uint32_t ordinal)
Definition Export.hpp:179
uint32_t names_addr_table_cnt() const
Number of exports by name.
Definition Export.hpp:134
Main interface to parse PE binaries. In particular, the static Parser::parse functions should be used...
Definition PE/Parser.hpp:53
Definition Visitor.hpp:212
Iterator which returns reference on container's values.
Definition iterators.hpp:47
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition DataDirectory.hpp:37
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
@ E
Definition AcceleratorCodes.hpp:82
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:316
#define LIEF_API
Definition visibility.h:45