LIEF: Library to Instrument Executable Formats Version 0.16.6
Loading...
Searching...
No Matches
Abstract/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_ABSTRACT_HEADER_H
17#define LIEF_ABSTRACT_HEADER_H
18
19#include <ostream>
20#include <cstdint>
21#include <vector>
22
23#include "LIEF/Object.hpp"
24#include "LIEF/visibility.h"
25#include "LIEF/enums.hpp"
26
27namespace LIEF {
28namespace ELF {
29class Binary;
30}
31
32namespace PE {
33class Binary;
34}
35
36namespace MachO {
37class Binary;
38}
39
40class LIEF_API Header : public Object {
41 public:
42 enum class ARCHITECTURES {
43 UNKNOWN = 0,
44 ARM,
45 ARM64,
46 MIPS,
47 X86,
48 X86_64,
49 PPC,
50 SPARC,
51 SYSZ,
52 XCORE,
53 RISCV,
54 LOONGARCH,
55 PPC64,
56 };
57
58 enum class ENDIANNESS {
59 UNKNOWN = 0,
60 BIG,
61 LITTLE,
62 };
63
64 enum MODES : uint64_t {
65 NONE = 0,
66
67 BITS_16 = 1LLU << 0,
68 BITS_32 = 1LLU << 1,
69 BITS_64 = 1LLU << 2,
70 THUMB = 1LLU << 3,
71
72 ARM64E = 1LLU << 4,
73 };
74
75 enum class OBJECT_TYPES {
76 UNKNOWN = 0,
77 EXECUTABLE,
78 LIBRARY,
79 OBJECT,
80 };
81
82 static Header from(const LIEF::ELF::Binary& elf);
83 static Header from(const LIEF::PE::Binary& pe);
84 static Header from(const LIEF::MachO::Binary& macho);
85
86 Header() = default;
87 Header(const Header&) = default;
88 Header& operator=(const Header&) = default;
89 ~Header() override = default;
90 ARCHITECTURES architecture() const {
93 return architecture_;
94 }
95 MODES modes() const {
98 return modes_;
99 }
100 std::vector<MODES> modes_list() const;
103
104 bool is(MODES m) const {
105 return ((uint64_t)m & (uint64_t)modes_) != 0;
106 }
107
108 OBJECT_TYPES object_type() const {
109 return object_type_;
110 }
111 uint64_t entrypoint() const {
112 return entrypoint_;
113 }
114
115 ENDIANNESS endianness() const {
116 return endianness_;
117 }
118
119 bool is_32() const {
120 return ((uint64_t)modes_ & (uint64_t)MODES::BITS_32) != 0;
121 }
122
123 bool is_64() const {
124 return ((uint64_t)modes_ & (uint64_t)MODES::BITS_64) != 0;
125 }
126
127 void accept(Visitor& visitor) const override;
128
129 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& hdr);
130
131 protected:
132 ARCHITECTURES architecture_ = ARCHITECTURES::UNKNOWN;
133 OBJECT_TYPES object_type_ = OBJECT_TYPES::UNKNOWN;
134 uint64_t entrypoint_ = 0;
135 ENDIANNESS endianness_ = ENDIANNESS::UNKNOWN;
136 MODES modes_ = MODES::NONE;
137};
138
139LIEF_API const char* to_string(Header::ARCHITECTURES e);
140LIEF_API const char* to_string(Header::OBJECT_TYPES e);
141LIEF_API const char* to_string(Header::MODES e);
142LIEF_API const char* to_string(Header::ENDIANNESS e);
143}
144
145ENABLE_BITMASK_OPERATORS(LIEF::Header::MODES)
146
147#endif
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
Definition Abstract/Header.hpp:40
uint64_t entrypoint() const
Definition Abstract/Header.hpp:111
ARCHITECTURES architecture() const
Target architecture.
Definition Abstract/Header.hpp:92
static Header from(const LIEF::ELF::Binary &elf)
std::vector< MODES > modes_list() const
MODES as a vector.
MODES modes() const
Optional features for the given architecture.
Definition Abstract/Header.hpp:97
Header()=default
static Header from(const LIEF::MachO::Binary &macho)
Header(const Header &)=default
OBJECT_TYPES object_type() const
Definition Abstract/Header.hpp:108
ARCHITECTURES
Definition Abstract/Header.hpp:42
OBJECT_TYPES
Definition Abstract/Header.hpp:75
friend std::ostream & operator<<(std::ostream &os, const Header &hdr)
bool is(MODES m) const
Definition Abstract/Header.hpp:104
ENDIANNESS endianness() const
Definition Abstract/Header.hpp:115
ENDIANNESS
Definition Abstract/Header.hpp:58
Header & operator=(const Header &)=default
void accept(Visitor &visitor) const override
static Header from(const LIEF::PE::Binary &pe)
~Header() override=default
bool is_32() const
Definition Abstract/Header.hpp:119
MODES
Definition Abstract/Header.hpp:64
bool is_64() const
Definition Abstract/Header.hpp:123
Class which represents a MachO binary.
Definition MachO/Binary.hpp:85
Definition Object.hpp:25
Class which represents a PE binary This is the main interface to manage and modify a PE executable.
Definition PE/Binary.hpp:52
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
LIEF namespace.
Definition Abstract/Binary.hpp:36
const char * to_string(Binary::VA_TYPES e)
#define LIEF_API
Definition visibility.h:41