LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
ELF/Header.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_ELF_HEADER_H
17#define LIEF_ELF_HEADER_H
18
19#include <ostream>
20#include <array>
21#include <vector>
22
23#include "LIEF/Object.hpp"
24#include "LIEF/visibility.h"
25
26#include "LIEF/ELF/enums.hpp"
28
29namespace LIEF {
30namespace ELF {
31class Parser;
32
35class LIEF_API Header : public Object {
36 friend class Parser;
37
38 public:
39 using identity_t = std::array<uint8_t, 16>;
40
41 public:
56
59 enum class FILE_TYPE {
60 NONE = 0,
61 REL = 1,
62 EXEC = 2,
63 DYN = 3,
64 CORE = 4,
65 };
66
68 enum class VERSION {
69 NONE = 0,
70 CURRENT = 1,
71 };
72
74 enum class CLASS {
75 NONE = 0,
76 ELF32,
77 ELF64,
78 };
79
81 enum class OS_ABI {
82 SYSTEMV = 0,
83 HPUX = 1,
84 NETBSD = 2,
85 GNU = 3,
86 LINUX = 3,
87 HURD = 4,
88 SOLARIS = 6,
89 AIX = 7,
90 IRIX = 8,
91 FREEBSD = 9,
92 TRU64 = 10,
93 MODESTO = 11,
94 OPENBSD = 12,
95 OPENVMS = 13,
96 NSK = 14,
97 AROS = 15,
98 FENIXOS = 16,
99 CLOUDABI = 17,
100 C6000_ELFABI = 64,
101 AMDGPU_HSA = 64,
102 C6000_LINUX = 65,
103 ARM = 97,
104 STANDALONE = 255,
105 };
106
108 enum class ELF_DATA {
109 NONE = 0,
110 LSB = 1,
111 MSB = 2,
112 };
113
114 Header() = default;
115
116 Header& operator=(const Header&) = default;
117 Header(const Header&) = default;
118
119 ~Header() override = default;
120
123 return file_type_;
124 }
125
128 return machine_type_;
129 }
130
133 return object_file_version_;
134 }
135
137 uint64_t entrypoint() const {
138 return entrypoint_;
139 }
140
142 uint64_t program_headers_offset() const {
143 return program_headers_offset_;
144 }
145
147 uint64_t section_headers_offset() const {
148 return section_headers_offset_;
149 }
150
152 uint32_t processor_flag() const {
153 return processor_flags_;
154 }
155
158 uint32_t header_size() const {
159 return header_size_;
160 }
161
164 uint32_t program_header_size() const {
165 return program_header_size_;
166 }
167
169 uint32_t numberof_segments() const {
170 return numberof_segments_;
171 }
172
175 uint32_t section_header_size() const {
176 return section_header_size_;
177 }
178
183 uint32_t numberof_sections() const {
184 return numberof_sections_;
185 }
186
188 uint32_t section_name_table_idx() const {
189 return section_string_table_idx_;
190 }
191
194 return identity_;
195 }
196
197 const identity_t& identity() const {
198 return identity_;
199 }
200
203 return CLASS(identity_[ELI_CLASS]);
204 }
205
208 return ELF_DATA(identity_[ELI_DATA]);
209 }
210
213 return VERSION(identity_[ELI_VERSION]);
214 }
215
218 return OS_ABI(identity_[ELI_OSABI]);
219 }
220
222 uint32_t identity_abi_version() const {
223 return identity_[ELI_ABIVERSION];
224 }
225
226 bool has(PROCESSOR_FLAGS flag) const;
227
228 std::vector<PROCESSOR_FLAGS> flags_list() const;
229
230 void file_type(FILE_TYPE type) {
231 file_type_ = type;
232 }
233
234 void machine_type(ARCH arch) {
235 machine_type_ = arch;
236 }
237
239 object_file_version_ = version;
240 }
241
242 void entrypoint(uint64_t entry) {
243 entrypoint_ = entry;
244 }
245
246 void program_headers_offset(uint64_t offset) {
247 program_headers_offset_ = offset;
248 }
249
250 void section_headers_offset(uint64_t offset) {
251 section_headers_offset_ = offset;
252 }
253
254 void processor_flag(uint32_t flags) {
255 processor_flags_ = flags;
256 }
257
258 void header_size(uint32_t size) {
259 header_size_ = size;
260 }
261
262 void program_header_size(uint32_t size) {
263 program_header_size_ = size;
264 }
265
266 void numberof_segments(uint32_t n) {
267 numberof_segments_ = n;
268 }
269 void section_header_size(uint32_t size) {
270 section_header_size_ = size;
271 }
272
273 void numberof_sections(uint32_t n) {
274 numberof_sections_ = n;
275 }
276 void section_name_table_idx(uint32_t idx) {
277 section_string_table_idx_ = idx;
278 }
279
280 void identity(const std::string& identity);
282
284 identity_[ELI_CLASS] = static_cast<uint8_t>(cls);
285 }
286
288 identity_[ELI_DATA] = static_cast<uint8_t>(data);
289 }
290
292 identity_[ELI_VERSION] = static_cast<uint8_t>(version);
293 }
294
296 identity_[ELI_OSABI] = static_cast<uint8_t>(osabi);
297 }
298
300 identity_[ELI_ABIVERSION] = version;
301 }
302
303 void accept(Visitor& visitor) const override;
304
305 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& hdr);
306
307 private:
308 template<class T>
309 LIEF_LOCAL Header(const T& header);
310
311 identity_t identity_;
312 FILE_TYPE file_type_ = FILE_TYPE::NONE;
313 ARCH machine_type_ = ARCH::NONE;
314 VERSION object_file_version_ = VERSION::NONE;
315 uint64_t entrypoint_ = 0;
316 uint64_t program_headers_offset_ = 0;
317 uint64_t section_headers_offset_ = 0;
318 uint32_t processor_flags_ = 0;
319 uint32_t header_size_ = 0;
320 uint32_t program_header_size_ = 0;
321 uint32_t numberof_segments_ = 0;
322 uint32_t section_header_size_ = 0;
323 uint32_t numberof_sections_ = 0;
324 uint32_t section_string_table_idx_ = 0;
325};
326
332
333}
334}
335#endif
void identity(const identity_t &identity)
uint32_t numberof_sections() const
Return the number of sections.
Definition ELF/Header.hpp:183
std::array< uint8_t, 16 > identity_t
Definition ELF/Header.hpp:39
uint32_t processor_flag() const
Processor-specific flags.
Definition ELF/Header.hpp:152
void section_headers_offset(uint64_t offset)
Definition ELF/Header.hpp:250
uint32_t numberof_segments() const
Return the number of segments.
Definition ELF/Header.hpp:169
void identity(const std::string &identity)
uint64_t program_headers_offset() const
Offset of the programs table (also known as segments table).
Definition ELF/Header.hpp:142
uint64_t entrypoint() const
Executable entrypoint.
Definition ELF/Header.hpp:137
friend std::ostream & operator<<(std::ostream &os, const Header &hdr)
void program_headers_offset(uint64_t offset)
Definition ELF/Header.hpp:246
VERSION identity_version() const
Definition ELF/Header.hpp:212
std::vector< PROCESSOR_FLAGS > flags_list() const
const identity_t & identity() const
Definition ELF/Header.hpp:197
ELF_DATA
Match the result Elfxx_Ehdr.e_ident[EI_DATA].
Definition ELF/Header.hpp:108
void header_size(uint32_t size)
Definition ELF/Header.hpp:258
~Header() override=default
void machine_type(ARCH arch)
Definition ELF/Header.hpp:234
VERSION
Match the result of Elfxx_Ehdr.e_version.
Definition ELF/Header.hpp:68
@ NONE
Invalid ELF version.
Definition ELF/Header.hpp:69
void identity_data(ELF_DATA data)
Definition ELF/Header.hpp:287
ELF_DATA identity_data() const
Specify the data encoding.
Definition ELF/Header.hpp:207
FILE_TYPE
The type of the underlying ELF file. This enum matches the semantic of ET_NONE, ET_REL,...
Definition ELF/Header.hpp:59
@ NONE
Can't be determined.
Definition ELF/Header.hpp:60
identity_t & identity()
Return the ELF identity as an std::array.
Definition ELF/Header.hpp:193
CLASS identity_class() const
Return the object's class. ELF64 or ELF32.
Definition ELF/Header.hpp:202
uint32_t program_header_size() const
Return the size of a program header (i.e. sizeof(Elfxx_Phdr)) This size should be 56 for an ELF64 bin...
Definition ELF/Header.hpp:164
void accept(Visitor &visitor) const override
Header(const Header &)=default
ELF_INDENT
e_ident size and indices.
Definition ELF/Header.hpp:43
@ ELI_MAG2
File identification index.
Definition ELF/Header.hpp:46
@ ELI_OSABI
OS/ABI identification.
Definition ELF/Header.hpp:51
@ ELI_MAG1
File identification index.
Definition ELF/Header.hpp:45
@ ELI_CLASS
File class.
Definition ELF/Header.hpp:48
@ ELI_VERSION
File version.
Definition ELF/Header.hpp:50
@ ELI_MAG3
File identification index.
Definition ELF/Header.hpp:47
@ ELI_PAD
Start of padding bytes.
Definition ELF/Header.hpp:53
@ ELI_NIDENT
Number of bytes in e_ident.
Definition ELF/Header.hpp:54
@ ELI_MAG0
File identification index.
Definition ELF/Header.hpp:44
@ ELI_DATA
Data encoding.
Definition ELF/Header.hpp:49
@ ELI_ABIVERSION
ABI version.
Definition ELF/Header.hpp:52
void section_header_size(uint32_t size)
Definition ELF/Header.hpp:269
uint64_t section_headers_offset() const
Offset of the sections table.
Definition ELF/Header.hpp:147
Header & operator=(const Header &)=default
void identity_os_abi(OS_ABI osabi)
Definition ELF/Header.hpp:295
bool has(PROCESSOR_FLAGS flag) const
void object_file_version(VERSION version)
Definition ELF/Header.hpp:238
uint32_t header_size() const
Size of the current header (i.e. sizeof(Elfxx_Ehdr)) This size should be 64 for an ELF64 binary and 5...
Definition ELF/Header.hpp:158
OS_ABI identity_os_abi() const
Identifies the version of the ABI for which the object is prepared.
Definition ELF/Header.hpp:217
void processor_flag(uint32_t flags)
Definition ELF/Header.hpp:254
uint32_t section_name_table_idx() const
Return the section's index which contains sections' names.
Definition ELF/Header.hpp:188
ARCH machine_type() const
Target architecture.
Definition ELF/Header.hpp:127
friend class Parser
Definition ELF/Header.hpp:36
void numberof_sections(uint32_t n)
Definition ELF/Header.hpp:273
void entrypoint(uint64_t entry)
Definition ELF/Header.hpp:242
uint32_t identity_abi_version() const
ABI Version.
Definition ELF/Header.hpp:222
void numberof_segments(uint32_t n)
Definition ELF/Header.hpp:266
void identity_class(CLASS cls)
Definition ELF/Header.hpp:283
FILE_TYPE file_type() const
Define the object file type. (e.g. executable, library...).
Definition ELF/Header.hpp:122
void program_header_size(uint32_t size)
Definition ELF/Header.hpp:262
void identity_version(VERSION version)
Definition ELF/Header.hpp:291
void file_type(FILE_TYPE type)
Definition ELF/Header.hpp:230
OS_ABI
Match the result Elfxx_Ehdr.e_ident[EI_OSABI].
Definition ELF/Header.hpp:81
CLASS
Match the result of Elfxx_Ehdr.e_ident[EI_CLASS].
Definition ELF/Header.hpp:74
void section_name_table_idx(uint32_t idx)
Definition ELF/Header.hpp:276
VERSION object_file_version() const
Version of the object file format.
Definition ELF/Header.hpp:132
uint32_t section_header_size() const
Return the size of a section header (i.e. sizeof(Elfxx_Shdr)) This size should be 64 for a ELF64 bina...
Definition ELF/Header.hpp:175
void identity_abi_version(uint8_t version)
Definition ELF/Header.hpp:299
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
Definition Visitor.hpp:212
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
const char * to_string(DynamicEntry::TAG e)
ARCH
Definition ELF/enums.hpp:30
@ ARM
Definition ELF/enums.hpp:54
@ NONE
Definition ELF/enums.hpp:31
PROCESSOR_FLAGS
Definition ProcessorFlags.hpp:32
LIEF namespace.
Definition Abstract/Binary.hpp:40
lief_version_t version()
Return the current version.
#define LIEF_API
Definition visibility.h:43
#define LIEF_LOCAL
Definition visibility.h:44