LIEF: Library to Instrument Executable Formats Version 1.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 - 2026 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;
43
49 friend class ObjectFileLayout;
50 friend class Layout;
51 friend class ExeLayout;
52
53 public:
55 struct config_t {
57 bool dt_hash = true;
58
60 bool dyn_str = true;
61
63 bool dynamic_section = true;
64
66 bool fini_array = true;
67
69 bool gnu_hash = true;
70
72 bool init_array = true;
73
75 bool interpreter = true;
76
78 bool jmprel = true;
79
81 bool notes = false;
82
84 bool preinit_array = true;
85
87 bool relr = true;
88
90 bool android_rela = true;
91
93 bool rela = true;
94
96 bool static_symtab = true;
97
99 bool sym_verdef = true;
100
102 bool sym_verneed = true;
103
105 bool sym_versym = true;
106
108 bool symtab = true;
109
111 bool coredump_notes = true;
112
115 bool force_relocate = false;
116
119 bool skip_dynamic = false;
120
124 };
125
126 Builder(Binary& binary, const config_t& config);
127 Builder(Binary& binary) :
128 Builder(binary, config_t()) {}
129
130 Builder() = delete;
132
134 void build();
135
137 return config_;
138 }
139
141 const std::vector<uint8_t>& get_build() LIEF_LIFETIMEBOUND;
142
144 void write(const std::string& filename) const;
145
147 void write(std::ostream& os) const;
148
149 protected:
150 template<typename ELF_T>
152
153 template<typename ELF_T>
154 LIEF_LOCAL ok_error_t build_relocatable();
155
156 template<typename ELF_T>
157 LIEF_LOCAL ok_error_t build_exe_lib();
158
159 template<typename ELF_T>
160 LIEF_LOCAL ok_error_t build(const Header& header);
161
162 template<typename ELF_T>
163 LIEF_LOCAL ok_error_t build_sections();
164
165 template<typename ELF_T>
166 LIEF_LOCAL ok_error_t build_segments();
167
168 template<typename ELF_T>
169 LIEF_LOCAL ok_error_t build_symtab_symbols();
170
171 template<typename ELF_T>
172 LIEF_LOCAL ok_error_t build_dynamic();
173
174 template<typename ELF_T>
175 LIEF_LOCAL ok_error_t build_dynamic_section();
176
177 template<typename ELF_T>
178 LIEF_LOCAL ok_error_t build_dynamic_symbols();
179
180 template<typename ELF_T>
181 LIEF_LOCAL ok_error_t build_obj_symbols();
182
183 template<typename ELF_T>
184 LIEF_LOCAL ok_error_t build_dynamic_relocations();
185
186 template<typename ELF_T>
187 LIEF_LOCAL ok_error_t build_relative_relocations();
188
189 template<typename ELF_T>
190 LIEF_LOCAL ok_error_t build_android_relocations();
191
192 template<typename ELF_T>
193 LIEF_LOCAL ok_error_t build_pltgot_relocations();
194
195 template<typename ELF_T>
196 LIEF_LOCAL ok_error_t build_section_relocations();
197
198 LIEF_LOCAL uint32_t sort_dynamic_symbols();
199
200 template<typename ELF_T>
201 LIEF_LOCAL ok_error_t build_hash_table();
202
203 template<typename ELF_T>
204 LIEF_LOCAL ok_error_t build_symbol_hash();
205
206 LIEF_LOCAL ok_error_t build_empty_symbol_gnuhash();
207
208 template<typename ELF_T>
209 LIEF_LOCAL ok_error_t build_symbol_requirement();
210
211 template<typename ELF_T>
212 LIEF_LOCAL ok_error_t build_symbol_definition();
213
214 template<typename ELF_T>
215 LIEF_LOCAL ok_error_t build_symbol_version();
216
217 template<typename ELF_T>
218 LIEF_LOCAL ok_error_t build_interpreter();
219
220 template<typename ELF_T>
221 LIEF_LOCAL ok_error_t build_notes();
222
223 LIEF_LOCAL ok_error_t update_note_section(const Note& note,
224 std::set<const Note*>& notes);
225
226 template<typename ELF_T>
227 LIEF_LOCAL ok_error_t build_overlay();
228
229 LIEF_LOCAL bool should_swap() const;
230
231 template<class ELF_T>
232 LIEF_LOCAL ok_error_t process_object_relocations();
233
234 LIEF_LOCAL bool should_build_notes() const;
235
236 config_t config_;
237 mutable vector_iostream ios_;
238 Binary* binary_ = nullptr;
239 std::unique_ptr<Layout> layout_;
240};
241
242} // namespace ELF
243} // namespace LIEF
244
245
246#endif
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
friend class ObjectFileLayout
Definition ELF/Builder.hpp:49
friend class ExeLayout
Definition ELF/Builder.hpp:51
void build()
Perform the build of the provided ELF binary.
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:50
config_t & config()
Definition ELF/Builder.hpp:136
Builder(Binary &binary)
Definition ELF/Builder.hpp:127
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:37
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:39
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:40
Class which represents an ELF Section.
Definition ELF/Section.hpp:48
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:117
Definition iostream.hpp:32
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
LIEF namespace.
Definition Abstract/Binary.hpp:40
Configuration options to tweak the building process.
Definition ELF/Builder.hpp:55
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:123
bool force_relocate
Force to relocating all the ELF structures that are supported by LIEF (mostly for testing).
Definition ELF/Builder.hpp:115
bool dynamic_section
Rebuild PT_DYNAMIC segment.
Definition ELF/Builder.hpp:63
bool dyn_str
Rebuild DT_STRTAB.
Definition ELF/Builder.hpp:60
bool static_symtab
Rebuild .symtab.
Definition ELF/Builder.hpp:96
bool relr
Rebuild DT_RELR.
Definition ELF/Builder.hpp:87
bool init_array
Rebuild DT_INIT_ARRAY.
Definition ELF/Builder.hpp:72
bool symtab
Rebuild DT_SYMTAB.
Definition ELF/Builder.hpp:108
bool android_rela
Rebuild DT_ANDROID_REL[A].
Definition ELF/Builder.hpp:90
bool sym_versym
Rebuild DT_VERSYM.
Definition ELF/Builder.hpp:105
bool gnu_hash
Rebuild DT_GNU_HASH.
Definition ELF/Builder.hpp:69
bool rela
Rebuild DT_REL[A].
Definition ELF/Builder.hpp:93
bool preinit_array
Rebuild DT_PREINIT_ARRAY.
Definition ELF/Builder.hpp:84
bool sym_verneed
Rebuild DT_VERNEED.
Definition ELF/Builder.hpp:102
bool coredump_notes
Rebuild the Coredump notes.
Definition ELF/Builder.hpp:111
bool fini_array
Rebuild DT_FINI_ARRAY.
Definition ELF/Builder.hpp:66
bool dt_hash
Rebuild DT_HASH.
Definition ELF/Builder.hpp:57
bool interpreter
Rebuild PT_INTERPRETER.
Definition ELF/Builder.hpp:75
bool skip_dynamic
Skip relocating the PT_DYNAMIC segment (only relevant if force_relocate is set.
Definition ELF/Builder.hpp:119
bool notes
Disable note building since it can break the default layout.
Definition ELF/Builder.hpp:81
bool jmprel
Rebuild DT_JMPREL.
Definition ELF/Builder.hpp:78
bool sym_verdef
Rebuild DT_VERDEF.
Definition ELF/Builder.hpp:99
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46