LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
PE/Section.hpp
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
23#include "LIEF/visibility.h"
24#include "LIEF/iterators.hpp"
25#include "LIEF/Abstract/Section.hpp"
26#include "LIEF/enums.hpp"
27#include "LIEF/PE/enums.hpp"
28
29namespace LIEF {
30namespace PE {
31
32class Parser;
33class Builder;
34class Binary;
35
36namespace details {
37struct pe_section;
38}
39
41class LIEF_API Section : public LIEF::Section {
42
43 friend class Parser;
44 friend class Builder;
45 friend class Binary;
46
47 public:
49 static constexpr size_t MAX_SECTION_NAME = 8;
50
51 enum class CHARACTERISTICS: size_t {
52 TYPE_NO_PAD = 0x00000008,
53 CNT_CODE = 0x00000020,
54 CNT_INITIALIZED_DATA = 0x00000040,
55 CNT_UNINITIALIZED_DATA = 0x00000080,
56 LNK_OTHER = 0x00000100,
57 LNK_INFO = 0x00000200,
58 LNK_REMOVE = 0x00000800,
59 LNK_COMDAT = 0x00001000,
60 GPREL = 0x00008000,
61 MEM_PURGEABLE = 0x00010000,
62 MEM_16BIT = 0x00020000,
63 MEM_LOCKED = 0x00040000,
64 MEM_PRELOAD = 0x00080000,
65 ALIGN_1BYTES = 0x00100000,
66 ALIGN_2BYTES = 0x00200000,
67 ALIGN_4BYTES = 0x00300000,
68 ALIGN_8BYTES = 0x00400000,
69 ALIGN_16BYTES = 0x00500000,
70 ALIGN_32BYTES = 0x00600000,
71 ALIGN_64BYTES = 0x00700000,
72 ALIGN_128BYTES = 0x00800000,
73 ALIGN_256BYTES = 0x00900000,
74 ALIGN_512BYTES = 0x00A00000,
75 ALIGN_1024BYTES = 0x00B00000,
76 ALIGN_2048BYTES = 0x00C00000,
77 ALIGN_4096BYTES = 0x00D00000,
78 ALIGN_8192BYTES = 0x00E00000,
79 LNK_NRELOC_OVFL = 0x01000000,
80 MEM_DISCARDABLE = 0x02000000,
81 MEM_NOT_CACHED = 0x04000000,
82 MEM_NOT_PAGED = 0x08000000,
83 MEM_SHARED = 0x10000000,
84 MEM_EXECUTE = 0x20000000,
85 MEM_READ = 0x40000000,
86 MEM_WRITE = 0x80000000
87 };
88
89 Section(const details::pe_section& header);
90 Section();
91 Section(const std::vector<uint8_t>& data,
92 const std::string& name = "", uint32_t characteristics = 0);
93 Section(const std::string& name);
94
95 Section& operator=(const Section&);
96 Section(const Section&);
97 ~Section() override;
98
100 uint32_t sizeof_raw_data() const;
101
105 uint32_t virtual_size() const {
106 return virtual_size_;
107 }
108
110 span<const uint8_t> content() const override {
111 return content_;
112 }
113
115 span<const uint8_t> padding() const {
116 return padding_;
117 }
118
120 uint32_t pointerto_raw_data() const;
121
127 uint32_t pointerto_relocation() const {
128 return pointer_to_relocations_;
129 }
130
134 uint32_t pointerto_line_numbers() const {
135 return pointer_to_linenumbers_;
136 }
137
139 uint16_t numberof_relocations() const {
140 return number_of_relocations_;
141 }
142
144 uint16_t numberof_line_numbers() const {
145 return number_of_linenumbers_;
146 }
147
151 uint32_t characteristics() const {
152 return characteristics_;
153 }
154
156 bool is_type(PE_SECTION_TYPES type) const;
157
159 const std::set<PE_SECTION_TYPES>& types() const;
160
162 bool has_characteristic(CHARACTERISTICS c) const {
163 return (characteristics() & static_cast<size_t>(c)) > 0;
164 }
165
167 std::vector<CHARACTERISTICS> characteristics_list() const;
168
170 void clear(uint8_t c);
171 void content(const std::vector<uint8_t>& data) override;
172
173 void name(const std::string& name) override;
174
175 void virtual_size(uint32_t virtual_sz) {
176 virtual_size_ = virtual_sz;
177 }
178
179 void pointerto_raw_data(uint32_t ptr);
180
181 void pointerto_relocation(uint32_t ptr) {
182 pointer_to_relocations_ = ptr;
183 }
184
185 void pointerto_line_numbers(uint32_t ptr) {
186 pointer_to_linenumbers_ = ptr;
187 }
188
189 void numberof_relocations(uint16_t nb) {
190 number_of_relocations_ = nb;
191 }
192
193 void numberof_line_numbers(uint16_t nb) {
194 number_of_linenumbers_ = nb;
195 }
196
197 void sizeof_raw_data(uint32_t sizeOfRawData);
198
199 void characteristics(uint32_t characteristics) {
200 characteristics_ = characteristics;
201 }
202
203 void type(PE_SECTION_TYPES type);
204 void add_type(PE_SECTION_TYPES type);
205 void remove_type(PE_SECTION_TYPES type);
206
207 void remove_characteristic(Section::CHARACTERISTICS characteristic) {
208 characteristics_ &= ~static_cast<size_t>(characteristic);
209 }
210
211 void add_characteristic(Section::CHARACTERISTICS characteristic) {
212 characteristics_ |= static_cast<size_t>(characteristic);
213 }
214
215 void accept(Visitor& visitor) const override;
216
217 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Section& section);
218
219 private:
220 span<uint8_t> writable_content() {
221 return content_;
222 }
223
224 std::vector<uint8_t> content_;
225 std::vector<uint8_t> padding_;
226 uint32_t virtual_size_ = 0;
227 uint32_t pointer_to_relocations_ = 0;
228 uint32_t pointer_to_linenumbers_ = 0;
229 uint16_t number_of_relocations_ = 0;
230 uint16_t number_of_linenumbers_ = 0;
231 uint32_t characteristics_ = 0;
232 std::set<PE_SECTION_TYPES> types_ = {PE_SECTION_TYPES::UNKNOWN};
233};
234
235LIEF_API const char* to_string(Section::CHARACTERISTICS e);
236
237} // namespace PE
238} // namespace LIEF
239
240ENABLE_BITMASK_OPERATORS(LIEF::PE::Section::CHARACTERISTICS)
241
242#endif
Class which represents a PE binary This is the main interface to manage and modify a PE executable.
Definition PE/Binary.hpp:54
Class that is used to rebuild a raw PE binary from a PE::Binary object.
Definition PE/Builder.hpp:45
Main interface to parse PE binaries. In particular the static functions: Parser::parse should be used...
Definition PE/Parser.hpp:47
Class which represents a PE section.
Definition PE/Section.hpp:41
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:134
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:127
const std::set< PE_SECTION_TYPES > & types() const
Deprecated do not use. It will likely change in a future release of LIEF.
bool has_characteristic(CHARACTERISTICS c) const
Check if the section has the given CHARACTERISTICS.
Definition PE/Section.hpp:162
uint16_t numberof_line_numbers() const
No longer used in recent PE binaries produced by Visual Studio.
Definition PE/Section.hpp:144
span< const uint8_t > content() const override
The actual content of the section.
Definition PE/Section.hpp:110
void clear(uint8_t c)
Fill the content of the section with the given char
std::vector< CHARACTERISTICS > characteristics_list() const
List of the section characteristics as a std::set.
uint32_t characteristics() const
Characteristics of the section: it gives information about the permissions of the section when mapped...
Definition PE/Section.hpp:151
void name(const std::string &name) override
Change the section's name.
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:115
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.
uint16_t numberof_relocations() const
No longer used in recent PE binaries produced by Visual Studio.
Definition PE/Section.hpp:139
bool is_type(PE_SECTION_TYPES type) const
Deprecated do not use. It will likely change in a future release of LIEF.
uint32_t virtual_size() const
Return the size of the data when mapped in memory.
Definition PE/Section.hpp:105
Class which represents an abstracted section.
Definition Abstract/Section.hpp:30
virtual std::string name() const
section's name
PE_SECTION_TYPES
Common section type.
Definition PE/enums.hpp:666
LIEF namespace.
Definition Abstract/Binary.hpp:32