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