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#include <memory>
22
23#include "LIEF/errors.hpp"
24#include "LIEF/Object.hpp"
25#include "LIEF/visibility.h"
26#include "LIEF/iterators.hpp"
28
29namespace LIEF {
30namespace PE {
31class Parser;
32class Builder;
33class DataDirectory;
34
35namespace details {
36struct pe_import;
37}
38class LIEF_API Import : public Object {
41
42 friend class Parser;
43 friend class Builder;
44
45 public:
46 using entries_t = std::vector<std::unique_ptr<ImportEntry>>;
47 using it_entries = ref_iterator<entries_t&, ImportEntry*>;
48 using it_const_entries = const_ref_iterator<const entries_t&, const ImportEntry*>;
49
50 Import(const details::pe_import& import);
51 Import(std::string name) :
52 name_(std::move(name))
53 {}
54 Import() = default;
55 ~Import() override = default;
56
57 Import(const Import& other);
58 Import& operator=(const Import& other);
59
60 Import(Import&& other) noexcept = default;
61 Import& operator=(Import&& other) noexcept = default;
62 uint32_t forwarder_chain() const {
65 return forwarder_chain_;
66 }
67 uint32_t timedatestamp() const {
71 return timedatestamp_;
72 }
73 it_const_entries entries() const {
76 return entries_;
77 }
78
79 it_entries entries() {
80 return entries_;
81 }
82 uint32_t import_address_table_rva() const {
89 return iat_rva_;
90 }
91 uint32_t import_lookup_table_rva() const {
96 return ilt_rva_;
97 }
98 result<uint32_t> get_function_rva_from_iat(const std::string& function) const;
103 ImportEntry* get_entry(const std::string& name) {
106 return const_cast<ImportEntry*>(static_cast<const Import*>(this)->get_entry(name));
107 }
108 const ImportEntry* get_entry(const std::string& name) const;
109 const std::string& name() const {
112 return name_;
113 }
114 void name(std::string name) {
117 name_ = std::move(name);
118 }
119 uint32_t name_rva() const {
122 return name_rva_;
123 }
124 DataDirectory* directory() {
130 return directory_;
131 }
132
133 const DataDirectory* directory() const {
134 return directory_;
135 }
136 DataDirectory* iat_directory() {
142 return iat_directory_;
143 }
144
145 const DataDirectory* iat_directory() const {
146 return iat_directory_;
147 }
148 ImportEntry& add_entry(ImportEntry entry) {
151 entries_.emplace_back(new ImportEntry(std::move(entry)));
152 return *entries_.back();
153 }
154 ImportEntry& add_entry(const std::string& name) {
157 entries_.emplace_back(new ImportEntry(name));
158 return *entries_.back();
159 }
160 bool remove_entry(const std::string& name);
165 bool remove_entry(uint32_t ordinal);
170
171 void import_lookup_table_rva(uint32_t rva) {
172 ilt_rva_ = rva;
173 }
174
175 void import_address_table_rva(uint32_t rva) {
176 iat_rva_ = rva;
177 }
178 LIEF_LOCAL size_t nb_original_func() const {
181 return nb_original_func_;
182 }
183
184 void accept(Visitor& visitor) const override;
185
186 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Import& entry);
187
188 private:
189 entries_t entries_;
190 DataDirectory* directory_ = nullptr;
191 DataDirectory* iat_directory_ = nullptr;
192 uint32_t ilt_rva_ = 0;
193 uint32_t timedatestamp_ = 0;
194 uint32_t forwarder_chain_ = 0;
195 uint32_t name_rva_ = 0;
196 uint32_t iat_rva_ = 0;
197 std::string name_;
198 PE_TYPE type_ = PE_TYPE::PE32;
199 size_t nb_original_func_ = 0;
200};
201
202}
203}
204
205#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:40
LIEF::PE::Import::~Import
~Import() override=default
LIEF::PE::Import::entries
it_entries entries()
Definition Import.hpp:79
LIEF::PE::Import::name_rva
uint32_t name_rva() const
The original name rva.
Definition Import.hpp:121
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:129
LIEF::PE::Import::Import
Import(const Import &other)
LIEF::PE::Import::entries
it_const_entries entries() const
Iterator over the PE::ImportEntry.
Definition Import.hpp:75
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:156
LIEF::PE::Import::import_lookup_table_rva
void import_lookup_table_rva(uint32_t rva)
Definition Import.hpp:171
LIEF::PE::Import::directory
const DataDirectory * directory() const
Definition Import.hpp:133
LIEF::PE::Import::forwarder_chain
uint32_t forwarder_chain() const
The index of the first forwarder reference.
Definition Import.hpp:64
LIEF::PE::Import::Import
Import(std::string name)
Definition Import.hpp:51
LIEF::PE::Import::get_entry
ImportEntry * get_entry(const std::string &name)
Return the imported function with the given name.
Definition Import.hpp:105
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:175
LIEF::PE::Import::operator=
Import & operator=(Import &&other) noexcept=default
LIEF::PE::Import::Import
Import(const details::pe_import &import)
LIEF::PE::Import::operator=
Import & operator=(const Import &other)
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:95
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:88
LIEF::PE::Import::Import
Import(Import &&other) noexcept=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:70
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:145
LIEF::PE::Import::name
const std::string & name() const
Return the library's name (e.g. kernel32.dll)
Definition Import.hpp:111
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:150
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:141
LIEF::PE::Import::name
void name(std::string name)
Change the current import name.
Definition Import.hpp:116
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:39
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42