LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
ELF/Header.hpp
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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#include <set>
23
24#include "LIEF/Object.hpp"
25#include "LIEF/visibility.h"
26#include "LIEF/Abstract/enums.hpp"
27
28#include "LIEF/ELF/enums.hpp"
29#include "LIEF/ELF/ProcessorFlags.hpp"
30
31namespace LIEF {
32namespace ELF {
33class Parser;
34
37class LIEF_API Header : public Object {
38 friend class Parser;
39 public:
40 using identity_t = std::array<uint8_t, 16>;
41 using abstract_architecture_t = std::pair<ARCHITECTURES, std::set<MODES>>;
42
43 public:
45 enum {
46 EI_MAG0 = 0,
47 EI_MAG1 = 1,
48 EI_MAG2 = 2,
49 EI_MAG3 = 3,
50 EI_CLASS = 4,
51 EI_DATA = 5,
52 EI_VERSION = 6,
53 EI_OSABI = 7,
54 EI_ABIVERSION = 8,
55 EI_PAD = 9,
56 EI_NIDENT = 16
57 };
58
61 enum class FILE_TYPE {
62 NONE = 0,
63 REL = 1,
64 EXEC = 2,
65 DYN = 3,
66 CORE = 4,
67 };
68
70 enum class VERSION {
71 NONE = 0,
72 CURRENT = 1,
73 };
74
76 enum class CLASS {
77 NONE = 0,
78 ELF32,
79 ELF64,
80 };
81
83 enum class OS_ABI {
84 SYSTEMV = 0,
85 HPUX = 1,
86 NETBSD = 2,
87 GNU = 3,
88 LINUX = 3,
89 HURD = 4,
90 SOLARIS = 6,
91 AIX = 7,
92 IRIX = 8,
93 FREEBSD = 9,
94 TRU64 = 10,
95 MODESTO = 11,
96 OPENBSD = 12,
97 OPENVMS = 13,
98 NSK = 14,
99 AROS = 15,
100 FENIXOS = 16,
101 CLOUDABI = 17,
102 C6000_ELFABI = 64,
103 AMDGPU_HSA = 64,
104 C6000_LINUX = 65,
105 ARM = 97,
106 STANDALONE = 255
107 };
108
110 enum class ELF_DATA {
111 NONE = 0,
112 LSB = 1,
113 MSB = 2
114 };
115
116 Header() = default;
117
118 Header& operator=(const Header&) = default;
119 Header(const Header&) = default;
120
121 ~Header() override = default;
122
125 return file_type_;
126 }
127
129 OBJECT_TYPES abstract_object_type() const;
130
133 return machine_type_;
134 }
135
137 abstract_architecture_t abstract_architecture() const;
138
140 ENDIANNESS abstract_endianness() const;
141
144 return object_file_version_;
145 }
146
148 uint64_t entrypoint() const {
149 return entrypoint_;
150 }
151
153 uint64_t program_headers_offset() const {
154 return program_headers_offset_;
155 }
156
158 uint64_t section_headers_offset() const {
159 return section_headers_offset_;
160 }
161
163 uint32_t processor_flag() const {
164 return processor_flags_;
165 }
166
169 uint32_t header_size() const {
170 return header_size_;
171 }
172
175 uint32_t program_header_size() const {
176 return program_header_size_;
177 }
178
180 uint32_t numberof_segments() const {
181 return numberof_segments_;
182 }
183
186 uint32_t section_header_size() const {
187 return section_header_size_;
188 }
189
194 uint32_t numberof_sections() const {
195 return numberof_sections_;
196 }
197
199 uint32_t section_name_table_idx() const {
200 return section_string_table_idx_;
201 }
202
204 identity_t& identity() {
205 return identity_;
206 }
207
208 const identity_t& identity() const {
209 return identity_;
210 }
211
214 return CLASS(identity_[EI_CLASS]);
215 }
216
219 return ELF_DATA(identity_[EI_DATA]);
220 }
221
224 return VERSION(identity_[EI_VERSION]);
225 }
226
229 return OS_ABI(identity_[EI_OSABI]);
230 }
231
233 uint32_t identity_abi_version() const {
234 return identity_[EI_ABIVERSION];
235 }
236
237 bool has(PROCESSOR_FLAGS flag) const;
238
239 std::vector<PROCESSOR_FLAGS> flags_list() const;
240
241 void file_type(FILE_TYPE type) {
242 file_type_ = type;
243 }
244
245 void machine_type(ARCH arch) {
246 machine_type_ = arch;
247 }
248
249 void object_file_version(VERSION version) {
250 object_file_version_ = version;
251 }
252
253 void entrypoint(uint64_t entry) {
254 entrypoint_ = entry;
255 }
256
257 void program_headers_offset(uint64_t offset) {
258 program_headers_offset_ = offset;
259 }
260
261 void section_headers_offset(uint64_t offset) {
262 section_headers_offset_ = offset;
263 }
264
265 void processor_flag(uint32_t flags) {
266 processor_flags_ = flags;
267 }
268
269 void header_size(uint32_t size) {
270 header_size_ = size;
271 }
272
273 void program_header_size(uint32_t size) {
274 program_header_size_ = size;
275 }
276
277 void numberof_segments(uint32_t n) {
278 numberof_segments_ = n;
279 }
280 void section_header_size(uint32_t size) {
281 section_header_size_ = size;
282 }
283
284 void numberof_sections(uint32_t n) {
285 numberof_sections_ = n;
286 }
287 void section_name_table_idx(uint32_t idx) {
288 section_string_table_idx_ = idx;
289 }
290
291 void identity(const std::string& identity);
292 void identity(const identity_t& identity);
293
294 void identity_class(CLASS cls) {
295 identity_[EI_CLASS] = static_cast<uint8_t>(cls);
296 }
297
298 void identity_data(ELF_DATA data) {
299 identity_[EI_DATA] = static_cast<uint8_t>(data);
300 }
301
302 void identity_version(VERSION version) {
303 identity_[EI_VERSION] = static_cast<uint8_t>(version);
304 }
305
306 void identity_os_abi(OS_ABI osabi) {
307 identity_[EI_OSABI] = static_cast<uint8_t>(osabi);
308 }
309
310 void identity_abi_version(uint8_t version) {
311 identity_[EI_ABIVERSION] = version;
312 }
313
314 void accept(Visitor& visitor) const override;
315
316 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& hdr);
317
318 private:
319 template<class T>
320 LIEF_LOCAL Header(const T& header);
321
322 identity_t identity_;
323 FILE_TYPE file_type_ = FILE_TYPE::NONE;
324 ARCH machine_type_ = ARCH::NONE;
325 VERSION object_file_version_ = VERSION::NONE;
326 uint64_t entrypoint_ = 0;
327 uint64_t program_headers_offset_ = 0;
328 uint64_t section_headers_offset_ = 0;
329 uint32_t processor_flags_ = 0;
330 uint32_t header_size_ = 0;
331 uint32_t program_header_size_ = 0;
332 uint32_t numberof_segments_ = 0;
333 uint32_t section_header_size_ = 0;
334 uint32_t numberof_sections_ = 0;
335 uint32_t section_string_table_idx_ = 0;
336};
337
338LIEF_API const char* to_string(Header::FILE_TYPE type);
339LIEF_API const char* to_string(Header::VERSION version);
340LIEF_API const char* to_string(Header::CLASS version);
341LIEF_API const char* to_string(Header::OS_ABI abi);
342LIEF_API const char* to_string(Header::ELF_DATA abi);
343
344}
345}
346#endif
Class which represents the ELF's header. This class mirrors the raw ELF Elfxx_Ehdr structure.
Definition ELF/Header.hpp:37
uint32_t numberof_sections() const
Return the number of sections.
Definition ELF/Header.hpp:194
uint32_t processor_flag() const
Processor-specific flags.
Definition ELF/Header.hpp:163
uint32_t numberof_segments() const
Return the the number of segments.
Definition ELF/Header.hpp:180
uint64_t program_headers_offset() const
Offset of the programs table (also known as segments table)
Definition ELF/Header.hpp:153
uint64_t entrypoint() const
Executable entrypoint.
Definition ELF/Header.hpp:148
ENDIANNESS abstract_endianness() const
LIEF abstract endianness.
VERSION identity_version() const
Definition ELF/Header.hpp:223
ELF_DATA
Match the result Elfxx_Ehdr.e_ident[EI_DATA]
Definition ELF/Header.hpp:110
abstract_architecture_t abstract_architecture() const
LIEF abstract architecture.
VERSION
Match the result of Elfxx_Ehdr.e_version
Definition ELF/Header.hpp:70
ELF_DATA identity_data() const
Specify the data encoding.
Definition ELF/Header.hpp:218
FILE_TYPE
The type of the underlying ELF file. This enum matches the semantic of ET_NONE, ET_REL,...
Definition ELF/Header.hpp:61
identity_t & identity()
Return the ELF identity as an std::array
Definition ELF/Header.hpp:204
CLASS identity_class() const
Return the object's class. ELF64 or ELF32
Definition ELF/Header.hpp:213
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:175
uint64_t section_headers_offset() const
Offset of the sections table.
Definition ELF/Header.hpp:158
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:169
OS_ABI identity_os_abi() const
Identifies the version of the ABI for which the object is prepared.
Definition ELF/Header.hpp:228
uint32_t section_name_table_idx() const
Return the section's index which contains sections' names.
Definition ELF/Header.hpp:199
ARCH machine_type() const
Target architecture.
Definition ELF/Header.hpp:132
uint32_t identity_abi_version() const
ABI Version.
Definition ELF/Header.hpp:233
FILE_TYPE file_type() const
Define the object file type. (e.g. executable, library...)
Definition ELF/Header.hpp:124
OBJECT_TYPES abstract_object_type() const
LIEF abstract object type.
OS_ABI
Match the result Elfxx_Ehdr.e_ident[EI_OSABI]
Definition ELF/Header.hpp:83
CLASS
Match the result of Elfxx_Ehdr.e_ident[EI_CLASS]
Definition ELF/Header.hpp:76
VERSION object_file_version() const
Version of the object file format.
Definition ELF/Header.hpp:143
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:186
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
Definition Object.hpp:25
art_version_t version(const std::string &file)
Return the ART version of the given file.
ARCH
Machine architectures See current registered ELF machine architectures at: http://www....
Definition ELF/enums.hpp:30
LIEF namespace.
Definition Abstract/Binary.hpp:32