LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
COFF/Header.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 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_COFF_HEADER_H
17#define LIEF_COFF_HEADER_H
18#include <cstdint>
19#include <memory>
20#include <ostream>
21
22#include "LIEF/visibility.h"
23#include "LIEF/PE/Header.hpp"
24
25namespace LIEF {
26class BinaryStream;
27
28namespace COFF {
29
34 public:
35 enum class KIND {
36 UNKNOWN = 0,
37 REGULAR,
38 BIGOBJ,
39 };
40
43
45 static std::unique_ptr<Header> create(BinaryStream& stream);
46 static std::unique_ptr<Header> create(BinaryStream& stream, KIND kind);
47
48 Header() = default;
50 kind_(kind) {}
51
52 Header& operator=(const Header&) = default;
53 Header(const Header&) = default;
54
55 Header& operator=(Header&&) = default;
56 Header(Header&&) = default;
57
58 virtual std::unique_ptr<Header> clone() const = 0;
59
62 KIND kind() const {
63 return kind_;
64 }
65
68 return machine_;
69 }
70
72 uint32_t nb_sections() const {
73 return nb_sections_;
74 }
75
77 uint32_t pointerto_symbol_table() const {
78 return pointerto_symbol_table_;
79 }
80
82 uint32_t nb_symbols() const {
83 return nb_symbols_;
84 }
85
87 uint32_t timedatestamp() const {
88 return timedatestamp_;
89 }
90
92 machine_ = machine;
93 }
94
95 void nb_sections(uint32_t value) {
96 nb_sections_ = value;
97 }
98
99 void pointerto_symbol_table(uint32_t value) {
100 pointerto_symbol_table_ = value;
101 }
102
103 void nb_symbols(uint32_t value) {
104 nb_symbols_ = value;
105 }
106
107 void timedatestamp(uint32_t value) {
108 timedatestamp_ = value;
109 }
110
111 virtual std::string to_string() const;
112
113 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& hdr) {
114 os << hdr.to_string();
115 return os;
116 }
117
118 template<class T>
119 const T* as() const {
120 static_assert(std::is_base_of<Header, T>::value, "Require Header inheritance");
121 if (T::classof(this)) {
122 return static_cast<const T*>(this);
123 }
124 return nullptr;
125 }
126
127 virtual ~Header() = default;
128
129 protected:
130 KIND kind_ = KIND::UNKNOWN;
131 MACHINE_TYPES machine_ = MACHINE_TYPES::UNKNOWN;
132 uint32_t nb_sections_ = 0;
133 uint32_t pointerto_symbol_table_ = 0;
134 uint32_t nb_symbols_ = 0;
135 uint32_t timedatestamp_ = 0;
136};
137
138LIEF_API inline const char* to_string(Header::KIND kind) {
139 switch (kind) {
140 case Header::KIND::UNKNOWN: return "UNKNOWN";
141 case Header::KIND::REGULAR: return "REGULAR";
142 case Header::KIND::BIGOBJ: return "BIGOBJ";
143 }
144 return "UNKNOWN";
145}
146
147LIEF_API inline const char* to_string(Header::MACHINE_TYPES machine) {
148 return LIEF::PE::to_string(machine);
149}
150}
151}
152#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
static std::unique_ptr< Header > create(BinaryStream &stream, KIND kind)
void nb_sections(uint32_t value)
Definition COFF/Header.hpp:95
uint32_t pointerto_symbol_table() const
Offset of the symbols table.
Definition COFF/Header.hpp:77
const T * as() const
Definition COFF/Header.hpp:119
friend std::ostream & operator<<(std::ostream &os, const Header &hdr)
Definition COFF/Header.hpp:113
uint32_t nb_sections() const
The number of sections.
Definition COFF/Header.hpp:72
Header(KIND kind)
Definition COFF/Header.hpp:49
Header & operator=(const Header &)=default
void machine(MACHINE_TYPES machine)
Definition COFF/Header.hpp:91
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:62
uint32_t timedatestamp() const
Timestamp when the COFF has been generated.
Definition COFF/Header.hpp:87
void timedatestamp(uint32_t value)
Definition COFF/Header.hpp:107
uint32_t nb_symbols() const
Number of symbols (including auxiliary symbols).
Definition COFF/Header.hpp:82
void nb_symbols(uint32_t value)
Definition COFF/Header.hpp:103
KIND
Definition COFF/Header.hpp:35
@ UNKNOWN
Definition COFF/Header.hpp:36
@ REGULAR
Definition COFF/Header.hpp:37
@ BIGOBJ
Definition COFF/Header.hpp:38
virtual ~Header()=default
LIEF::PE::Header::MACHINE_TYPES MACHINE_TYPES
The different architectures (mirrored from PE).
Definition COFF/Header.hpp:42
void pointerto_symbol_table(uint32_t value)
Definition COFF/Header.hpp:99
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:67
virtual std::unique_ptr< Header > clone() const =0
MACHINE_TYPES
Definition PE/Header.hpp:40
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:40
#define LIEF_API
Definition visibility.h:43