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"
21#include "LIEF/PE/Section.hpp"
22#include "LIEF/PE/Import.hpp"
23#include <memory>
24
25namespace LIEF {
26namespace PE {
27class LIEF_API Factory {
30 public:
31 Factory(const Factory&) = delete;
32 Factory& operator=(const Factory&) = delete;
33
34 Factory(Factory&&);
35 Factory& operator=(Factory&&);
36 static std::unique_ptr<Factory> create(PE_TYPE type);
39
40 Factory& add_section(const Section& section) {
41 sections_.push_back(std::unique_ptr<Section>(new Section(section)));
42 return *this;
43 }
44
45 Factory& set_arch(Header::MACHINE_TYPES arch) {
46 pe_->header().machine(arch);
47 return *this;
48 }
49
50 Factory& set_entrypoint(uint64_t ep) {
51 pe_->optional_header().addressof_entrypoint(ep);
52 return *this;
53 }
54
55 std::unique_ptr<Binary> get() {
56 return process();
57 }
58
59 bool is_32bit() const {
60 return pe_->type() == PE_TYPE::PE32;
61 }
62
63 bool is_64bit() const {
64 return pe_->type() == PE_TYPE::PE32_PLUS;
65 }
66
67 uint32_t section_align() const {
68 return pe_->optional_header().section_alignment();
69 }
70
71 uint32_t file_align() const {
72 return pe_->optional_header().file_alignment();
73 }
74
76
77 protected:
78 Factory() :
79 pe_(std::unique_ptr<Binary>(new Binary{}))
80 {}
81 std::unique_ptr<Binary> process();
82
83 ok_error_t check_overlapping() const;
84 ok_error_t assign_locations();
85 ok_error_t update_headers();
86 ok_error_t move_sections();
87 uint32_t sizeof_headers() const;
88
89 std::vector<std::unique_ptr<Section>> sections_;
90 std::vector<Import> imports_;
91 std::unique_ptr<Binary> pe_;
92};
93}
94}
95#endif
96
This factory is used to create PE from scratch.
Definition Factory.hpp:29
Factory & set_arch(Header::MACHINE_TYPES arch)
Definition Factory.hpp:45
Factory & set_entrypoint(uint64_t ep)
Definition Factory.hpp:50
std::unique_ptr< Binary > get()
Definition Factory.hpp:55
bool is_64bit() const
Definition Factory.hpp:63
uint32_t file_align() const
Definition Factory.hpp:71
uint32_t section_align() const
Definition Factory.hpp:67
bool is_32bit() const
Definition Factory.hpp:59
Factory & add_section(const Section &section)
Definition Factory.hpp:40
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:39
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