LIEF: Library to Instrument Executable Formats Version 0.17.0
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 };
56
57 enum class ENDIANNESS {
58 UNKNOWN = 0,
59 BIG,
60 LITTLE,
61 };
62
63 enum MODES : uint64_t {
64 NONE = 0,
65
66 BITS_16 = 1LLU << 0,
67 BITS_32 = 1LLU << 1,
68 BITS_64 = 1LLU << 2,
69 THUMB = 1LLU << 3,
70
71 ARM64E = 1LLU << 4,
72 };
73
74 enum class OBJECT_TYPES {
75 UNKNOWN = 0,
76 EXECUTABLE,
77 LIBRARY,
78 OBJECT,
79 };
80
81 static Header from(const LIEF::ELF::Binary& elf);
82 static Header from(const LIEF::PE::Binary& pe);
83 static Header from(const LIEF::MachO::Binary& macho);
84
85 Header() = default;
86 Header(const Header&) = default;
87 Header& operator=(const Header&) = default;
88 ~Header() override = default;
89 ARCHITECTURES architecture() const {
92 return architecture_;
93 }
94 MODES modes() const {
97 return modes_;
98 }
99 std::vector<MODES> modes_list() const;
102
103 bool is(MODES m) const {
104 return ((uint64_t)m & (uint64_t)modes_) != 0;
105 }
106
107 OBJECT_TYPES object_type() const {
108 return object_type_;
109 }
110 uint64_t entrypoint() const {
111 return entrypoint_;
112 }
113
114 ENDIANNESS endianness() const {
115 return endianness_;
116 }
117
118 bool is_32() const {
119 return ((uint64_t)modes_ & (uint64_t)MODES::BITS_32) != 0;
120 }
121
122 bool is_64() const {
123 return ((uint64_t)modes_ & (uint64_t)MODES::BITS_64) != 0;
124 }
125
126 void accept(Visitor& visitor) const override;
127
128 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& hdr);
129
130 protected:
131 ARCHITECTURES architecture_ = ARCHITECTURES::UNKNOWN;
132 OBJECT_TYPES object_type_ = OBJECT_TYPES::UNKNOWN;
133 uint64_t entrypoint_ = 0;
134 ENDIANNESS endianness_ = ENDIANNESS::UNKNOWN;
135 MODES modes_ = MODES::NONE;
136};
137
138LIEF_API const char* to_string(Header::ARCHITECTURES e);
139LIEF_API const char* to_string(Header::OBJECT_TYPES e);
140LIEF_API const char* to_string(Header::MODES e);
141LIEF_API const char* to_string(Header::ENDIANNESS e);
142}
143
144ENABLE_BITMASK_OPERATORS(LIEF::Header::MODES)
145
146#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:110
ARCHITECTURES architecture() const
Target architecture.
Definition Abstract/Header.hpp:91
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:96
Header()=default
static Header from(const LIEF::MachO::Binary &macho)
Header(const Header &)=default
OBJECT_TYPES object_type() const
Definition Abstract/Header.hpp:107
ARCHITECTURES
Definition Abstract/Header.hpp:42
OBJECT_TYPES
Definition Abstract/Header.hpp:74
friend std::ostream & operator<<(std::ostream &os, const Header &hdr)
bool is(MODES m) const
Definition Abstract/Header.hpp:103
ENDIANNESS endianness() const
Definition Abstract/Header.hpp:114
ENDIANNESS
Definition Abstract/Header.hpp:57
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:118
MODES
Definition Abstract/Header.hpp:63
bool is_64() const
Definition Abstract/Header.hpp:122
Class which represents a MachO binary.
Definition MachO/Binary.hpp:86
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