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 <functional>
21#include <memory>
22#include <set>
23#include <string>
24#include <unordered_map>
25#include <vector>
26
27#include "LIEF/errors.hpp"
28
29#include "LIEF/visibility.h"
30#include "LIEF/iostream.hpp"
31#include "LIEF/ELF/enums.hpp"
32
33namespace LIEF {
34namespace ELF {
35class Binary;
36class Layout;
37class Header;
38class Note;
39class DynamicEntryArray;
40class DynamicEntry;
41class Section;
42class ExeLayout;
43class ObjectFileLayout;
44class Layout;
45class Relocation;
46
51class LIEF_API Builder {
52 friend class ObjectFileLayout;
53 friend class Layout;
54 friend class ExeLayout;
55
56 public:
58 struct config_t {
59 bool dt_hash = true;
60 bool dyn_str = true;
61 bool dynamic_section = true;
62 bool fini_array = true;
63 bool gnu_hash = true;
64 bool init_array = true;
65 bool interpreter = true;
66 bool jmprel = true;
67 bool notes = false;
68 bool preinit_array = true;
69 bool relr = true;
70 bool android_rela = true;
71 bool rela = true;
72 bool static_symtab = true;
73 bool sym_verdef = true;
74 bool sym_verneed = true;
75 bool sym_versym = true;
76 bool symtab = true;
77
78 bool force_relocate = false;
79 };
80
81 Builder(Binary& binary);
82
83 Builder() = delete;
84 ~Builder();
85
87 void build();
88
91 config_ = std::move(conf);
92 return *this;
93 }
94
95 config_t& config() {
96 return config_;
97 }
98
100 const std::vector<uint8_t>& get_build();
101
103 void write(const std::string& filename) const;
104
106 void write(std::ostream& os) const;
107
108 protected:
109 template<typename ELF_T>
110 ok_error_t build();
111
112 template<typename ELF_T>
113 ok_error_t build_relocatable();
114
115 template<typename ELF_T>
116 ok_error_t build_exe_lib();
117
118 template<typename ELF_T>
119 ok_error_t build(const Header& header);
120
121 template<typename ELF_T>
122 ok_error_t build_sections();
123
124 template<typename ELF_T>
125 ok_error_t build_segments();
126
127 template<typename ELF_T>
128 ok_error_t build_symtab_symbols();
129
130 template<typename ELF_T>
131 ok_error_t build_dynamic();
132
133 template<typename ELF_T>
134 ok_error_t build_dynamic_section();
135
136 template<typename ELF_T>
137 ok_error_t build_dynamic_symbols();
138
139 template<typename ELF_T>
140 ok_error_t build_obj_symbols();
141
142 template<typename ELF_T>
143 ok_error_t build_dynamic_relocations();
144
145 template<typename ELF_T>
146 ok_error_t build_relative_relocations();
147
148 template<typename ELF_T>
149 ok_error_t build_android_relocations();
150
151 template<typename ELF_T>
152 ok_error_t build_pltgot_relocations();
153
154 template<typename ELF_T>
155 ok_error_t build_section_relocations();
156
157 uint32_t sort_dynamic_symbols();
158
159 template<typename ELF_T>
160 ok_error_t build_hash_table();
161
162 template<typename ELF_T>
163 ok_error_t build_symbol_hash();
164
165 ok_error_t build_empty_symbol_gnuhash();
166
167 template<typename ELF_T>
168 ok_error_t build_symbol_requirement();
169
170 template<typename ELF_T>
171 ok_error_t build_symbol_definition();
172
173 template<typename ELF_T>
174 ok_error_t build_symbol_version();
175
176 template<typename ELF_T>
177 ok_error_t build_interpreter();
178
179 template<typename ELF_T>
180 ok_error_t build_notes();
181
182 ok_error_t update_note_section(const Note& note, std::set<const Note*>& notes);
183
184 template<typename ELF_T>
185 ok_error_t build_overlay();
186
187 bool should_swap() const;
188
189 template<class ELF_T>
190 ok_error_t process_object_relocations();
191
192 config_t config_;
193 mutable vector_iostream ios_;
194 Binary* binary_{nullptr};
195 std::unique_ptr<Layout> layout_;
196
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
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:51
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:90
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:36
Definition iostream.hpp:30
LIEF namespace.
Definition Abstract/Binary.hpp:32
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:106
Configuration options to tweak the building process.
Definition ELF/Builder.hpp:58