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