LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
ELF/Builder.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2021, NVIDIA CORPORATION. All rights reserved
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_ELF_BUIDLER_H
17#define LIEF_ELF_BUIDLER_H
18
19#include <memory>
20#include <set>
21#include <string>
22#include <vector>
23
24#include "LIEF/errors.hpp"
25
26#include "LIEF/iostream.hpp"
27#include "LIEF/visibility.h"
28
29
30namespace LIEF::ELF {
31class Binary;
32class Layout;
33class Header;
34class Note;
36class DynamicEntry;
37class Section;
38class ExeLayout;
39class ObjectFileLayout;
40class Layout;
41class Relocation;
42
48 friend class ObjectFileLayout;
49 friend class Layout;
50 friend class ExeLayout;
51
52 public:
54 struct config_t {
56 bool dt_hash = true;
57
59 bool dyn_str = true;
60
62 bool dynamic_section = true;
63
65 bool fini_array = true;
66
68 bool gnu_hash = true;
69
71 bool init_array = true;
72
74 bool interpreter = true;
75
77 bool jmprel = true;
78
80 bool notes = false;
81
83 bool preinit_array = true;
84
86 bool relr = true;
87
89 bool android_rela = true;
90
92 bool rela = true;
93
95 bool static_symtab = true;
96
98 bool sym_verdef = true;
99
101 bool sym_verneed = true;
102
104 bool sym_versym = true;
105
107 bool symtab = true;
108
110 bool coredump_notes = true;
111
114 bool force_relocate = false;
115
118 bool skip_dynamic = false;
119
123 };
124
125 Builder(Binary& binary, const config_t& config);
126 Builder(Binary& binary) :
127 Builder(binary, config_t()) {}
128
129 Builder() = delete;
131
134
136 return config_;
137 }
138
140 const std::vector<uint8_t>& get_build() LIEF_LIFETIMEBOUND;
141
143 void write(const std::string& filename) const;
144
146 void write(std::ostream& os) const;
147
148 protected:
149 template<typename ELF_T>
151
152 template<typename ELF_T>
153 LIEF_LOCAL ok_error_t build_relocatable();
154
155 template<typename ELF_T>
156 LIEF_LOCAL ok_error_t build_exe_lib();
157
158 template<typename ELF_T>
159 LIEF_LOCAL ok_error_t build(const Header& header);
160
161 template<typename ELF_T>
162 LIEF_LOCAL ok_error_t build_sections();
163
164 template<typename ELF_T>
165 LIEF_LOCAL ok_error_t build_segments();
166
167 template<typename ELF_T>
168 LIEF_LOCAL ok_error_t build_symtab_symbols();
169
170 template<typename ELF_T>
171 LIEF_LOCAL ok_error_t build_dynamic();
172
173 template<typename ELF_T>
174 LIEF_LOCAL ok_error_t build_dynamic_section();
175
176 template<typename ELF_T>
177 LIEF_LOCAL ok_error_t build_dynamic_symbols();
178
179 template<typename ELF_T>
180 LIEF_LOCAL ok_error_t build_obj_symbols();
181
182 template<typename ELF_T>
183 LIEF_LOCAL ok_error_t build_dynamic_relocations();
184
185 template<typename ELF_T>
186 LIEF_LOCAL ok_error_t build_relative_relocations();
187
188 template<typename ELF_T>
189 LIEF_LOCAL ok_error_t build_android_relocations();
190
191 template<typename ELF_T>
192 LIEF_LOCAL ok_error_t build_pltgot_relocations();
193
194 template<typename ELF_T>
195 LIEF_LOCAL ok_error_t build_section_relocations();
196
197 LIEF_LOCAL uint32_t sort_dynamic_symbols();
198
199 template<typename ELF_T>
200 LIEF_LOCAL ok_error_t build_hash_table();
201
202 template<typename ELF_T>
203 LIEF_LOCAL ok_error_t build_symbol_hash();
204
205 LIEF_LOCAL ok_error_t build_empty_symbol_gnuhash();
206
207 template<typename ELF_T>
208 LIEF_LOCAL ok_error_t build_symbol_requirement();
209
210 template<typename ELF_T>
211 LIEF_LOCAL ok_error_t build_symbol_definition();
212
213 template<typename ELF_T>
214 LIEF_LOCAL ok_error_t build_symbol_version();
215
216 template<typename ELF_T>
217 LIEF_LOCAL ok_error_t build_interpreter();
218
219 template<typename ELF_T>
220 LIEF_LOCAL ok_error_t build_notes();
221
222 LIEF_LOCAL ok_error_t update_note_section(const Note& note,
223 std::set<const Note*>& notes);
224
225 template<typename ELF_T>
226 LIEF_LOCAL ok_error_t build_overlay();
227
228 LIEF_LOCAL bool should_swap() const;
229
230 template<class ELF_T>
231 LIEF_LOCAL ok_error_t process_object_relocations();
232
233 LIEF_LOCAL bool should_build_notes() const;
234
235 config_t config_;
236 mutable vector_iostream ios_;
237 Binary* binary_ = nullptr;
238 std::unique_ptr<Layout> layout_;
239};
240
241}
242
243
244#endif
Class which represents an ELF binary.
Definition ELF/Binary.hpp:64
friend class ObjectFileLayout
Definition ELF/Builder.hpp:48
friend class ExeLayout
Definition ELF/Builder.hpp:50
Builder(Binary &binary, const config_t &config)
void write(const std::string &filename) const
Write the built ELF binary in the filename given in parameter.
friend class Layout
Definition ELF/Builder.hpp:49
config_t & config()
Definition ELF/Builder.hpp:135
Builder(Binary &binary)
Definition ELF/Builder.hpp:126
ok_error_t build()
Perform the build of the provided ELF binary.
const std::vector< uint8_t > & get_build()
Return the built ELF binary as a byte vector.
Class that represent an Array in the dynamic table. This entry is associated with constructors:
Definition DynamicEntryArray.hpp:35
Class which represents an entry in the dynamic table These entries are located in the ....
Definition DynamicEntry.hpp:36
Class which represents the ELF's header. This class mirrors the raw ELF Elfxx_Ehdr structure.
Definition ELF/Header.hpp:35
Class which represents an ELF note. This class can be instantiated using the static Note::create func...
Definition Note.hpp:40
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:42
Class which represents an ELF Section.
Definition ELF/Section.hpp:49
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:118
Definition iostream.hpp:33
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:27
Configuration options to tweak the building process.
Definition ELF/Builder.hpp:54
bool keep_empty_version_requirement
Remove entries in .gnu.version_r if they are not associated with at least one version.
Definition ELF/Builder.hpp:122
bool force_relocate
Force to relocating all the ELF structures that are supported by LIEF (mostly for testing).
Definition ELF/Builder.hpp:114
bool dynamic_section
Rebuild PT_DYNAMIC segment.
Definition ELF/Builder.hpp:62
bool dyn_str
Rebuild DT_STRTAB.
Definition ELF/Builder.hpp:59
bool static_symtab
Rebuild .symtab.
Definition ELF/Builder.hpp:95
bool relr
Rebuild DT_RELR.
Definition ELF/Builder.hpp:86
bool init_array
Rebuild DT_INIT_ARRAY.
Definition ELF/Builder.hpp:71
bool symtab
Rebuild DT_SYMTAB.
Definition ELF/Builder.hpp:107
bool android_rela
Rebuild DT_ANDROID_REL[A].
Definition ELF/Builder.hpp:89
bool sym_versym
Rebuild DT_VERSYM.
Definition ELF/Builder.hpp:104
bool gnu_hash
Rebuild DT_GNU_HASH.
Definition ELF/Builder.hpp:68
bool rela
Rebuild DT_REL[A].
Definition ELF/Builder.hpp:92
bool preinit_array
Rebuild DT_PREINIT_ARRAY.
Definition ELF/Builder.hpp:83
bool sym_verneed
Rebuild DT_VERNEED.
Definition ELF/Builder.hpp:101
bool coredump_notes
Rebuild the Coredump notes.
Definition ELF/Builder.hpp:110
bool fini_array
Rebuild DT_FINI_ARRAY.
Definition ELF/Builder.hpp:65
bool dt_hash
Rebuild DT_HASH.
Definition ELF/Builder.hpp:56
bool interpreter
Rebuild PT_INTERPRETER.
Definition ELF/Builder.hpp:74
bool skip_dynamic
Skip relocating the PT_DYNAMIC segment (only relevant if force_relocate is set.
Definition ELF/Builder.hpp:118
bool notes
Disable note building since it can break the default layout.
Definition ELF/Builder.hpp:80
bool jmprel
Rebuild DT_JMPREL.
Definition ELF/Builder.hpp:77
bool sym_verdef
Rebuild DT_VERDEF.
Definition ELF/Builder.hpp:98
#define LIEF_API
Definition visibility.h:44
#define LIEF_LOCAL
Definition visibility.h:45