LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
ELF/Header.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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 public:
38 using identity_t = std::array<uint8_t, 16>;
39
40 public:
55
58 enum class FILE_TYPE {
59 NONE = 0,
60 REL = 1,
61 EXEC = 2,
62 DYN = 3,
63 CORE = 4,
64 };
65
67 enum class VERSION {
68 NONE = 0,
69 CURRENT = 1,
70 };
71
73 enum class CLASS {
74 NONE = 0,
77 };
78
80 enum class OS_ABI {
81 SYSTEMV = 0,
82 HPUX = 1,
83 NETBSD = 2,
84 GNU = 3,
85 LINUX = 3,
86 HURD = 4,
87 SOLARIS = 6,
88 AIX = 7,
89 IRIX = 8,
90 FREEBSD = 9,
91 TRU64 = 10,
92 MODESTO = 11,
93 OPENBSD = 12,
94 OPENVMS = 13,
95 NSK = 14,
96 AROS = 15,
97 FENIXOS = 16,
98 CLOUDABI = 17,
102 ARM = 97,
104 };
105
107 enum class ELF_DATA {
108 NONE = 0,
109 LSB = 1,
110 MSB = 2
111 };
112
113 Header() = default;
114
115 Header& operator=(const Header&) = default;
116 Header(const Header&) = default;
117
118 ~Header() override = default;
119
122 return file_type_;
123 }
124
127 return machine_type_;
128 }
129
132 return object_file_version_;
133 }
134
136 uint64_t entrypoint() const {
137 return entrypoint_;
138 }
139
141 uint64_t program_headers_offset() const {
142 return program_headers_offset_;
143 }
144
146 uint64_t section_headers_offset() const {
147 return section_headers_offset_;
148 }
149
151 uint32_t processor_flag() const {
152 return processor_flags_;
153 }
154
157 uint32_t header_size() const {
158 return header_size_;
159 }
160
163 uint32_t program_header_size() const {
164 return program_header_size_;
165 }
166
168 uint32_t numberof_segments() const {
169 return numberof_segments_;
170 }
171
174 uint32_t section_header_size() const {
175 return section_header_size_;
176 }
177
182 uint32_t numberof_sections() const {
183 return numberof_sections_;
184 }
185
187 uint32_t section_name_table_idx() const {
188 return section_string_table_idx_;
189 }
190
193 return identity_;
194 }
195
196 const identity_t& identity() const {
197 return identity_;
198 }
199
202 return CLASS(identity_[ELI_CLASS]);
203 }
204
207 return ELF_DATA(identity_[ELI_DATA]);
208 }
209
212 return VERSION(identity_[ELI_VERSION]);
213 }
214
217 return OS_ABI(identity_[ELI_OSABI]);
218 }
219
221 uint32_t identity_abi_version() const {
222 return identity_[ELI_ABIVERSION];
223 }
224
225 bool has(PROCESSOR_FLAGS flag) const;
226
227 std::vector<PROCESSOR_FLAGS> flags_list() const;
228
229 void file_type(FILE_TYPE type) {
230 file_type_ = type;
231 }
232
233 void machine_type(ARCH arch) {
234 machine_type_ = arch;
235 }
236
238 object_file_version_ = version;
239 }
240
241 void entrypoint(uint64_t entry) {
242 entrypoint_ = entry;
243 }
244
245 void program_headers_offset(uint64_t offset) {
246 program_headers_offset_ = offset;
247 }
248
249 void section_headers_offset(uint64_t offset) {
250 section_headers_offset_ = offset;
251 }
252
253 void processor_flag(uint32_t flags) {
254 processor_flags_ = flags;
255 }
256
257 void header_size(uint32_t size) {
258 header_size_ = size;
259 }
260
261 void program_header_size(uint32_t size) {
262 program_header_size_ = size;
263 }
264
265 void numberof_segments(uint32_t n) {
266 numberof_segments_ = n;
267 }
268 void section_header_size(uint32_t size) {
269 section_header_size_ = size;
270 }
271
272 void numberof_sections(uint32_t n) {
273 numberof_sections_ = n;
274 }
275 void section_name_table_idx(uint32_t idx) {
276 section_string_table_idx_ = idx;
277 }
278
279 void identity(const std::string& identity);
281
283 identity_[ELI_CLASS] = static_cast<uint8_t>(cls);
284 }
285
287 identity_[ELI_DATA] = static_cast<uint8_t>(data);
288 }
289
291 identity_[ELI_VERSION] = static_cast<uint8_t>(version);
292 }
293
295 identity_[ELI_OSABI] = static_cast<uint8_t>(osabi);
296 }
297
299 identity_[ELI_ABIVERSION] = version;
300 }
301
302 void accept(Visitor& visitor) const override;
303
304 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& hdr);
305
306 private:
307 template<class T>
308 LIEF_LOCAL Header(const T& header);
309
310 identity_t identity_;
311 FILE_TYPE file_type_ = FILE_TYPE::NONE;
312 ARCH machine_type_ = ARCH::NONE;
313 VERSION object_file_version_ = VERSION::NONE;
314 uint64_t entrypoint_ = 0;
315 uint64_t program_headers_offset_ = 0;
316 uint64_t section_headers_offset_ = 0;
317 uint32_t processor_flags_ = 0;
318 uint32_t header_size_ = 0;
319 uint32_t program_header_size_ = 0;
320 uint32_t numberof_segments_ = 0;
321 uint32_t section_header_size_ = 0;
322 uint32_t numberof_sections_ = 0;
323 uint32_t section_string_table_idx_ = 0;
324};
325
331
332}
333}
334#endif
void identity(const identity_t &identity)
uint32_t numberof_sections() const
Return the number of sections.
Definition ELF/Header.hpp:182
std::array< uint8_t, 16 > identity_t
Definition ELF/Header.hpp:38
uint32_t processor_flag() const
Processor-specific flags.
Definition ELF/Header.hpp:151
void section_headers_offset(uint64_t offset)
Definition ELF/Header.hpp:249
uint32_t numberof_segments() const
Return the number of segments.
Definition ELF/Header.hpp:168
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:141
uint64_t entrypoint() const
Executable entrypoint.
Definition ELF/Header.hpp:136
friend std::ostream & operator<<(std::ostream &os, const Header &hdr)
void program_headers_offset(uint64_t offset)
Definition ELF/Header.hpp:245
VERSION identity_version() const
Definition ELF/Header.hpp:211
std::vector< PROCESSOR_FLAGS > flags_list() const
const identity_t & identity() const
Definition ELF/Header.hpp:196
ELF_DATA
Match the result Elfxx_Ehdr.e_ident[EI_DATA].
Definition ELF/Header.hpp:107
@ LSB
Invalid data encoding.
Definition ELF/Header.hpp:109
@ MSB
2's complement, little endian
Definition ELF/Header.hpp:110
void header_size(uint32_t size)
Definition ELF/Header.hpp:257
~Header() override=default
void machine_type(ARCH arch)
Definition ELF/Header.hpp:233
VERSION
Match the result of Elfxx_Ehdr.e_version.
Definition ELF/Header.hpp:67
@ CURRENT
Current version (default).
Definition ELF/Header.hpp:69
@ NONE
Invalid ELF version.
Definition ELF/Header.hpp:68
void identity_data(ELF_DATA data)
Definition ELF/Header.hpp:286
ELF_DATA identity_data() const
Specify the data encoding.
Definition ELF/Header.hpp:206
FILE_TYPE
The type of the underlying ELF file. This enum matches the semantic of ET_NONE, ET_REL,...
Definition ELF/Header.hpp:58
@ NONE
Can't be determined.
Definition ELF/Header.hpp:59
@ DYN
Shared library or a pie-executable.
Definition ELF/Header.hpp:62
@ CORE
Core dump file.
Definition ELF/Header.hpp:63
@ REL
Relocatable file (or object file).
Definition ELF/Header.hpp:60
@ EXEC
non-pie executable
Definition ELF/Header.hpp:61
identity_t & identity()
Return the ELF identity as an std::array.
Definition ELF/Header.hpp:192
CLASS identity_class() const
Return the object's class. ELF64 or ELF32.
Definition ELF/Header.hpp:201
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:163
void accept(Visitor &visitor) const override
Header(const Header &)=default
ELF_INDENT
e_ident size and indices.
Definition ELF/Header.hpp:42
@ ELI_MAG2
File identification index.
Definition ELF/Header.hpp:45
@ ELI_OSABI
OS/ABI identification.
Definition ELF/Header.hpp:50
@ ELI_MAG1
File identification index.
Definition ELF/Header.hpp:44
@ ELI_CLASS
File class.
Definition ELF/Header.hpp:47
@ ELI_VERSION
File version.
Definition ELF/Header.hpp:49
@ ELI_MAG3
File identification index.
Definition ELF/Header.hpp:46
@ ELI_PAD
Start of padding bytes.
Definition ELF/Header.hpp:52
@ ELI_NIDENT
Number of bytes in e_ident.
Definition ELF/Header.hpp:53
@ ELI_MAG0
File identification index.
Definition ELF/Header.hpp:43
@ ELI_DATA
Data encoding.
Definition ELF/Header.hpp:48
@ ELI_ABIVERSION
ABI version.
Definition ELF/Header.hpp:51
void section_header_size(uint32_t size)
Definition ELF/Header.hpp:268
uint64_t section_headers_offset() const
Offset of the sections table.
Definition ELF/Header.hpp:146
Header & operator=(const Header &)=default
void identity_os_abi(OS_ABI osabi)
Definition ELF/Header.hpp:294
bool has(PROCESSOR_FLAGS flag) const
void object_file_version(VERSION version)
Definition ELF/Header.hpp:237
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:157
OS_ABI identity_os_abi() const
Identifies the version of the ABI for which the object is prepared.
Definition ELF/Header.hpp:216
void processor_flag(uint32_t flags)
Definition ELF/Header.hpp:253
uint32_t section_name_table_idx() const
Return the section's index which contains sections' names.
Definition ELF/Header.hpp:187
ARCH machine_type() const
Target architecture.
Definition ELF/Header.hpp:126
friend class Parser
Definition ELF/Header.hpp:36
void numberof_sections(uint32_t n)
Definition ELF/Header.hpp:272
void entrypoint(uint64_t entry)
Definition ELF/Header.hpp:241
uint32_t identity_abi_version() const
ABI Version.
Definition ELF/Header.hpp:221
void numberof_segments(uint32_t n)
Definition ELF/Header.hpp:265
void identity_class(CLASS cls)
Definition ELF/Header.hpp:282
FILE_TYPE file_type() const
Define the object file type. (e.g. executable, library...).
Definition ELF/Header.hpp:121
void program_header_size(uint32_t size)
Definition ELF/Header.hpp:261
void identity_version(VERSION version)
Definition ELF/Header.hpp:290
void file_type(FILE_TYPE type)
Definition ELF/Header.hpp:229
OS_ABI
Match the result Elfxx_Ehdr.e_ident[EI_OSABI].
Definition ELF/Header.hpp:80
@ OPENBSD
Novell Modesto.
Definition ELF/Header.hpp:93
@ TRU64
FreeBSD.
Definition ELF/Header.hpp:91
@ SYSTEMV
Definition ELF/Header.hpp:81
@ FREEBSD
IRIX.
Definition ELF/Header.hpp:90
@ AMDGPU_HSA
Bare-metal TMS320C6000.
Definition ELF/Header.hpp:100
@ IRIX
AIX.
Definition ELF/Header.hpp:89
@ C6000_LINUX
AMD HSA runtime.
Definition ELF/Header.hpp:101
@ MODESTO
TRU64 UNIX.
Definition ELF/Header.hpp:92
@ NSK
OpenVMS.
Definition ELF/Header.hpp:95
@ NETBSD
HP-UX operating system.
Definition ELF/Header.hpp:83
@ ARM
Linux TMS320C6000.
Definition ELF/Header.hpp:102
@ AIX
Solaris.
Definition ELF/Header.hpp:88
@ CLOUDABI
FenixOS.
Definition ELF/Header.hpp:98
@ SOLARIS
GNU/Hurd.
Definition ELF/Header.hpp:87
@ GNU
NetBSD.
Definition ELF/Header.hpp:84
@ STANDALONE
ARM.
Definition ELF/Header.hpp:103
@ HPUX
UNIX System V ABI.
Definition ELF/Header.hpp:82
@ HURD
Historical alias for ELFOSABI_GNU.
Definition ELF/Header.hpp:86
@ LINUX
GNU/Linux.
Definition ELF/Header.hpp:85
@ AROS
Hewlett-Packard Non-Stop Kernel.
Definition ELF/Header.hpp:96
@ OPENVMS
OpenBSD.
Definition ELF/Header.hpp:94
@ C6000_ELFABI
Nuxi CloudABI.
Definition ELF/Header.hpp:99
@ FENIXOS
AROS.
Definition ELF/Header.hpp:97
CLASS
Match the result of Elfxx_Ehdr.e_ident[EI_CLASS].
Definition ELF/Header.hpp:73
@ ELF64
32-bit objects
Definition ELF/Header.hpp:76
@ ELF32
Invalid class.
Definition ELF/Header.hpp:75
void section_name_table_idx(uint32_t idx)
Definition ELF/Header.hpp:275
VERSION object_file_version() const
Version of the object file format.
Definition ELF/Header.hpp:131
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:174
void identity_abi_version(uint8_t version)
Definition ELF/Header.hpp:298
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
Definition Visitor.hpp:210
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
@ 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:41
#define LIEF_LOCAL
Definition visibility.h:42