LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
COFF/Header.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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 {
29class LIEF_API Header {
34 public:
35
36 enum class KIND {
37 UNKNOWN = 0,
38 REGULAR,
39 BIGOBJ
40 };
41 using MACHINE_TYPES = LIEF::PE::Header::MACHINE_TYPES;
44 static std::unique_ptr<Header> create(BinaryStream& stream);
47 static std::unique_ptr<Header> create(BinaryStream& stream, KIND kind);
48
49 Header() = default;
50 Header(KIND kind) :
51 kind_(kind)
52 {}
53
54 Header& operator=(const Header&) = default;
55 Header(const Header&) = default;
56
57 Header& operator=(Header&&) = default;
58 Header(Header&&) = default;
59
60 virtual std::unique_ptr<Header> clone() const = 0;
61 KIND kind() const {
65 return kind_;
66 }
67 MACHINE_TYPES machine() const {
70 return machine_;
71 }
72 uint32_t nb_sections() const {
75 return nb_sections_;
76 }
77 uint32_t pointerto_symbol_table() const {
80 return pointerto_symbol_table_;
81 }
82 uint32_t nb_symbols() const {
85 return nb_symbols_;
86 }
87 uint32_t timedatestamp() const {
90 return timedatestamp_;
91 }
92
93 void machine(MACHINE_TYPES machine) {
94 machine_ = machine;
95 }
96
97 void nb_sections(uint32_t value) {
98 nb_sections_ = value;
99 }
100
101 void pointerto_symbol_table(uint32_t value) {
102 pointerto_symbol_table_ = value;
103 }
104
105 void nb_symbols(uint32_t value) {
106 nb_symbols_ = value;
107 }
108
109 void timedatestamp(uint32_t value) {
110 timedatestamp_ = value;
111 }
112
113 virtual std::string to_string() const;
114
115 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& hdr) {
116 os << hdr.to_string();
117 return os;
118 }
119
120 template<class T>
121 const T* as() const {
122 static_assert(std::is_base_of<Header, T>::value,
123 "Require Header inheritance");
124 if (T::classof(this)) {
125 return static_cast<const T*>(this);
126 }
127 return nullptr;
128 }
129
130 virtual ~Header() = default;
131
132 protected:
133 KIND kind_ = KIND::UNKNOWN;
134 MACHINE_TYPES machine_ = MACHINE_TYPES::UNKNOWN;
135 uint32_t nb_sections_ = 0;
136 uint32_t pointerto_symbol_table_ = 0;
137 uint32_t nb_symbols_ = 0;
138 uint32_t timedatestamp_ = 0;
139};
140
141LIEF_API inline const char* to_string(Header::KIND kind) {
142 switch (kind) {
143 case Header::KIND::UNKNOWN: return "UNKNOWN";
144 case Header::KIND::REGULAR: return "REGULAR";
145 case Header::KIND::BIGOBJ: return "BIGOBJ";
146 }
147 return "UNKNOWN";
148}
149
150LIEF_API inline const char* to_string(Header::MACHINE_TYPES machine) {
151 return LIEF::PE::to_string(machine);
152}
153}
154}
155#endif
Header.hpp
LIEF::BinaryStream
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
LIEF::COFF::Header
Class that represents the COFF header. It is subclassed by LIEF::COFF::RegularHeader and LIEF::COFF::...
Definition COFF/Header.hpp:33
LIEF::COFF::Header::create
static std::unique_ptr< Header > create(BinaryStream &stream, KIND kind)
LIEF::COFF::Header::nb_sections
void nb_sections(uint32_t value)
Definition COFF/Header.hpp:97
LIEF::COFF::Header::pointerto_symbol_table
uint32_t pointerto_symbol_table() const
Offset of the symbols table.
Definition COFF/Header.hpp:79
LIEF::COFF::Header::as
const T * as() const
Definition COFF/Header.hpp:121
LIEF::COFF::Header::operator<<
friend std::ostream & operator<<(std::ostream &os, const Header &hdr)
Definition COFF/Header.hpp:115
LIEF::COFF::Header::nb_sections
uint32_t nb_sections() const
The number of sections.
Definition COFF/Header.hpp:74
LIEF::COFF::Header::Header
Header()=default
LIEF::COFF::Header::Header
Header(KIND kind)
Definition COFF/Header.hpp:50
LIEF::COFF::Header::operator=
Header & operator=(const Header &)=default
LIEF::COFF::Header::machine
void machine(MACHINE_TYPES machine)
Definition COFF/Header.hpp:93
LIEF::COFF::Header::Header
Header(const Header &)=default
LIEF::COFF::Header::Header
Header(Header &&)=default
LIEF::COFF::Header::to_string
virtual std::string to_string() const
LIEF::COFF::Header::kind
KIND kind() const
The type of this header: whether it is regular or using the /bigobj format.
Definition COFF/Header.hpp:64
LIEF::COFF::Header::timedatestamp
uint32_t timedatestamp() const
Timestamp when the COFF has been generated.
Definition COFF/Header.hpp:89
LIEF::COFF::Header::timedatestamp
void timedatestamp(uint32_t value)
Definition COFF/Header.hpp:109
LIEF::COFF::Header::nb_symbols
uint32_t nb_symbols() const
Number of symbols (including auxiliary symbols)
Definition COFF/Header.hpp:84
LIEF::COFF::Header::nb_symbols
void nb_symbols(uint32_t value)
Definition COFF/Header.hpp:105
LIEF::COFF::Header::KIND
KIND
Definition COFF/Header.hpp:36
LIEF::COFF::Header::KIND::UNKNOWN
@ UNKNOWN
Definition COFF/Header.hpp:37
LIEF::COFF::Header::KIND::REGULAR
@ REGULAR
Definition COFF/Header.hpp:38
LIEF::COFF::Header::KIND::BIGOBJ
@ BIGOBJ
Definition COFF/Header.hpp:39
LIEF::COFF::Header::~Header
virtual ~Header()=default
LIEF::COFF::Header::MACHINE_TYPES
LIEF::PE::Header::MACHINE_TYPES MACHINE_TYPES
The different architectures (mirrored from PE)
Definition COFF/Header.hpp:43
LIEF::COFF::Header::pointerto_symbol_table
void pointerto_symbol_table(uint32_t value)
Definition COFF/Header.hpp:101
LIEF::COFF::Header::operator=
Header & operator=(Header &&)=default
LIEF::COFF::Header::create
static std::unique_ptr< Header > create(BinaryStream &stream)
Create a header from the given stream.
LIEF::COFF::Header::machine
MACHINE_TYPES machine() const
The machine type targeted by this COFF.
Definition COFF/Header.hpp:69
LIEF::COFF::Header::clone
virtual std::unique_ptr< Header > clone() const =0
LIEF::PE::Header::MACHINE_TYPES
MACHINE_TYPES
Definition PE/Header.hpp:40
LIEF::COFF
Definition AuxiliarySymbol.hpp:29
LIEF::COFF::to_string
const char * to_string(AuxiliarySectionDefinition::COMDAT_SELECTION e)
LIEF::PE::to_string
const char * to_string(CODE_PAGES e)
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:39
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41