LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
Factory.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_FACTORY_H
17#define LIEF_PE_FACTORY_H
18
19#include "LIEF/visibility.h"
20#include "LIEF/PE/Binary.hpp"
22#include "LIEF/PE/Section.hpp"
23#include "LIEF/PE/Import.hpp"
24#include <memory>
25
26namespace LIEF {
27namespace PE {
28class LIEF_API Factory {
31 public:
32 Factory(const Factory&) = delete;
33 Factory& operator=(const Factory&) = delete;
34
35 Factory(Factory&&);
36 Factory& operator=(Factory&&);
37 static std::unique_ptr<Factory> create(PE_TYPE type);
40
41 Factory& add_section(const Section& section) {
42 sections_.push_back(std::unique_ptr<Section>(new Section(section)));
43 return *this;
44 }
45
46 Factory& set_arch(Header::MACHINE_TYPES arch) {
47 pe_->header().machine(arch);
48 return *this;
49 }
50
51 Factory& set_entrypoint(uint64_t ep) {
52 pe_->optional_header().addressof_entrypoint(ep);
53 return *this;
54 }
55
56 std::unique_ptr<Binary> get() {
57 return process();
58 }
59
60 bool is_32bit() const {
61 return pe_->type() == PE_TYPE::PE32;
62 }
63
64 bool is_64bit() const {
65 return pe_->type() == PE_TYPE::PE32_PLUS;
66 }
67
68 uint32_t section_align() const {
69 return pe_->optional_header().section_alignment();
70 }
71
72 uint32_t file_align() const {
73 return pe_->optional_header().file_alignment();
74 }
75
77
78 protected:
79 Factory() :
80 pe_(std::unique_ptr<Binary>(new Binary{}))
81 {}
82 std::unique_ptr<Binary> process();
83
84 ok_error_t check_overlapping() const;
85 ok_error_t assign_locations();
86 ok_error_t update_headers();
87 ok_error_t move_sections();
88 uint32_t sizeof_headers() const;
89
90 std::vector<std::unique_ptr<Section>> sections_;
91 std::vector<Import> imports_;
92 std::unique_ptr<Binary> pe_;
93};
94}
95}
96#endif
97
This factory is used to create PE from scratch.
Definition Factory.hpp:30
Factory & set_arch(Header::MACHINE_TYPES arch)
Definition Factory.hpp:46
Factory & set_entrypoint(uint64_t ep)
Definition Factory.hpp:51
std::unique_ptr< Binary > get()
Definition Factory.hpp:56
bool is_64bit() const
Definition Factory.hpp:64
uint32_t file_align() const
Definition Factory.hpp:72
uint32_t section_align() const
Definition Factory.hpp:68
bool is_32bit() const
Definition Factory.hpp:60
Factory & add_section(const Section &section)
Definition Factory.hpp:41
Factory & operator=(const Factory &)=delete
Factory(const Factory &)=delete
Factory & operator=(Factory &&)
static std::unique_ptr< Factory > create(PE_TYPE type)
Initiate the factory to construct a PE which the given type.
Factory(Factory &&)
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
LIEF namespace.
Definition Abstract/Binary.hpp:36
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:109
#define LIEF_API
Definition visibility.h:41