LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
ELF/Builder.hpp
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;
36class DynamicEntryArray;
37class DynamicEntry;
38class Section;
39class ExeLayout;
40class ObjectFileLayout;
41class Layout;
42class Relocation;
43
48class LIEF_API Builder {
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 };
77
78 Builder(Binary& binary);
79
80 Builder() = delete;
81 ~Builder();
82
84 void build();
85
88 config_ = conf;
89 return *this;
90 }
91
92 config_t& config() {
93 return config_;
94 }
95
97 const std::vector<uint8_t>& get_build();
98
100 void write(const std::string& filename) const;
101
103 void write(std::ostream& os) const;
104
105 protected:
106 template<typename ELF_T>
107 ok_error_t build();
108
109 template<typename ELF_T>
110 ok_error_t build_relocatable();
111
112 template<typename ELF_T>
113 ok_error_t build_exe_lib();
114
115 template<typename ELF_T>
116 ok_error_t build(const Header& header);
117
118 template<typename ELF_T>
119 ok_error_t build_sections();
120
121 template<typename ELF_T>
122 ok_error_t build_segments();
123
124 template<typename ELF_T>
125 ok_error_t build_symtab_symbols();
126
127 template<typename ELF_T>
128 ok_error_t build_dynamic();
129
130 template<typename ELF_T>
131 ok_error_t build_dynamic_section();
132
133 template<typename ELF_T>
134 ok_error_t build_dynamic_symbols();
135
136 template<typename ELF_T>
137 ok_error_t build_obj_symbols();
138
139 template<typename ELF_T>
140 ok_error_t build_dynamic_relocations();
141
142 template<typename ELF_T>
143 ok_error_t build_relative_relocations();
144
145 template<typename ELF_T>
146 ok_error_t build_android_relocations();
147
148 template<typename ELF_T>
149 ok_error_t build_pltgot_relocations();
150
151 template<typename ELF_T>
152 ok_error_t build_section_relocations();
153
154 uint32_t sort_dynamic_symbols();
155
156 template<typename ELF_T>
157 ok_error_t build_hash_table();
158
159 template<typename ELF_T>
160 ok_error_t build_symbol_hash();
161
162 ok_error_t build_empty_symbol_gnuhash();
163
164 template<typename ELF_T>
165 ok_error_t build_symbol_requirement();
166
167 template<typename ELF_T>
168 ok_error_t build_symbol_definition();
169
170 template<typename ELF_T>
171 ok_error_t build_symbol_version();
172
173 template<typename ELF_T>
174 ok_error_t build_interpreter();
175
176 template<typename ELF_T>
177 ok_error_t build_notes();
178
179 ok_error_t update_note_section(const Note& note, std::set<const Note*>& notes);
180
181 template<typename ELF_T>
182 ok_error_t build_overlay();
183
184 bool should_swap() const;
185
186 template<class ELF_T>
187 ok_error_t process_object_relocations();
188
189 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
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:48
void build()
Perform the build of the provided ELF binary.
void write(const std::string &filename) const
Write the built ELF binary in the filename given in parameter.
Builder & set_config(config_t conf)
Tweak the ELF builder with the provided config parameter.
Definition ELF/Builder.hpp:87
void write(std::ostream &os) const
Write the built ELF binary in the stream os given in parameter.
const std::vector< uint8_t > & get_build()
Return the built ELF binary as a byte vector.
Class which represents the ELF's header. This class mirrors the raw ELF Elfxx_Ehdr structure.
Definition ELF/Header.hpp:37
Class which represents an ELF note. This class can be instantiated using the static Note::create func...
Definition Note.hpp:39
Definition iostream.hpp:29
LIEF namespace.
Definition Abstract/Binary.hpp:31
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:107
Configuration options to tweak the building process.
Definition ELF/Builder.hpp:55