LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
PE/Section.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_PE_SECTION_H
17#define LIEF_PE_SECTION_H
18#include <ostream>
19#include <vector>
20#include <string>
21#include <memory>
22
23#include "LIEF/iostream.hpp"
25#include "LIEF/visibility.h"
27#include "LIEF/enums.hpp"
28
29namespace LIEF {
30class SpanStream;
31
32namespace COFF {
33class String;
34}
35
36namespace PE {
37
38class Parser;
39class Builder;
40class Binary;
41
42namespace details {
43struct pe_section;
44}
45
48
49 friend class Parser;
50 friend class Builder;
51 friend class Binary;
52
53 public:
55 static constexpr size_t MAX_SECTION_NAME = 8;
56
57 enum class CHARACTERISTICS : uint64_t {
58 // clang-format off
59 TYPE_NO_PAD = 0x00000008,
60 CNT_CODE = 0x00000020,
61 CNT_INITIALIZED_DATA = 0x00000040,
62 CNT_UNINITIALIZED_DATA = 0x00000080,
63 LNK_OTHER = 0x00000100,
64 LNK_INFO = 0x00000200,
65 LNK_REMOVE = 0x00000800,
66 LNK_COMDAT = 0x00001000,
67 GPREL = 0x00008000,
68 MEM_PURGEABLE = 0x00010000,
69 MEM_16BIT = 0x00020000,
70 MEM_LOCKED = 0x00040000,
71 MEM_PRELOAD = 0x00080000,
72 ALIGN_1BYTES = 0x00100000,
73 ALIGN_2BYTES = 0x00200000,
74 ALIGN_4BYTES = 0x00300000,
75 ALIGN_8BYTES = 0x00400000,
76 ALIGN_16BYTES = 0x00500000,
77 ALIGN_32BYTES = 0x00600000,
78 ALIGN_64BYTES = 0x00700000,
79 ALIGN_128BYTES = 0x00800000,
80 ALIGN_256BYTES = 0x00900000,
81 ALIGN_512BYTES = 0x00A00000,
82 ALIGN_1024BYTES = 0x00B00000,
83 ALIGN_2048BYTES = 0x00C00000,
84 ALIGN_4096BYTES = 0x00D00000,
85 ALIGN_8192BYTES = 0x00E00000,
86 LNK_NRELOC_OVFL = 0x01000000,
87 MEM_DISCARDABLE = 0x02000000,
88 MEM_NOT_CACHED = 0x04000000,
89 MEM_NOT_PAGED = 0x08000000,
90 MEM_SHARED = 0x10000000,
91 MEM_EXECUTE = 0x20000000,
92 MEM_READ = 0x40000000,
93 MEM_WRITE = 0x80000000,
94 // clang-format on
95 };
96
97 Section(const details::pe_section& header);
98 Section() = default;
99 Section(std::string name) :
100 Section::Section() {
101 name_ = std::move(name);
102 }
103
104 Section(std::string name, std::vector<uint8_t> content) :
105 Section(std::move(name)) {
106 content_ = std::move(content);
107 size_ = content_.size();
108 }
109
110 Section& operator=(const Section&) = default;
111 Section(const Section&) = default;
112 ~Section() override = default;
113
115 uint32_t sizeof_raw_data() const;
116
120 uint32_t virtual_size() const {
121 return virtual_size_;
122 }
123
126 return content_;
127 }
128
131 return padding_;
132 }
133
135 uint32_t pointerto_raw_data() const;
136
143 uint32_t pointerto_relocation() const {
144 return pointer_to_relocations_;
145 }
146
151 uint32_t pointerto_line_numbers() const {
152 return pointer_to_linenumbers_;
153 }
154
156 uint16_t numberof_relocations() const {
157 return number_of_relocations_;
158 }
159
161 uint16_t numberof_line_numbers() const {
162 return number_of_linenumbers_;
163 }
164
168 uint32_t characteristics() const {
169 return characteristics_;
170 }
171
174 return (characteristics() & (uint32_t)c) > 0;
175 }
176
178 std::vector<CHARACTERISTICS> characteristics_list() const {
179 return characteristics_to_list(characteristics_);
180 }
181
188
190 void clear(uint8_t c);
191 void content(const std::vector<uint8_t>& data) override;
192
193 void name(std::string name) override;
194
195 void virtual_size(uint32_t virtual_sz) {
196 virtual_size_ = virtual_sz;
197 }
198
199 void pointerto_raw_data(uint32_t ptr) {
200 offset(ptr);
201 }
202
203 void pointerto_relocation(uint32_t ptr) {
204 pointer_to_relocations_ = ptr;
205 }
206
207 void pointerto_line_numbers(uint32_t ptr) {
208 pointer_to_linenumbers_ = ptr;
209 }
210
211 void numberof_relocations(uint16_t nb) {
212 number_of_relocations_ = nb;
213 }
214
215 void numberof_line_numbers(uint16_t nb) {
216 number_of_linenumbers_ = nb;
217 }
218
219 void sizeof_raw_data(uint32_t size) {
220 this->size(size);
221 }
222
224 characteristics_ = characteristics;
225 }
226
232 return coff_string_;
233 }
234
236 return coff_string_;
237 }
238
239 Section&
241 characteristics_ &= ~static_cast<size_t>(characteristic);
242 return *this;
243 }
244
246 characteristics_ |= static_cast<size_t>(characteristic);
247 return *this;
248 }
249
250 std::unique_ptr<SpanStream> stream() const LIEF_LIFETIMEBOUND;
251
253 LIEF_LOCAL Section& reserve(size_t size, uint8_t value = 0) LIEF_LIFETIMEBOUND {
254 content_.resize(size, value);
255 return *this;
256 }
257
260 return content_;
261 }
262
264 return content_;
265 }
266
267 void accept(Visitor& visitor) const override;
268
269 LIEF_API friend std::ostream& operator<<(std::ostream& os,
270 const Section& section);
271
272 static std::vector<CHARACTERISTICS> characteristics_to_list(uint32_t value);
273
274 private:
275 std::vector<uint8_t> content_;
276 std::vector<uint8_t> padding_;
277 uint32_t virtual_size_ = 0;
278 uint32_t pointer_to_relocations_ = 0;
279 uint32_t pointer_to_linenumbers_ = 0;
280 uint16_t number_of_relocations_ = 0;
281 uint16_t number_of_linenumbers_ = 0;
282 uint32_t characteristics_ = 0;
283
284 COFF::String* coff_string_ = nullptr;
285};
286
288
289} // namespace PE
290} // namespace LIEF
291
293
294#endif
This class represents a string located in the COFF string table.
Definition String.hpp:34
Class which represents a PE binary This is the main interface to manage and modify a PE executable.
Definition PE/Binary.hpp:57
Class that is used to rebuild a raw PE binary from a PE::Binary object.
Definition PE/Builder.hpp:44
Main interface to parse PE binaries. In particular, the static Parser::parse functions should be used...
Definition PE/Parser.hpp:53
Class which represents a PE section.
Definition PE/Section.hpp:47
Section(const Section &)=default
uint32_t pointerto_line_numbers() const
The file pointer to the beginning of line-number entries for the section. This is set to zero if ther...
Definition PE/Section.hpp:151
void sizeof_raw_data(uint32_t size)
Definition PE/Section.hpp:219
Section(std::string name)
Definition PE/Section.hpp:99
uint32_t pointerto_relocation() const
The file pointer to the beginning of the COFF relocation entries for the section. This is set to zero...
Definition PE/Section.hpp:143
Section & remove_characteristic(CHARACTERISTICS characteristic)
Definition PE/Section.hpp:240
static std::vector< CHARACTERISTICS > characteristics_to_list(uint32_t value)
friend std::ostream & operator<<(std::ostream &os, const Section &section)
Section & operator=(const Section &)=default
bool has_characteristic(CHARACTERISTICS c) const
Check if the section has the given CHARACTERISTICS.
Definition PE/Section.hpp:173
uint16_t numberof_line_numbers() const
No longer used in recent PE binaries produced by Visual Studio.
Definition PE/Section.hpp:161
bool is_discardable() const
True if the section can be discarded as needed.
Definition PE/Section.hpp:185
~Section() override=default
CHARACTERISTICS
Definition PE/Section.hpp:57
@ MEM_DISCARDABLE
Definition PE/Section.hpp:87
const COFF::String * coff_string() const
Definition PE/Section.hpp:235
friend class Builder
Definition PE/Section.hpp:50
span< const uint8_t > content() const override
The actual content of the section.
Definition PE/Section.hpp:125
void clear(uint8_t c)
Fill the content of the section with the given char.
void pointerto_raw_data(uint32_t ptr)
Definition PE/Section.hpp:199
std::vector< CHARACTERISTICS > characteristics_list() const
List of the section characteristics.
Definition PE/Section.hpp:178
uint32_t characteristics() const
Characteristics of the section: it provides information about the permissions of the section when map...
Definition PE/Section.hpp:168
void name(std::string name) override
Change the section's name.
static constexpr size_t MAX_SECTION_NAME
Definition PE/Section.hpp:55
friend class Binary
Definition PE/Section.hpp:51
void pointerto_relocation(uint32_t ptr)
Definition PE/Section.hpp:203
uint32_t sizeof_raw_data() const
Return the size of the data in the section.
span< const uint8_t > padding() const
Content of the section's padding area.
Definition PE/Section.hpp:130
void pointerto_line_numbers(uint32_t ptr)
Definition PE/Section.hpp:207
std::unique_ptr< SpanStream > stream() const
span< uint8_t > writable_content()
Definition PE/Section.hpp:263
friend class Parser
Definition PE/Section.hpp:49
void characteristics(uint32_t characteristics)
Definition PE/Section.hpp:223
void virtual_size(uint32_t virtual_sz)
Definition PE/Section.hpp:195
void content(const std::vector< uint8_t > &data) override
Change section content.
uint32_t pointerto_raw_data() const
The offset of the section data in the PE file.
void numberof_line_numbers(uint16_t nb)
Definition PE/Section.hpp:215
Section(const details::pe_section &header)
uint16_t numberof_relocations() const
No longer used in recent PE binaries produced by Visual Studio.
Definition PE/Section.hpp:156
COFF::String * coff_string()
Return the COFF string associated with the section's name (or a nullptr).
Definition PE/Section.hpp:231
void accept(Visitor &visitor) const override
Section(std::string name, std::vector< uint8_t > content)
Definition PE/Section.hpp:104
uint32_t virtual_size() const
Return the size of the data when mapped in memory.
Definition PE/Section.hpp:120
Section & add_characteristic(CHARACTERISTICS characteristic)
Definition PE/Section.hpp:245
void numberof_relocations(uint16_t nb)
Definition PE/Section.hpp:211
Class which represents an abstracted section.
Definition Abstract/Section.hpp:30
virtual void size(uint64_t size)
Change the section size.
Definition Abstract/Section.hpp:60
virtual std::string name() const
Section's name.
Definition Abstract/Section.hpp:44
virtual uint64_t offset() const
Offset in the binary.
Definition Abstract/Section.hpp:70
Definition SpanStream.hpp:32
Definition Visitor.hpp:212
Definition iostream.hpp:32
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
Definition AuxiliarySymbol.hpp:30
Definition DataDirectory.hpp:37
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
const char * to_string(CODE_PAGES e)
LIEF namespace.
Definition Abstract/Binary.hpp:41
tcb::span< ElementType, Extent > span
Definition span.hpp:22
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46