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 {
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 =
65 false;
66 bool preinit_array = true;
67 bool relr = true;
68 bool android_rela = true;
69 bool rela = true;
70 bool static_symtab = true;
71 bool sym_verdef = true;
72 bool sym_verneed = true;
73 bool sym_versym = true;
74 bool symtab = true;
75 bool coredump_notes = true;
77 false;
79 bool skip_dynamic = false;
81
85 };
86
87 Builder(Binary& binary, const config_t& config);
88 Builder(Binary& binary) :
89 Builder(binary, config_t()) {}
90
91 Builder() = delete;
93
95 void build();
96
98 return config_;
99 }
100
102 const std::vector<uint8_t>& get_build();
103
105 void write(const std::string& filename) const;
106
108 void write(std::ostream& os) const;
109
110 protected:
111 template<typename ELF_T>
113
114 template<typename ELF_T>
115 LIEF_LOCAL ok_error_t build_relocatable();
116
117 template<typename ELF_T>
118 LIEF_LOCAL ok_error_t build_exe_lib();
119
120 template<typename ELF_T>
121 LIEF_LOCAL ok_error_t build(const Header& header);
122
123 template<typename ELF_T>
124 LIEF_LOCAL ok_error_t build_sections();
125
126 template<typename ELF_T>
127 LIEF_LOCAL ok_error_t build_segments();
128
129 template<typename ELF_T>
130 LIEF_LOCAL ok_error_t build_symtab_symbols();
131
132 template<typename ELF_T>
133 LIEF_LOCAL ok_error_t build_dynamic();
134
135 template<typename ELF_T>
136 LIEF_LOCAL ok_error_t build_dynamic_section();
137
138 template<typename ELF_T>
139 LIEF_LOCAL ok_error_t build_dynamic_symbols();
140
141 template<typename ELF_T>
142 LIEF_LOCAL ok_error_t build_obj_symbols();
143
144 template<typename ELF_T>
145 LIEF_LOCAL ok_error_t build_dynamic_relocations();
146
147 template<typename ELF_T>
148 LIEF_LOCAL ok_error_t build_relative_relocations();
149
150 template<typename ELF_T>
151 LIEF_LOCAL ok_error_t build_android_relocations();
152
153 template<typename ELF_T>
154 LIEF_LOCAL ok_error_t build_pltgot_relocations();
155
156 template<typename ELF_T>
157 LIEF_LOCAL ok_error_t build_section_relocations();
158
159 LIEF_LOCAL uint32_t sort_dynamic_symbols();
160
161 template<typename ELF_T>
162 LIEF_LOCAL ok_error_t build_hash_table();
163
164 template<typename ELF_T>
165 LIEF_LOCAL ok_error_t build_symbol_hash();
166
167 LIEF_LOCAL ok_error_t build_empty_symbol_gnuhash();
168
169 template<typename ELF_T>
170 LIEF_LOCAL ok_error_t build_symbol_requirement();
171
172 template<typename ELF_T>
173 LIEF_LOCAL ok_error_t build_symbol_definition();
174
175 template<typename ELF_T>
176 LIEF_LOCAL ok_error_t build_symbol_version();
177
178 template<typename ELF_T>
179 LIEF_LOCAL ok_error_t build_interpreter();
180
181 template<typename ELF_T>
182 LIEF_LOCAL ok_error_t build_notes();
183
184 LIEF_LOCAL ok_error_t update_note_section(const Note& note,
185 std::set<const Note*>& notes);
186
187 template<typename ELF_T>
188 LIEF_LOCAL ok_error_t build_overlay();
189
190 LIEF_LOCAL bool should_swap() const;
191
192 template<class ELF_T>
193 LIEF_LOCAL ok_error_t process_object_relocations();
194
195 LIEF_LOCAL bool should_build_notes() const;
196
197 config_t config_;
198 mutable vector_iostream ios_;
199 Binary* binary_ = nullptr;
200 std::unique_ptr<Layout> layout_;
201};
202
203} // namespace ELF
204} // namespace LIEF
205
206
207#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.
void write(std::ostream &os) const
Write the built ELF binary in the stream os given in parameter.
friend class Layout
Definition ELF/Builder.hpp:50
config_t & config()
Definition ELF/Builder.hpp:97
Builder(Binary &binary)
Definition ELF/Builder.hpp:88
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:114
Definition iostream.hpp:32
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
Skip relocating the PT_DYNAMIC segment (only relevant if force_relocate is set).
Definition ELF/Builder.hpp:84
bool force_relocate
Rebuild the Coredump notes.
Definition ELF/Builder.hpp:76
bool dynamic_section
Rebuild DT_STRTAB.
Definition ELF/Builder.hpp:58
bool dyn_str
Rebuild DT_HASH.
Definition ELF/Builder.hpp:57
bool static_symtab
Rebuild DT_REL[A].
Definition ELF/Builder.hpp:70
bool relr
Rebuild DT_PREINIT_ARRAY.
Definition ELF/Builder.hpp:67
bool init_array
Rebuild DT_GNU_HASH.
Definition ELF/Builder.hpp:61
bool symtab
Rebuild DT_VERSYM.
Definition ELF/Builder.hpp:74
bool android_rela
Rebuild DT_RELR.
Definition ELF/Builder.hpp:68
bool sym_versym
Rebuild DT_VERNEED.
Definition ELF/Builder.hpp:73
bool gnu_hash
Rebuild DT_FINI_ARRAY.
Definition ELF/Builder.hpp:60
bool rela
Rebuild DT_ANDROID_REL[A].
Definition ELF/Builder.hpp:69
bool preinit_array
Disable note building since it can break the default layout.
Definition ELF/Builder.hpp:66
bool sym_verneed
Rebuild DT_VERDEF.
Definition ELF/Builder.hpp:72
bool coredump_notes
Rebuild DT_SYMTAB.
Definition ELF/Builder.hpp:75
bool fini_array
Rebuild PT_DYNAMIC segment.
Definition ELF/Builder.hpp:59
bool dt_hash
Definition ELF/Builder.hpp:56
bool interpreter
Rebuild DT_INIT_ARRAY.
Definition ELF/Builder.hpp:62
bool skip_dynamic
Force to relocating all the ELF structures that are supported by LIEF (mostly for testing).
Definition ELF/Builder.hpp:79
bool notes
Rebuild DT_JMPREL.
Definition ELF/Builder.hpp:64
bool jmprel
Rebuild PT_INTERPRETER.
Definition ELF/Builder.hpp:63
bool sym_verdef
Rebuild .symtab.
Definition ELF/Builder.hpp:71
#define LIEF_API
Definition visibility.h:43
#define LIEF_LOCAL
Definition visibility.h:44