LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
Import.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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_IMPORT_H
17#define LIEF_PE_IMPORT_H
18
19#include <string>
20#include <ostream>
21
22#include "LIEF/errors.hpp"
23#include "LIEF/Object.hpp"
24#include "LIEF/visibility.h"
25#include "LIEF/iterators.hpp"
27
28namespace LIEF {
29namespace PE {
30class Parser;
31class Builder;
32class DataDirectory;
33
34namespace details {
35struct pe_import;
36}
37class LIEF_API Import : public Object {
40
41 friend class Parser;
42 friend class Builder;
43
44 public:
45 using entries_t = std::vector<ImportEntry>;
46 using it_entries = ref_iterator<entries_t&>;
47 using it_const_entries = const_ref_iterator<const entries_t&>;
48
49 Import(const details::pe_import& import);
50 Import(std::string name) :
51 name_(std::move(name))
52 {}
53 Import() = default;
54 ~Import() override = default;
55
56 Import(const Import& other) = default;
57 Import(Import&& other) noexcept = default;
58 Import& operator=(Import&& other) noexcept = default;
59 Import& operator=(const Import& other) = default;
60 uint32_t forwarder_chain() const {
63 return forwarder_chain_;
64 }
65 uint32_t timedatestamp() const {
69 return timedatestamp_;
70 }
71 it_const_entries entries() const {
74 return entries_;
75 }
76
77 it_entries entries() {
78 return entries_;
79 }
80 uint32_t import_address_table_rva() const {
87 return iat_rva_;
88 }
89 uint32_t import_lookup_table_rva() const {
94 return ilt_rva_;
95 }
96 result<uint32_t> get_function_rva_from_iat(const std::string& function) const;
101 ImportEntry* get_entry(const std::string& name) {
104 return const_cast<ImportEntry*>(static_cast<const Import*>(this)->get_entry(name));
105 }
106 const ImportEntry* get_entry(const std::string& name) const;
107 const std::string& name() const {
110 return name_;
111 }
112 void name(std::string name) {
115 name_ = std::move(name);
116 }
117 uint32_t name_rva() const {
120 return name_rva_;
121 }
122 DataDirectory* directory() {
128 return directory_;
129 }
130
131 const DataDirectory* directory() const {
132 return directory_;
133 }
134 DataDirectory* iat_directory() {
140 return iat_directory_;
141 }
142
143 const DataDirectory* iat_directory() const {
144 return iat_directory_;
145 }
146 ImportEntry& add_entry(ImportEntry entry) {
149 entries_.push_back(std::move(entry));
150 return entries_.back();
151 }
152 ImportEntry& add_entry(const std::string& name) {
155 entries_.emplace_back(name);
156 return entries_.back();
157 }
158 bool remove_entry(const std::string& name);
163 bool remove_entry(uint32_t ordinal);
168
169 void import_lookup_table_rva(uint32_t rva) {
170 ilt_rva_ = rva;
171 }
172
173 void import_address_table_rva(uint32_t rva) {
174 iat_rva_ = rva;
175 }
176 LIEF_LOCAL size_t nb_original_func() const {
179 return nb_original_func_;
180 }
181
182 void accept(Visitor& visitor) const override;
183
184 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Import& entry);
185
186 private:
187 entries_t entries_;
188 DataDirectory* directory_ = nullptr;
189 DataDirectory* iat_directory_ = nullptr;
190 uint32_t ilt_rva_ = 0;
191 uint32_t timedatestamp_ = 0;
192 uint32_t forwarder_chain_ = 0;
193 uint32_t name_rva_ = 0;
194 uint32_t iat_rva_ = 0;
195 std::string name_;
196 PE_TYPE type_ = PE_TYPE::PE32;
197 size_t nb_original_func_ = 0;
198};
199
200}
201}
202
203#endif
ImportEntry.hpp
Object.hpp
LIEF::PE::Builder
Class that is used to rebuild a raw PE binary from a PE::Binary object.
Definition PE/Builder.hpp:45
LIEF::PE::DataDirectory
Class that represents a PE data directory entry.
Definition DataDirectory.hpp:42
LIEF::PE::Import
Class that represents a PE import.
Definition Import.hpp:39
LIEF::PE::Import::~Import
~Import() override=default
LIEF::PE::Import::entries
it_entries entries()
Definition Import.hpp:77
LIEF::PE::Import::name_rva
uint32_t name_rva() const
The original name rva.
Definition Import.hpp:119
LIEF::PE::Import::directory
DataDirectory * directory()
Return the PE::DataDirectory associated with this import. It should be the one at index PE::DataDirec...
Definition Import.hpp:127
LIEF::PE::Import::entries
it_const_entries entries() const
Iterator over the PE::ImportEntry.
Definition Import.hpp:73
LIEF::PE::Import::remove_entry
bool remove_entry(const std::string &name)
Remove the import entry with the given name.
LIEF::PE::Import::get_entry
const ImportEntry * get_entry(const std::string &name) const
LIEF::PE::Import::add_entry
ImportEntry & add_entry(const std::string &name)
Add a new import entry with the given name (i.e. an imported function)
Definition Import.hpp:154
LIEF::PE::Import::import_lookup_table_rva
void import_lookup_table_rva(uint32_t rva)
Definition Import.hpp:169
LIEF::PE::Import::directory
const DataDirectory * directory() const
Definition Import.hpp:131
LIEF::PE::Import::forwarder_chain
uint32_t forwarder_chain() const
The index of the first forwarder reference.
Definition Import.hpp:62
LIEF::PE::Import::Import
Import(std::string name)
Definition Import.hpp:50
LIEF::PE::Import::get_entry
ImportEntry * get_entry(const std::string &name)
Return the imported function with the given name.
Definition Import.hpp:103
LIEF::PE::Import::remove_entry
bool remove_entry(uint32_t ordinal)
Remove the import entry with the given ordinal number.
LIEF::PE::Import::import_address_table_rva
void import_address_table_rva(uint32_t rva)
Definition Import.hpp:173
LIEF::PE::Import::operator=
Import & operator=(Import &&other) noexcept=default
LIEF::PE::Import::Import
Import(const details::pe_import &import)
LIEF::PE::Import::Import
Import(const Import &other)=default
LIEF::PE::Import::import_lookup_table_rva
uint32_t import_lookup_table_rva() const
Return the relative virtual address of the import lookup table.
Definition Import.hpp:93
LIEF::PE::Import::import_address_table_rva
uint32_t import_address_table_rva() const
The RVA of the import address table (IAT). The content of this table is identical to the content of t...
Definition Import.hpp:86
LIEF::PE::Import::Import
Import(Import &&other) noexcept=default
LIEF::PE::Import::operator=
Import & operator=(const Import &other)=default
LIEF::PE::Import::timedatestamp
uint32_t timedatestamp() const
The stamp that is set to zero until the image is bound. After the image is bound, this field is set t...
Definition Import.hpp:68
LIEF::PE::Import::operator<<
friend std::ostream & operator<<(std::ostream &os, const Import &entry)
LIEF::PE::Import::accept
void accept(Visitor &visitor) const override
LIEF::PE::Import::get_function_rva_from_iat
result< uint32_t > get_function_rva_from_iat(const std::string &function) const
Return the Function's RVA from the import address table (IAT)
LIEF::PE::Import::iat_directory
const DataDirectory * iat_directory() const
Definition Import.hpp:143
LIEF::PE::Import::name
const std::string & name() const
Return the library's name (e.g. kernel32.dll)
Definition Import.hpp:109
LIEF::PE::Import::Import
Import()=default
LIEF::PE::Import::add_entry
ImportEntry & add_entry(ImportEntry entry)
Add a new import entry (i.e. an imported function)
Definition Import.hpp:148
LIEF::PE::Import::iat_directory
DataDirectory * iat_directory()
Return the PE::DataDirectory associated associated with the IAT. It should be the one at index PE::Da...
Definition Import.hpp:139
LIEF::PE::Import::name
void name(std::string name)
Change the current import name.
Definition Import.hpp:114
LIEF::PE::Parser
Main interface to parse PE binaries. In particular the static functions: Parser::parse should be used...
Definition PE/Parser.hpp:52
errors.hpp
iterators.hpp
LIEF::PE::details
Definition DataDirectory.hpp:37
LIEF::PE
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
LIEF::PE::PE_TYPE
PE_TYPE
Definition PE/enums.hpp:22
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42