LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
ELF/Builder.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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 = 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 bool skip_dynamic = false;
77
81 };
82
83 Builder(Binary& binary, const config_t& config);
84 Builder(Binary& binary) :
85 Builder(binary, config_t())
86 {}
87
88 Builder() = delete;
90
92 void build();
93
95 return config_;
96 }
97
99 const std::vector<uint8_t>& get_build();
100
102 void write(const std::string& filename) const;
103
105 void write(std::ostream& os) const;
106
107 protected:
108 template<typename ELF_T>
110
111 template<typename ELF_T>
112 LIEF_LOCAL ok_error_t build_relocatable();
113
114 template<typename ELF_T>
115 LIEF_LOCAL ok_error_t build_exe_lib();
116
117 template<typename ELF_T>
118 LIEF_LOCAL ok_error_t build(const Header& header);
119
120 template<typename ELF_T>
121 LIEF_LOCAL ok_error_t build_sections();
122
123 template<typename ELF_T>
124 LIEF_LOCAL ok_error_t build_segments();
125
126 template<typename ELF_T>
127 LIEF_LOCAL ok_error_t build_symtab_symbols();
128
129 template<typename ELF_T>
130 LIEF_LOCAL ok_error_t build_dynamic();
131
132 template<typename ELF_T>
133 LIEF_LOCAL ok_error_t build_dynamic_section();
134
135 template<typename ELF_T>
136 LIEF_LOCAL ok_error_t build_dynamic_symbols();
137
138 template<typename ELF_T>
139 LIEF_LOCAL ok_error_t build_obj_symbols();
140
141 template<typename ELF_T>
142 LIEF_LOCAL ok_error_t build_dynamic_relocations();
143
144 template<typename ELF_T>
145 LIEF_LOCAL ok_error_t build_relative_relocations();
146
147 template<typename ELF_T>
148 LIEF_LOCAL ok_error_t build_android_relocations();
149
150 template<typename ELF_T>
151 LIEF_LOCAL ok_error_t build_pltgot_relocations();
152
153 template<typename ELF_T>
154 LIEF_LOCAL ok_error_t build_section_relocations();
155
156 LIEF_LOCAL uint32_t sort_dynamic_symbols();
157
158 template<typename ELF_T>
159 LIEF_LOCAL ok_error_t build_hash_table();
160
161 template<typename ELF_T>
162 LIEF_LOCAL ok_error_t build_symbol_hash();
163
164 LIEF_LOCAL ok_error_t build_empty_symbol_gnuhash();
165
166 template<typename ELF_T>
167 LIEF_LOCAL ok_error_t build_symbol_requirement();
168
169 template<typename ELF_T>
170 LIEF_LOCAL ok_error_t build_symbol_definition();
171
172 template<typename ELF_T>
173 LIEF_LOCAL ok_error_t build_symbol_version();
174
175 template<typename ELF_T>
176 LIEF_LOCAL ok_error_t build_interpreter();
177
178 template<typename ELF_T>
179 LIEF_LOCAL ok_error_t build_notes();
180
181 LIEF_LOCAL ok_error_t update_note_section(const Note& note, std::set<const Note*>& notes);
182
183 template<typename ELF_T>
184 LIEF_LOCAL ok_error_t build_overlay();
185
186 LIEF_LOCAL bool should_swap() const;
187
188 template<class ELF_T>
189 LIEF_LOCAL ok_error_t process_object_relocations();
190
191 LIEF_LOCAL bool should_build_notes() const;
192
193 config_t config_;
194 mutable vector_iostream ios_;
195 Binary* binary_ = nullptr;
196 std::unique_ptr<Layout> layout_;
197};
198
199} // namespace ELF
200} // namespace LIEF
201
202
203
204
205#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:94
Builder(Binary &binary)
Definition ELF/Builder.hpp:84
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:39
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:40
Class which represents an ELF Section.
Definition ELF/Section.hpp:48
Definition iostream.hpp:31
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
LIEF namespace.
Definition Abstract/Binary.hpp:40
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:109
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:80
bool force_relocate
Rebuild the Coredump notes.
Definition ELF/Builder.hpp:75
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:69
bool relr
Rebuild DT_PREINIT_ARRAY.
Definition ELF/Builder.hpp:66
bool init_array
Rebuild DT_GNU_HASH.
Definition ELF/Builder.hpp:61
bool symtab
Rebuild DT_VERSYM.
Definition ELF/Builder.hpp:73
bool android_rela
Rebuild DT_RELR.
Definition ELF/Builder.hpp:67
bool sym_versym
Rebuild DT_VERNEED.
Definition ELF/Builder.hpp:72
bool gnu_hash
Rebuild DT_FINI_ARRAY.
Definition ELF/Builder.hpp:60
bool rela
Rebuild DT_ANDROID_REL[A].
Definition ELF/Builder.hpp:68
bool preinit_array
Disable note building since it can break the default layout.
Definition ELF/Builder.hpp:65
bool sym_verneed
Rebuild DT_VERDEF.
Definition ELF/Builder.hpp:71
bool coredump_notes
Rebuild DT_SYMTAB.
Definition ELF/Builder.hpp:74
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:76
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:70
#define LIEF_API
Definition visibility.h:41
#define LIEF_LOCAL
Definition visibility.h:42