LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
ELF/Header.hpp
Go to the documentation of this file.
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
23#include "LIEF/Object.hpp"
24#include "LIEF/visibility.h"
25
26#include "LIEF/ELF/enums.hpp"
28
29namespace LIEF {
30namespace ELF {
31class Parser;
32class LIEF_API Header : public Object {
36 friend class Parser;
37 public:
38 using identity_t = std::array<uint8_t, 16>;
39
40 public: enum ELF_INDENT {
43 ELI_MAG0 = 0,
44 ELI_MAG1 = 1,
45 ELI_MAG2 = 2,
46 ELI_MAG3 = 3,
47 ELI_CLASS = 4,
48 ELI_DATA = 5,
49 ELI_VERSION = 6,
50 ELI_OSABI = 7,
51 ELI_ABIVERSION = 8,
52 ELI_PAD = 9,
53 ELI_NIDENT = 16
54 };
55 enum class FILE_TYPE {
59 NONE = 0,
60 REL = 1,
61 EXEC = 2,
62 DYN = 3,
63 CORE = 4,
64 };
65 enum class VERSION {
68 NONE = 0,
69 CURRENT = 1,
70 };
71 enum class CLASS {
74 NONE = 0,
75 ELF32,
76 ELF64,
77 };
78 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,
99 C6000_ELFABI = 64,
100 AMDGPU_HSA = 64,
101 C6000_LINUX = 65,
102 ARM = 97,
103 STANDALONE = 255
104 };
105 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 FILE_TYPE file_type() const {
122 return file_type_;
123 }
124 ARCH machine_type() const {
127 return machine_type_;
128 }
129 VERSION object_file_version() const {
132 return object_file_version_;
133 }
134 uint64_t entrypoint() const {
137 return entrypoint_;
138 }
139 uint64_t program_headers_offset() const {
142 return program_headers_offset_;
143 }
144 uint64_t section_headers_offset() const {
147 return section_headers_offset_;
148 }
149 uint32_t processor_flag() const {
152 return processor_flags_;
153 }
154 uint32_t header_size() const {
158 return header_size_;
159 }
160 uint32_t program_header_size() const {
164 return program_header_size_;
165 }
166 uint32_t numberof_segments() const {
169 return numberof_segments_;
170 }
171 uint32_t section_header_size() const {
175 return section_header_size_;
176 }
177 uint32_t numberof_sections() const {
183 return numberof_sections_;
184 }
185 uint32_t section_name_table_idx() const {
188 return section_string_table_idx_;
189 }
190 identity_t& identity() {
193 return identity_;
194 }
195
196 const identity_t& identity() const {
197 return identity_;
198 }
199 CLASS identity_class() const {
202 return CLASS(identity_[ELI_CLASS]);
203 }
204 ELF_DATA identity_data() const {
207 return ELF_DATA(identity_[ELI_DATA]);
208 }
209 VERSION identity_version() const {
212 return VERSION(identity_[ELI_VERSION]);
213 }
214 OS_ABI identity_os_abi() const {
217 return OS_ABI(identity_[ELI_OSABI]);
218 }
219 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
237 void object_file_version(VERSION version) {
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);
280 void identity(const identity_t& identity);
281
282 void identity_class(CLASS cls) {
283 identity_[ELI_CLASS] = static_cast<uint8_t>(cls);
284 }
285
286 void identity_data(ELF_DATA data) {
287 identity_[ELI_DATA] = static_cast<uint8_t>(data);
288 }
289
290 void identity_version(VERSION version) {
291 identity_[ELI_VERSION] = static_cast<uint8_t>(version);
292 }
293
294 void identity_os_abi(OS_ABI osabi) {
295 identity_[ELI_OSABI] = static_cast<uint8_t>(osabi);
296 }
297
298 void identity_abi_version(uint8_t version) {
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
326LIEF_API const char* to_string(Header::FILE_TYPE type);
327LIEF_API const char* to_string(Header::VERSION version);
328LIEF_API const char* to_string(Header::CLASS version);
329LIEF_API const char* to_string(Header::OS_ABI abi);
330LIEF_API const char* to_string(Header::ELF_DATA abi);
331
332}
333}
334#endif
enums.hpp
Object.hpp
ProcessorFlags.hpp
LIEF::ELF::Header
Class which represents the ELF's header. This class mirrors the raw ELF Elfxx_Ehdr structure.
Definition ELF/Header.hpp:35
LIEF::ELF::Header::identity
void identity(const identity_t &identity)
LIEF::ELF::Header::numberof_sections
uint32_t numberof_sections() const
Return the number of sections.
Definition ELF/Header.hpp:182
LIEF::ELF::Header::processor_flag
uint32_t processor_flag() const
Processor-specific flags.
Definition ELF/Header.hpp:151
LIEF::ELF::Header::section_headers_offset
void section_headers_offset(uint64_t offset)
Definition ELF/Header.hpp:249
LIEF::ELF::Header::numberof_segments
uint32_t numberof_segments() const
Return the number of segments.
Definition ELF/Header.hpp:168
LIEF::ELF::Header::identity
void identity(const std::string &identity)
LIEF::ELF::Header::Header
Header()=default
LIEF::ELF::Header::program_headers_offset
uint64_t program_headers_offset() const
Offset of the programs table (also known as segments table)
Definition ELF/Header.hpp:141
LIEF::ELF::Header::entrypoint
uint64_t entrypoint() const
Executable entrypoint.
Definition ELF/Header.hpp:136
LIEF::ELF::Header::operator<<
friend std::ostream & operator<<(std::ostream &os, const Header &hdr)
LIEF::ELF::Header::program_headers_offset
void program_headers_offset(uint64_t offset)
Definition ELF/Header.hpp:245
LIEF::ELF::Header::identity_version
VERSION identity_version() const
Definition ELF/Header.hpp:211
LIEF::ELF::Header::flags_list
std::vector< PROCESSOR_FLAGS > flags_list() const
LIEF::ELF::Header::identity
const identity_t & identity() const
Definition ELF/Header.hpp:196
LIEF::ELF::Header::ELF_DATA
ELF_DATA
Match the result Elfxx_Ehdr.e_ident[EI_DATA]
Definition ELF/Header.hpp:107
LIEF::ELF::Header::header_size
void header_size(uint32_t size)
Definition ELF/Header.hpp:257
LIEF::ELF::Header::~Header
~Header() override=default
LIEF::ELF::Header::machine_type
void machine_type(ARCH arch)
Definition ELF/Header.hpp:233
LIEF::ELF::Header::VERSION
VERSION
Match the result of Elfxx_Ehdr.e_version
Definition ELF/Header.hpp:67
LIEF::ELF::Header::identity_data
void identity_data(ELF_DATA data)
Definition ELF/Header.hpp:286
LIEF::ELF::Header::identity_data
ELF_DATA identity_data() const
Specify the data encoding.
Definition ELF/Header.hpp:206
LIEF::ELF::Header::FILE_TYPE
FILE_TYPE
The type of the underlying ELF file. This enum matches the semantic of ET_NONE, ET_REL,...
Definition ELF/Header.hpp:58
LIEF::ELF::Header::identity
identity_t & identity()
Return the ELF identity as an std::array
Definition ELF/Header.hpp:192
LIEF::ELF::Header::identity_class
CLASS identity_class() const
Return the object's class. ELF64 or ELF32
Definition ELF/Header.hpp:201
LIEF::ELF::Header::program_header_size
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
LIEF::ELF::Header::accept
void accept(Visitor &visitor) const override
LIEF::ELF::Header::Header
Header(const Header &)=default
LIEF::ELF::Header::section_header_size
void section_header_size(uint32_t size)
Definition ELF/Header.hpp:268
LIEF::ELF::Header::section_headers_offset
uint64_t section_headers_offset() const
Offset of the sections table.
Definition ELF/Header.hpp:146
LIEF::ELF::Header::operator=
Header & operator=(const Header &)=default
LIEF::ELF::Header::identity_os_abi
void identity_os_abi(OS_ABI osabi)
Definition ELF/Header.hpp:294
LIEF::ELF::Header::has
bool has(PROCESSOR_FLAGS flag) const
LIEF::ELF::Header::object_file_version
void object_file_version(VERSION version)
Definition ELF/Header.hpp:237
LIEF::ELF::Header::header_size
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
LIEF::ELF::Header::identity_os_abi
OS_ABI identity_os_abi() const
Identifies the version of the ABI for which the object is prepared.
Definition ELF/Header.hpp:216
LIEF::ELF::Header::processor_flag
void processor_flag(uint32_t flags)
Definition ELF/Header.hpp:253
LIEF::ELF::Header::section_name_table_idx
uint32_t section_name_table_idx() const
Return the section's index which contains sections' names.
Definition ELF/Header.hpp:187
LIEF::ELF::Header::machine_type
ARCH machine_type() const
Target architecture.
Definition ELF/Header.hpp:126
LIEF::ELF::Header::numberof_sections
void numberof_sections(uint32_t n)
Definition ELF/Header.hpp:272
LIEF::ELF::Header::entrypoint
void entrypoint(uint64_t entry)
Definition ELF/Header.hpp:241
LIEF::ELF::Header::identity_abi_version
uint32_t identity_abi_version() const
ABI Version.
Definition ELF/Header.hpp:221
LIEF::ELF::Header::numberof_segments
void numberof_segments(uint32_t n)
Definition ELF/Header.hpp:265
LIEF::ELF::Header::identity_class
void identity_class(CLASS cls)
Definition ELF/Header.hpp:282
LIEF::ELF::Header::file_type
FILE_TYPE file_type() const
Define the object file type. (e.g. executable, library...)
Definition ELF/Header.hpp:121
LIEF::ELF::Header::program_header_size
void program_header_size(uint32_t size)
Definition ELF/Header.hpp:261
LIEF::ELF::Header::identity_version
void identity_version(VERSION version)
Definition ELF/Header.hpp:290
LIEF::ELF::Header::file_type
void file_type(FILE_TYPE type)
Definition ELF/Header.hpp:229
LIEF::ELF::Header::OS_ABI
OS_ABI
Match the result Elfxx_Ehdr.e_ident[EI_OSABI]
Definition ELF/Header.hpp:80
LIEF::ELF::Header::CLASS
CLASS
Match the result of Elfxx_Ehdr.e_ident[EI_CLASS]
Definition ELF/Header.hpp:73
LIEF::ELF::Header::section_name_table_idx
void section_name_table_idx(uint32_t idx)
Definition ELF/Header.hpp:275
LIEF::ELF::Header::object_file_version
VERSION object_file_version() const
Version of the object file format.
Definition ELF/Header.hpp:131
LIEF::ELF::Header::section_header_size
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
LIEF::ELF::Header::identity_abi_version
void identity_abi_version(uint8_t version)
Definition ELF/Header.hpp:298
LIEF::ELF::Parser
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
LIEF::ELF
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
LIEF::ELF::to_string
const char * to_string(DynamicEntry::TAG e)
LIEF::ELF::ARCH
ARCH
Definition ELF/enums.hpp:30
LIEF::ELF::ARCH::ARM
@ ARM
Definition ELF/enums.hpp:54
LIEF::ELF::ARCH::NONE
@ NONE
Definition ELF/enums.hpp:31
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42