LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
ELF/Builder.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 Quarkslab
3 * Copyright 2017 - 2021, NVIDIA CORPORATION. All rights reserved
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#ifndef LIEF_ELF_BUIDLER_H
18#define LIEF_ELF_BUIDLER_H
19
20#include <memory>
21#include <set>
22#include <string>
23#include <vector>
24
25#include "LIEF/errors.hpp"
26
27#include "LIEF/visibility.h"
28#include "LIEF/iostream.hpp"
29
30namespace LIEF {
31namespace ELF {
32class Binary;
33class Layout;
34class Header;
35class Note;
37class DynamicEntry;
38class Section;
39class ExeLayout;
40class ObjectFileLayout;
41class Layout;
42class Relocation;
43class LIEF_API Builder {
49 friend class ObjectFileLayout;
50 friend class Layout;
51 friend class ExeLayout;
52
53 public: struct config_t {
56 bool dt_hash = true;
57 bool dyn_str = true;
58 bool dynamic_section = true;
59 bool fini_array = true;
60 bool gnu_hash = true;
61 bool init_array = true;
62 bool interpreter = true;
63 bool jmprel = true;
64 bool notes = false;
65 bool preinit_array = true;
66 bool relr = true;
67 bool android_rela = true;
68 bool rela = true;
69 bool static_symtab = true;
70 bool sym_verdef = true;
71 bool sym_verneed = true;
72 bool sym_versym = true;
73 bool symtab = true;
74 bool coredump_notes = true;
75 bool force_relocate = false;
76 };
77
78 Builder(Binary& binary);
79
80 Builder() = delete;
82 void build();
85 Builder& set_config(config_t conf) {
88 config_ = conf;
89 return *this;
90 }
91
92 config_t& config() {
93 return config_;
94 }
95 const std::vector<uint8_t>& get_build();
98 void write(const std::string& filename) const;
101 void write(std::ostream& os) const;
104
105 protected:
106 template<typename ELF_T>
107 LIEF_LOCAL ok_error_t build();
108
109 template<typename ELF_T>
110 LIEF_LOCAL ok_error_t build_relocatable();
111
112 template<typename ELF_T>
113 LIEF_LOCAL ok_error_t build_exe_lib();
114
115 template<typename ELF_T>
116 LIEF_LOCAL ok_error_t build(const Header& header);
117
118 template<typename ELF_T>
119 LIEF_LOCAL ok_error_t build_sections();
120
121 template<typename ELF_T>
122 LIEF_LOCAL ok_error_t build_segments();
123
124 template<typename ELF_T>
125 LIEF_LOCAL ok_error_t build_symtab_symbols();
126
127 template<typename ELF_T>
128 LIEF_LOCAL ok_error_t build_dynamic();
129
130 template<typename ELF_T>
131 LIEF_LOCAL ok_error_t build_dynamic_section();
132
133 template<typename ELF_T>
134 LIEF_LOCAL ok_error_t build_dynamic_symbols();
135
136 template<typename ELF_T>
137 LIEF_LOCAL ok_error_t build_obj_symbols();
138
139 template<typename ELF_T>
140 LIEF_LOCAL ok_error_t build_dynamic_relocations();
141
142 template<typename ELF_T>
143 LIEF_LOCAL ok_error_t build_relative_relocations();
144
145 template<typename ELF_T>
146 LIEF_LOCAL ok_error_t build_android_relocations();
147
148 template<typename ELF_T>
149 LIEF_LOCAL ok_error_t build_pltgot_relocations();
150
151 template<typename ELF_T>
152 LIEF_LOCAL ok_error_t build_section_relocations();
153
154 LIEF_LOCAL uint32_t sort_dynamic_symbols();
155
156 template<typename ELF_T>
157 LIEF_LOCAL ok_error_t build_hash_table();
158
159 template<typename ELF_T>
160 LIEF_LOCAL ok_error_t build_symbol_hash();
161
162 LIEF_LOCAL ok_error_t build_empty_symbol_gnuhash();
163
164 template<typename ELF_T>
165 LIEF_LOCAL ok_error_t build_symbol_requirement();
166
167 template<typename ELF_T>
168 LIEF_LOCAL ok_error_t build_symbol_definition();
169
170 template<typename ELF_T>
171 LIEF_LOCAL ok_error_t build_symbol_version();
172
173 template<typename ELF_T>
174 LIEF_LOCAL ok_error_t build_interpreter();
175
176 template<typename ELF_T>
177 LIEF_LOCAL ok_error_t build_notes();
178
179 LIEF_LOCAL ok_error_t update_note_section(const Note& note, std::set<const Note*>& notes);
180
181 template<typename ELF_T>
182 LIEF_LOCAL ok_error_t build_overlay();
183
184 LIEF_LOCAL bool should_swap() const;
185
186 template<class ELF_T>
187 LIEF_LOCAL ok_error_t process_object_relocations();
188
189 LIEF_LOCAL bool should_build_notes() const;
190
191 config_t config_;
192 mutable vector_iostream ios_;
193 Binary* binary_{nullptr};
194 std::unique_ptr<Layout> layout_;
195};
196
197} // namespace ELF
198} // namespace LIEF
199
200
201
202
203#endif
LIEF::ELF::Binary
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
LIEF::ELF::Builder
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:48
LIEF::ELF::Builder::~Builder
~Builder()
LIEF::ELF::Builder::build
void build()
Perform the build of the provided ELF binary.
LIEF::ELF::Builder::Builder
Builder()=delete
LIEF::ELF::Builder::write
void write(const std::string &filename) const
Write the built ELF binary in the filename given in parameter.
LIEF::ELF::Builder::set_config
Builder & set_config(config_t conf)
Tweak the ELF builder with the provided config parameter.
Definition ELF/Builder.hpp:87
LIEF::ELF::Builder::write
void write(std::ostream &os) const
Write the built ELF binary in the stream os given in parameter.
LIEF::ELF::Builder::config
config_t & config()
Definition ELF/Builder.hpp:92
LIEF::ELF::Builder::Builder
Builder(Binary &binary)
LIEF::ELF::Builder::get_build
const std::vector< uint8_t > & get_build()
Return the built ELF binary as a byte vector.
LIEF::ELF::DynamicEntryArray
Class that represent an Array in the dynamic table. This entry is associated with constructors:
Definition DynamicEntryArray.hpp:35
LIEF::ELF::DynamicEntry
Class which represents an entry in the dynamic table These entries are located in the ....
Definition DynamicEntry.hpp:36
LIEF::ELF::Header
Class which represents the ELF's header. This class mirrors the raw ELF Elfxx_Ehdr structure.
Definition ELF/Header.hpp:35
LIEF::ELF::Note
Class which represents an ELF note. This class can be instantiated using the static Note::create func...
Definition Note.hpp:39
LIEF::ELF::Relocation
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:40
LIEF::ELF::Section
Class wich represents an ELF Section.
Definition ELF/Section.hpp:48
errors.hpp
iostream.hpp
LIEF::ELF
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
LIEF::ok_error_t
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:109
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42