LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
COFF/Header.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_COFF_HEADER_H
16#define LIEF_COFF_HEADER_H
17#include <cstdint>
18#include <memory>
19#include <ostream>
20
21#include "LIEF/PE/Header.hpp"
22#include "LIEF/visibility.h"
23
24namespace LIEF {
25class BinaryStream;
26
27namespace COFF {
28
33 public:
34 enum class KIND {
35 UNKNOWN = 0,
36 REGULAR,
37 BIGOBJ,
38 };
39
42
44 static std::unique_ptr<Header> create(BinaryStream& stream);
45 static std::unique_ptr<Header> create(BinaryStream& stream, KIND kind);
46
47 Header() = default;
49 kind_(kind) {}
50
51 Header& operator=(const Header&) = default;
52 Header(const Header&) = default;
53
54 Header& operator=(Header&&) = default;
55 Header(Header&&) = default;
56
57 virtual std::unique_ptr<Header> clone() const = 0;
58
61 KIND kind() const {
62 return kind_;
63 }
64
67 return machine_;
68 }
69
71 uint32_t nb_sections() const {
72 return nb_sections_;
73 }
74
76 uint32_t pointerto_symbol_table() const {
77 return pointerto_symbol_table_;
78 }
79
81 uint32_t nb_symbols() const {
82 return nb_symbols_;
83 }
84
86 uint32_t timedatestamp() const {
87 return timedatestamp_;
88 }
89
91 machine_ = machine;
92 }
93
94 void nb_sections(uint32_t value) {
95 nb_sections_ = value;
96 }
97
98 void pointerto_symbol_table(uint32_t value) {
99 pointerto_symbol_table_ = value;
100 }
101
102 void nb_symbols(uint32_t value) {
103 nb_symbols_ = value;
104 }
105
106 void timedatestamp(uint32_t value) {
107 timedatestamp_ = value;
108 }
109
110 virtual std::string to_string() const;
111
112 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& hdr) {
113 os << hdr.to_string();
114 return os;
115 }
116
117 template<class T>
118 const T* as() const {
119 static_assert(std::is_base_of_v<Header, T>, "Require Header inheritance");
120 if (T::classof(this)) {
121 return static_cast<const T*>(this);
122 }
123 return nullptr;
124 }
125
126 virtual ~Header() = default;
127
128 protected:
129 KIND kind_ = KIND::UNKNOWN;
130 MACHINE_TYPES machine_ = MACHINE_TYPES::UNKNOWN;
131 uint32_t nb_sections_ = 0;
132 uint32_t pointerto_symbol_table_ = 0;
133 uint32_t nb_symbols_ = 0;
134 uint32_t timedatestamp_ = 0;
135};
136
137LIEF_API inline const char* to_string(Header::KIND kind) {
138 switch (kind) {
139 case Header::KIND::UNKNOWN: return "UNKNOWN";
140 case Header::KIND::REGULAR: return "REGULAR";
141 case Header::KIND::BIGOBJ: return "BIGOBJ";
142 }
143 return "UNKNOWN";
144}
145
146LIEF_API inline const char* to_string(Header::MACHINE_TYPES machine) {
147 return LIEF::PE::to_string(machine);
148}
149}
150}
151#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:34
static std::unique_ptr< Header > create(BinaryStream &stream, KIND kind)
void nb_sections(uint32_t value)
Definition COFF/Header.hpp:94
uint32_t pointerto_symbol_table() const
Offset of the symbols table.
Definition COFF/Header.hpp:76
const T * as() const
Definition COFF/Header.hpp:118
friend std::ostream & operator<<(std::ostream &os, const Header &hdr)
Definition COFF/Header.hpp:112
uint32_t nb_sections() const
The number of sections.
Definition COFF/Header.hpp:71
Header(KIND kind)
Definition COFF/Header.hpp:48
Header & operator=(const Header &)=default
void machine(MACHINE_TYPES machine)
Definition COFF/Header.hpp:90
Header(const Header &)=default
Header(Header &&)=default
virtual std::string to_string() const
KIND kind() const
The type of this header: whether it is regular or using the /bigobj format.
Definition COFF/Header.hpp:61
uint32_t timedatestamp() const
Timestamp when the COFF has been generated.
Definition COFF/Header.hpp:86
void timedatestamp(uint32_t value)
Definition COFF/Header.hpp:106
uint32_t nb_symbols() const
Number of symbols (including auxiliary symbols).
Definition COFF/Header.hpp:81
void nb_symbols(uint32_t value)
Definition COFF/Header.hpp:102
KIND
Definition COFF/Header.hpp:34
@ UNKNOWN
Definition COFF/Header.hpp:35
@ REGULAR
Definition COFF/Header.hpp:36
@ BIGOBJ
Definition COFF/Header.hpp:37
virtual ~Header()=default
LIEF::PE::Header::MACHINE_TYPES MACHINE_TYPES
The different architectures (mirrored from PE).
Definition COFF/Header.hpp:41
void pointerto_symbol_table(uint32_t value)
Definition COFF/Header.hpp:98
Header & operator=(Header &&)=default
static std::unique_ptr< Header > create(BinaryStream &stream)
Create a header from the given stream.
MACHINE_TYPES machine() const
The machine type targeted by this COFF.
Definition COFF/Header.hpp:66
virtual std::unique_ptr< Header > clone() const =0
MACHINE_TYPES
Definition PE/Header.hpp:44
Definition AuxiliarySymbol.hpp:29
const char * to_string(AuxiliarySectionDefinition::COMDAT_SELECTION e)
const char * to_string(CODE_PAGES e)
LIEF namespace.
Definition Abstract/Binary.hpp:41
#define LIEF_API
Definition visibility.h:44