LIEF: Library to Instrument Executable Formats Version 2.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 <string_view>
20#include <array>
21#include <ostream>
22#include <vector>
23
24#include "LIEF/Object.hpp"
25#include "LIEF/visibility.h"
26
28#include "LIEF/ELF/enums.hpp"
29
30
31namespace LIEF::ELF {
32class Parser;
33
36class LIEF_API Header : public Object {
37 friend class Parser;
38
39 public:
40 using identity_t = std::array<uint8_t, 16>;
41
42 public:
57
60 enum class FILE_TYPE {
61 NONE = 0,
62 REL = 1,
63 EXEC = 2,
64 DYN = 3,
65 CORE = 4,
66 };
67
69 enum class VERSION {
70 NONE = 0,
71 CURRENT = 1,
72 };
73
75 enum class CLASS {
76 NONE = 0,
77 ELF32,
78 ELF64,
79 };
80
82 enum class OS_ABI {
83 SYSTEMV = 0,
84 HPUX = 1,
85 NETBSD = 2,
86 GNU = 3,
87 LINUX = 3,
88 HURD = 4,
89 SOLARIS = 6,
90 AIX = 7,
91 IRIX = 8,
92 FREEBSD = 9,
93 TRU64 = 10,
94 MODESTO = 11,
95 OPENBSD = 12,
96 OPENVMS = 13,
97 NSK = 14,
98 AROS = 15,
99 FENIXOS = 16,
100 CLOUDABI = 17,
101 C6000_ELFABI = 64,
102 AMDGPU_HSA = 64,
103 C6000_LINUX = 65,
104 ARM = 97,
105 STANDALONE = 255,
106 };
107
109 enum class ELF_DATA {
110 NONE = 0,
111 LSB = 1,
112 MSB = 2,
113 };
114
115 Header() = default;
116
117 Header& operator=(const Header&) = default;
118 Header(const Header&) = default;
119
120 ~Header() override = default;
121
124 return file_type_;
125 }
126
129 return machine_type_;
130 }
131
134 return object_file_version_;
135 }
136
138 uint64_t entrypoint() const {
139 return entrypoint_;
140 }
141
143 uint64_t program_headers_offset() const {
144 return program_headers_offset_;
145 }
146
148 uint64_t section_headers_offset() const {
149 return section_headers_offset_;
150 }
151
153 uint32_t processor_flag() const {
154 return processor_flags_;
155 }
156
159 uint32_t header_size() const {
160 return header_size_;
161 }
162
165 uint32_t program_header_size() const {
166 return program_header_size_;
167 }
168
170 uint32_t numberof_segments() const {
171 return numberof_segments_;
172 }
173
176 uint32_t section_header_size() const {
177 return section_header_size_;
178 }
179
184 uint32_t numberof_sections() const {
185 return numberof_sections_;
186 }
187
189 uint32_t section_name_table_idx() const {
190 return section_string_table_idx_;
191 }
192
195 return identity_;
196 }
197
198 const identity_t& identity() const {
199 return identity_;
200 }
201
204 return CLASS(identity_[ELI_CLASS]);
205 }
206
209 return ELF_DATA(identity_[ELI_DATA]);
210 }
211
214 return VERSION(identity_[ELI_VERSION]);
215 }
216
219 return OS_ABI(identity_[ELI_OSABI]);
220 }
221
223 uint32_t identity_abi_version() const {
224 return identity_[ELI_ABIVERSION];
225 }
226
229 bool is_mips_n64() const {
231 }
232
233 bool has(PROCESSOR_FLAGS flag) const;
234
235 std::vector<PROCESSOR_FLAGS> flags_list() const;
236
237 void file_type(FILE_TYPE type) {
238 file_type_ = type;
239 }
240
241 void machine_type(ARCH arch) {
242 machine_type_ = arch;
243 }
244
246 object_file_version_ = version;
247 }
248
249 void entrypoint(uint64_t entry) {
250 entrypoint_ = entry;
251 }
252
253 void program_headers_offset(uint64_t offset) {
254 program_headers_offset_ = offset;
255 }
256
257 void section_headers_offset(uint64_t offset) {
258 section_headers_offset_ = offset;
259 }
260
261 void processor_flag(uint32_t flags) {
262 processor_flags_ = flags;
263 }
264
265 void header_size(uint32_t size) {
266 header_size_ = size;
267 }
268
269 void program_header_size(uint32_t size) {
270 program_header_size_ = size;
271 }
272
273 void numberof_segments(uint32_t n) {
274 numberof_segments_ = n;
275 }
276 void section_header_size(uint32_t size) {
277 section_header_size_ = size;
278 }
279
280 void numberof_sections(uint32_t n) {
281 numberof_sections_ = n;
282 }
283 void section_name_table_idx(uint32_t idx) {
284 section_string_table_idx_ = idx;
285 }
286
287 void identity(std::string_view identity);
289
291 identity_[ELI_CLASS] = static_cast<uint8_t>(cls);
292 }
293
295 identity_[ELI_DATA] = static_cast<uint8_t>(data);
296 }
297
299 identity_[ELI_VERSION] = static_cast<uint8_t>(version);
300 }
301
303 identity_[ELI_OSABI] = static_cast<uint8_t>(osabi);
304 }
305
307 identity_[ELI_ABIVERSION] = version;
308 }
309
310 void accept(Visitor& visitor) const override;
311
312 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& hdr);
313
314 private:
315 template<class T>
316 LIEF_LOCAL Header(const T& header);
317
318 identity_t identity_{};
319 FILE_TYPE file_type_ = FILE_TYPE::NONE;
320 ARCH machine_type_ = ARCH::NONE;
321 VERSION object_file_version_ = VERSION::NONE;
322 uint64_t entrypoint_ = 0;
323 uint64_t program_headers_offset_ = 0;
324 uint64_t section_headers_offset_ = 0;
325 uint32_t processor_flags_ = 0;
326 uint32_t header_size_ = 0;
327 uint32_t program_header_size_ = 0;
328 uint32_t numberof_segments_ = 0;
329 uint32_t section_header_size_ = 0;
330 uint32_t numberof_sections_ = 0;
331 uint32_t section_string_table_idx_ = 0;
332};
333
339
340}
341
342#endif
void identity(const identity_t &identity)
uint32_t numberof_sections() const
Return the number of sections.
Definition ELF/Header.hpp:184
std::array< uint8_t, 16 > identity_t
Definition ELF/Header.hpp:40
uint32_t processor_flag() const
Processor-specific flags.
Definition ELF/Header.hpp:153
void section_headers_offset(uint64_t offset)
Definition ELF/Header.hpp:257
uint32_t numberof_segments() const
Return the number of segments.
Definition ELF/Header.hpp:170
uint64_t program_headers_offset() const
Offset of the programs table (also known as segments table).
Definition ELF/Header.hpp:143
uint64_t entrypoint() const
Executable entrypoint.
Definition ELF/Header.hpp:138
friend std::ostream & operator<<(std::ostream &os, const Header &hdr)
void program_headers_offset(uint64_t offset)
Definition ELF/Header.hpp:253
VERSION identity_version() const
Definition ELF/Header.hpp:213
std::vector< PROCESSOR_FLAGS > flags_list() const
const identity_t & identity() const
Definition ELF/Header.hpp:198
ELF_DATA
Match the result Elfxx_Ehdr.e_ident[EI_DATA].
Definition ELF/Header.hpp:109
void header_size(uint32_t size)
Definition ELF/Header.hpp:265
~Header() override=default
void machine_type(ARCH arch)
Definition ELF/Header.hpp:241
VERSION
Match the result of Elfxx_Ehdr.e_version.
Definition ELF/Header.hpp:69
void identity_data(ELF_DATA data)
Definition ELF/Header.hpp:294
void identity(std::string_view identity)
ELF_DATA identity_data() const
Specify the data encoding.
Definition ELF/Header.hpp:208
FILE_TYPE
The type of the underlying ELF file. This enum matches the semantic of ET_NONE, ET_REL,...
Definition ELF/Header.hpp:60
identity_t & identity()
Return the ELF identity as an std::array.
Definition ELF/Header.hpp:194
CLASS identity_class() const
Return the object's class. ELF64 or ELF32.
Definition ELF/Header.hpp:203
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:165
void accept(Visitor &visitor) const override
Header(const Header &)=default
ELF_INDENT
e_ident size and indices.
Definition ELF/Header.hpp:44
@ ELI_MAG2
File identification index.
Definition ELF/Header.hpp:47
@ ELI_OSABI
OS/ABI identification.
Definition ELF/Header.hpp:52
@ ELI_MAG1
File identification index.
Definition ELF/Header.hpp:46
@ ELI_CLASS
File class.
Definition ELF/Header.hpp:49
@ ELI_VERSION
File version.
Definition ELF/Header.hpp:51
@ ELI_MAG3
File identification index.
Definition ELF/Header.hpp:48
@ ELI_PAD
Start of padding bytes.
Definition ELF/Header.hpp:54
@ ELI_NIDENT
Number of bytes in e_ident.
Definition ELF/Header.hpp:55
@ ELI_MAG0
File identification index.
Definition ELF/Header.hpp:45
@ ELI_DATA
Data encoding.
Definition ELF/Header.hpp:50
@ ELI_ABIVERSION
ABI version.
Definition ELF/Header.hpp:53
void section_header_size(uint32_t size)
Definition ELF/Header.hpp:276
uint64_t section_headers_offset() const
Offset of the sections table.
Definition ELF/Header.hpp:148
Header & operator=(const Header &)=default
void identity_os_abi(OS_ABI osabi)
Definition ELF/Header.hpp:302
bool has(PROCESSOR_FLAGS flag) const
void object_file_version(VERSION version)
Definition ELF/Header.hpp:245
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:159
OS_ABI identity_os_abi() const
Identifies the version of the ABI for which the object is prepared.
Definition ELF/Header.hpp:218
void processor_flag(uint32_t flags)
Definition ELF/Header.hpp:261
uint32_t section_name_table_idx() const
Return the section's index which contains sections' names.
Definition ELF/Header.hpp:189
ARCH machine_type() const
Target architecture.
Definition ELF/Header.hpp:128
friend class Parser
Definition ELF/Header.hpp:37
void numberof_sections(uint32_t n)
Definition ELF/Header.hpp:280
void entrypoint(uint64_t entry)
Definition ELF/Header.hpp:249
uint32_t identity_abi_version() const
ABI Version.
Definition ELF/Header.hpp:223
void numberof_segments(uint32_t n)
Definition ELF/Header.hpp:273
void identity_class(CLASS cls)
Definition ELF/Header.hpp:290
bool is_mips_n64() const
Check whether this header identifies a 64-bit MIPS ELF file whose relocations use the MIPS-specific n...
Definition ELF/Header.hpp:229
FILE_TYPE file_type() const
Define the object file type. (e.g. executable, library...).
Definition ELF/Header.hpp:123
void program_header_size(uint32_t size)
Definition ELF/Header.hpp:269
void identity_version(VERSION version)
Definition ELF/Header.hpp:298
void file_type(FILE_TYPE type)
Definition ELF/Header.hpp:237
OS_ABI
Match the result Elfxx_Ehdr.e_ident[EI_OSABI].
Definition ELF/Header.hpp:82
CLASS
Match the result of Elfxx_Ehdr.e_ident[EI_CLASS].
Definition ELF/Header.hpp:75
@ ELF64
64-bit objects
Definition ELF/Header.hpp:78
void section_name_table_idx(uint32_t idx)
Definition ELF/Header.hpp:283
VERSION object_file_version() const
Version of the object file format.
Definition ELF/Header.hpp:133
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:176
void identity_abi_version(uint8_t version)
Definition ELF/Header.hpp:306
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:47
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:28
@ MIPS
Definition ELF/enums.hpp:37
@ ARM
Definition ELF/enums.hpp:52
@ NONE
Definition ELF/enums.hpp:29
PROCESSOR_FLAGS
Definition ProcessorFlags.hpp:33
lief_version_t version()
Return the current version.
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46