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