LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
PE/Section.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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 <set>
22#include <memory>
23
24#include "LIEF/visibility.h"
26#include "LIEF/enums.hpp"
27#include "LIEF/PE/enums.hpp"
28
29namespace LIEF {
30class SpanStream;
31
32namespace PE {
33
34class Parser;
35class Builder;
36class Binary;
37
38namespace details {
39struct pe_section;
40}
41class LIEF_API Section : public LIEF::Section {
44
45 friend class Parser;
46 friend class Builder;
47 friend class Binary;
48
49 public:
51 static constexpr size_t MAX_SECTION_NAME = 8;
52
53 enum class CHARACTERISTICS: uint64_t {
54 TYPE_NO_PAD = 0x00000008,
55 CNT_CODE = 0x00000020,
56 CNT_INITIALIZED_DATA = 0x00000040,
57 CNT_UNINITIALIZED_DATA = 0x00000080,
58 LNK_OTHER = 0x00000100,
59 LNK_INFO = 0x00000200,
60 LNK_REMOVE = 0x00000800,
61 LNK_COMDAT = 0x00001000,
62 GPREL = 0x00008000,
63 MEM_PURGEABLE = 0x00010000,
64 MEM_16BIT = 0x00020000,
65 MEM_LOCKED = 0x00040000,
66 MEM_PRELOAD = 0x00080000,
67 ALIGN_1BYTES = 0x00100000,
68 ALIGN_2BYTES = 0x00200000,
69 ALIGN_4BYTES = 0x00300000,
70 ALIGN_8BYTES = 0x00400000,
71 ALIGN_16BYTES = 0x00500000,
72 ALIGN_32BYTES = 0x00600000,
73 ALIGN_64BYTES = 0x00700000,
74 ALIGN_128BYTES = 0x00800000,
75 ALIGN_256BYTES = 0x00900000,
76 ALIGN_512BYTES = 0x00A00000,
77 ALIGN_1024BYTES = 0x00B00000,
78 ALIGN_2048BYTES = 0x00C00000,
79 ALIGN_4096BYTES = 0x00D00000,
80 ALIGN_8192BYTES = 0x00E00000,
81 LNK_NRELOC_OVFL = 0x01000000,
82 MEM_DISCARDABLE = 0x02000000,
83 MEM_NOT_CACHED = 0x04000000,
84 MEM_NOT_PAGED = 0x08000000,
85 MEM_SHARED = 0x10000000,
86 MEM_EXECUTE = 0x20000000,
87 MEM_READ = 0x40000000,
88 MEM_WRITE = 0x80000000
89 };
90
91 Section(const details::pe_section& header);
92 Section() = default;
93 Section(const std::vector<uint8_t>& data,
94 const std::string& name = "", uint32_t characteristics = 0);
95 Section(const std::string& name);
96
97 Section& operator=(const Section&) = default;
98 Section(const Section&) = default;
99 ~Section() override = default;
100 uint32_t sizeof_raw_data() const;
103 uint32_t virtual_size() const {
108 return virtual_size_;
109 }
110 span<const uint8_t> content() const override {
113 return content_;
114 }
115 span<const uint8_t> padding() const {
118 return padding_;
119 }
120 uint32_t pointerto_raw_data() const;
123 uint32_t pointerto_relocation() const {
130 return pointer_to_relocations_;
131 }
132 uint32_t pointerto_line_numbers() const {
137 return pointer_to_linenumbers_;
138 }
139 uint16_t numberof_relocations() const {
142 return number_of_relocations_;
143 }
144 uint16_t numberof_line_numbers() const {
147 return number_of_linenumbers_;
148 }
149 uint32_t characteristics() const {
154 return characteristics_;
155 }
156 bool is_type(PE_SECTION_TYPES type) const;
159 const std::set<PE_SECTION_TYPES>& types() const;
162 bool has_characteristic(CHARACTERISTICS c) const {
165 return (characteristics() & static_cast<size_t>(c)) > 0;
166 }
167 std::vector<CHARACTERISTICS> characteristics_list() const;
170 void clear(uint8_t c);
173 void content(const std::vector<uint8_t>& data) override;
174
175 void name(std::string name) override;
176
177 void virtual_size(uint32_t virtual_sz) {
178 virtual_size_ = virtual_sz;
179 }
180
181 void pointerto_raw_data(uint32_t ptr);
182
183 void pointerto_relocation(uint32_t ptr) {
184 pointer_to_relocations_ = ptr;
185 }
186
187 void pointerto_line_numbers(uint32_t ptr) {
188 pointer_to_linenumbers_ = ptr;
189 }
190
191 void numberof_relocations(uint16_t nb) {
192 number_of_relocations_ = nb;
193 }
194
195 void numberof_line_numbers(uint16_t nb) {
196 number_of_linenumbers_ = nb;
197 }
198
199 void sizeof_raw_data(uint32_t sizeOfRawData);
200
201 void characteristics(uint32_t characteristics) {
202 characteristics_ = characteristics;
203 }
204
205 void type(PE_SECTION_TYPES type);
206 void add_type(PE_SECTION_TYPES type);
207 void remove_type(PE_SECTION_TYPES type);
208
209 Section& remove_characteristic(CHARACTERISTICS characteristic) {
210 characteristics_ &= ~static_cast<size_t>(characteristic);
211 return *this;
212 }
213
214 Section& add_characteristic(CHARACTERISTICS characteristic) {
215 characteristics_ |= static_cast<size_t>(characteristic);
216 return *this;
217 }
218
219 std::unique_ptr<SpanStream> stream() const;
220
221 void accept(Visitor& visitor) const override;
222
223 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Section& section);
224
225 private:
226 span<uint8_t> writable_content() {
227 return content_;
228 }
229
230 std::vector<uint8_t> content_;
231 std::vector<uint8_t> padding_;
232 uint32_t virtual_size_ = 0;
233 uint32_t pointer_to_relocations_ = 0;
234 uint32_t pointer_to_linenumbers_ = 0;
235 uint16_t number_of_relocations_ = 0;
236 uint16_t number_of_linenumbers_ = 0;
237 uint32_t characteristics_ = 0;
238 std::set<PE_SECTION_TYPES> types_ = {PE_SECTION_TYPES::UNKNOWN};
239};
240
241LIEF_API const char* to_string(Section::CHARACTERISTICS e);
242
243} // namespace PE
244} // namespace LIEF
245
246ENABLE_BITMASK_OPERATORS(LIEF::PE::Section::CHARACTERISTICS)
247
248#endif
Section.hpp
enums.hpp
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:52
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:47
LIEF::PE::Section
Class which represents a PE section.
Definition PE/Section.hpp:43
LIEF::PE::Section::type
void type(PE_SECTION_TYPES type)
LIEF::PE::Section::sizeof_raw_data
void sizeof_raw_data(uint32_t sizeOfRawData)
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:136
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:129
LIEF::PE::Section::remove_characteristic
Section & remove_characteristic(CHARACTERISTICS characteristic)
Definition PE/Section.hpp:209
LIEF::PE::Section::types
const std::set< PE_SECTION_TYPES > & types() const
Deprecated do not use. It will likely change in a future release of LIEF.
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:164
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:146
LIEF::PE::Section::Section
Section(const std::vector< uint8_t > &data, const std::string &name="", uint32_t characteristics=0)
LIEF::PE::Section::~Section
~Section() override=default
LIEF::PE::Section::CHARACTERISTICS
CHARACTERISTICS
Definition PE/Section.hpp:53
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:112
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)
LIEF::PE::Section::characteristics_list
std::vector< CHARACTERISTICS > characteristics_list() const
List of the section characteristics as a std::set.
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:153
LIEF::PE::Section::name
void name(std::string name) override
Change the section's name.
LIEF::PE::Section::Section
Section(const std::string &name)
LIEF::PE::Section::pointerto_relocation
void pointerto_relocation(uint32_t ptr)
Definition PE/Section.hpp:183
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:117
LIEF::PE::Section::pointerto_line_numbers
void pointerto_line_numbers(uint32_t ptr)
Definition PE/Section.hpp:187
LIEF::PE::Section::stream
std::unique_ptr< SpanStream > stream() const
LIEF::PE::Section::characteristics
void characteristics(uint32_t characteristics)
Definition PE/Section.hpp:201
LIEF::PE::Section::virtual_size
void virtual_size(uint32_t virtual_sz)
Definition PE/Section.hpp:177
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::add_type
void add_type(PE_SECTION_TYPES type)
LIEF::PE::Section::remove_type
void remove_type(PE_SECTION_TYPES type)
LIEF::PE::Section::numberof_line_numbers
void numberof_line_numbers(uint16_t nb)
Definition PE/Section.hpp:195
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:141
LIEF::PE::Section::is_type
bool is_type(PE_SECTION_TYPES type) const
Deprecated do not use. It will likely change in a future release of LIEF.
LIEF::PE::Section::accept
void accept(Visitor &visitor) const override
LIEF::PE::Section::virtual_size
uint32_t virtual_size() const
Return the size of the data when mapped in memory.
Definition PE/Section.hpp:107
LIEF::PE::Section::add_characteristic
Section & add_characteristic(CHARACTERISTICS characteristic)
Definition PE/Section.hpp:214
LIEF::PE::Section::numberof_relocations
void numberof_relocations(uint16_t nb)
Definition PE/Section.hpp:191
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
LIEF::PE::details
Definition CodeIntegrity.hpp:26
LIEF::PE
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
LIEF::PE::to_string
const char * to_string(DataDirectory::TYPES e)
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41