LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
COFF/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_COFF_SECTION_H
17#define LIEF_COFF_SECTION_H
18#include <cstdint>
19#include <memory>
20#include <ostream>
21#include <string>
22
25#include "LIEF/PE/Section.hpp"
27#include "LIEF/iterators.hpp"
28#include "LIEF/visibility.h"
29#include <optional>
30
31namespace LIEF {
32class BinaryStream;
33
34namespace COFF {
35class Relocation;
36class Parser;
37class Symbol;
38
39
41class LIEF_API Section : public LIEF::Section {
42 public:
43 friend class Parser;
45
47
51 Symbol* symbol = nullptr;
52 COMDAT_SELECTION kind = COMDAT_SELECTION::NONE;
53 };
54
57
59 using relocations_t = std::vector<Relocation*>;
60
63
67
70 using symbols_t = std::vector<Symbol*>;
71
74
77
79 static std::unique_ptr<Section> parse(BinaryStream& stream);
80
82 uint32_t sizeof_raw_data() const {
83 return size_;
84 }
85
87 uint32_t virtual_size() const {
88 return virtual_size_;
89 }
90
93 return content_;
94 }
95
97 uint32_t pointerto_raw_data() const {
98 return offset_;
99 }
100
102 uint32_t pointerto_relocation() const {
103 return pointer_to_relocations_;
104 }
105
111 uint32_t pointerto_line_numbers() const {
112 return pointer_to_linenumbers_;
113 }
114
120 uint16_t numberof_relocations() const {
121 return number_of_relocations_;
122 }
123
125 uint16_t numberof_line_numbers() const {
126 return number_of_linenumbers_;
127 }
128
132 uint32_t characteristics() const {
133 return characteristics_;
134 }
135
138 return (characteristics() & (uint32_t)c) > 0;
139 }
140
142 std::vector<CHARACTERISTICS> characteristics_list() const {
143 return LIEF::PE::Section::characteristics_to_list(characteristics_);
144 }
145
149 bool is_discardable() const {
150 return has_characteristic(CHARACTERISTICS::MEM_DISCARDABLE);
151 }
152
153 void clear(uint8_t c) {
154 std::fill(content_.begin(), content_.end(), c);
155 }
156
159 return relocations_;
160 }
161
163 return relocations_;
164 }
165
168 return symbols_;
169 }
170
172 return symbols_;
173 }
174
177 std::optional<ComdatInfo> comdat_info() const;
178
182 static constexpr auto MAX_RELOC = /*uint16_t::max*/ 65535;
183 return has_characteristic(CHARACTERISTICS::LNK_NRELOC_OVFL) &&
184 numberof_relocations() == MAX_RELOC;
185 }
186
187 void content(const std::vector<uint8_t>& data) override {
188 content_ = data;
189 }
190
191 void name(std::string name) override;
192
193 void virtual_size(uint32_t virtual_sz) {
194 virtual_size_ = virtual_sz;
195 }
196
197 void pointerto_raw_data(uint32_t ptr) {
198 offset(ptr);
199 }
200
201 void pointerto_relocation(uint32_t ptr) {
202 pointer_to_relocations_ = ptr;
203 }
204
205 void pointerto_line_numbers(uint32_t ptr) {
206 pointer_to_linenumbers_ = ptr;
207 }
208
209 void numberof_relocations(uint16_t nb) {
210 number_of_relocations_ = nb;
211 }
212
213 void numberof_line_numbers(uint16_t nb) {
214 number_of_linenumbers_ = nb;
215 }
216
217 void sizeof_raw_data(uint32_t size) {
218 this->size(size);
219 }
220
222 characteristics_ = characteristics;
223 }
224
230 return coff_string_;
231 }
232
233 const String* coff_string() const {
234 return coff_string_;
235 }
236
237 std::string to_string() const;
238
239 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Section& sec) {
240 os << sec.to_string();
241 return os;
242 }
243
244 ~Section() override = default;
245
246 private:
247 Section() = default;
248
249 std::vector<uint8_t> content_;
250 uint32_t virtual_size_ = 0;
251 uint32_t pointer_to_relocations_ = 0;
252 uint32_t pointer_to_linenumbers_ = 0;
253 uint16_t number_of_relocations_ = 0;
254 uint16_t number_of_linenumbers_ = 0;
255 uint32_t characteristics_ = 0;
256
257 relocations_t relocations_;
258 symbols_t symbols_;
259
260 String* coff_string_ = nullptr;
261};
262
263inline const char* to_string(Section::CHARACTERISTICS e) {
264 return LIEF::PE::to_string(e);
265}
266
267}
268}
269#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:35
COMDAT_SELECTION
Values for the AuxiliarySectionDefinition::selection attribute.
Definition AuxiliarySectionDefinition.hpp:66
Definition COFF/Parser.hpp:34
This class represents a COFF relocation.
Definition COFF/Relocation.hpp:34
friend std::ostream & operator<<(std::ostream &os, const Section &sec)
Definition COFF/Section.hpp:239
std::optional< ComdatInfo > comdat_info() const
Return comdat information (only if the section has the CHARACTERISTICS::LNK_COMDAT characteristic).
const_ref_iterator< const symbols_t &, const Symbol * > it_const_symbols
Iterator that outputs const Symbol&.
Definition COFF/Section.hpp:76
void pointerto_relocation(uint32_t ptr)
Definition COFF/Section.hpp:201
uint32_t characteristics() const
Characteristics of the section: it provides information about the permissions of the section when map...
Definition COFF/Section.hpp:132
std::vector< Symbol * > symbols_t
Container for the symbols associated with this section (owned by the Binary object).
Definition COFF/Section.hpp:70
uint16_t numberof_line_numbers() const
Number of line number entries (if any).
Definition COFF/Section.hpp:125
void name(std::string name) override
Change the section's name.
String * coff_string()
Return the COFF string associated with the section's name (or a nullptr).
Definition COFF/Section.hpp:229
std::string to_string() const
it_relocations relocations()
Iterator over the relocations associated with this section.
Definition COFF/Section.hpp:158
it_const_relocations relocations() const
Definition COFF/Section.hpp:162
bool has_extended_relocations() const
Whether there is a large number of relocations whose number need to be stored in the virtual address ...
Definition COFF/Section.hpp:181
void virtual_size(uint32_t virtual_sz)
Definition COFF/Section.hpp:193
const_ref_iterator< const relocations_t &, const Relocation * > it_const_relocations
Iterator that outputs const Relocation&.
Definition COFF/Section.hpp:65
void content(const std::vector< uint8_t > &data) override
Change section content.
Definition COFF/Section.hpp:187
ref_iterator< symbols_t &, Symbol * > it_symbols
Iterator that outputs Symbol&.
Definition COFF/Section.hpp:73
it_const_symbols symbols() const
Definition COFF/Section.hpp:171
uint32_t pointerto_relocation() const
Offset to the relocation table.
Definition COFF/Section.hpp:102
std::vector< Relocation * > relocations_t
Container for the relocations in this section (owned by the Binary object).
Definition COFF/Section.hpp:59
static std::unique_ptr< Section > parse(BinaryStream &stream)
Parse a section from the given stream.
uint32_t pointerto_raw_data() const
Offset to the section's content.
Definition COFF/Section.hpp:97
void sizeof_raw_data(uint32_t size)
Definition COFF/Section.hpp:217
const String * coff_string() const
Definition COFF/Section.hpp:233
void numberof_relocations(uint16_t nb)
Definition COFF/Section.hpp:209
span< const uint8_t > content() const override
Content wrapped by this section.
Definition COFF/Section.hpp:92
std::vector< CHARACTERISTICS > characteristics_list() const
List of the section characteristics.
Definition COFF/Section.hpp:142
void pointerto_raw_data(uint32_t ptr)
Definition COFF/Section.hpp:197
void numberof_line_numbers(uint16_t nb)
Definition COFF/Section.hpp:213
~Section() override=default
uint32_t pointerto_line_numbers() const
The file pointer to the beginning of line-number entries for the section.
Definition COFF/Section.hpp:111
void clear(uint8_t c)
Definition COFF/Section.hpp:153
AuxiliarySectionDefinition::COMDAT_SELECTION COMDAT_SELECTION
Definition COFF/Section.hpp:46
friend class Parser
Definition COFF/Section.hpp:43
bool has_characteristic(CHARACTERISTICS c) const
Check if the section has the given CHARACTERISTICS.
Definition COFF/Section.hpp:137
uint32_t virtual_size() const
Virtual size of the section (should be 0).
Definition COFF/Section.hpp:87
uint16_t numberof_relocations() const
Number of relocations.
Definition COFF/Section.hpp:120
ref_iterator< relocations_t &, Relocation * > it_relocations
Iterator that outputs Relocation&.
Definition COFF/Section.hpp:62
void characteristics(uint32_t characteristics)
Definition COFF/Section.hpp:221
LIEF::PE::Section::CHARACTERISTICS CHARACTERISTICS
Mirror Characteristics from PE.
Definition COFF/Section.hpp:56
void pointerto_line_numbers(uint32_t ptr)
Definition COFF/Section.hpp:205
uint32_t sizeof_raw_data() const
Return the size of the data in the section.
Definition COFF/Section.hpp:82
bool is_discardable() const
True if the section can be discarded as needed.
Definition COFF/Section.hpp:149
it_symbols symbols()
Iterator over the symbols associated with this section.
Definition COFF/Section.hpp:167
This class represents a string located in the COFF string table.
Definition String.hpp:35
This class represents a COFF symbol.
Definition COFF/Symbol.hpp:37
static std::vector< CHARACTERISTICS > characteristics_to_list(uint32_t value)
CHARACTERISTICS
Definition PE/Section.hpp:57
Class which represents an abstracted section.
Definition Abstract/Section.hpp:31
virtual void size(uint64_t size)
Change the section size.
Definition Abstract/Section.hpp:61
virtual uint64_t offset() const
Offset in the binary.
Definition Abstract/Section.hpp:71
virtual std::string_view name() const
Section's name.
Definition Abstract/Section.hpp:45
Iterator which returns reference on container's values.
Definition iterators.hpp:47
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition AuxiliarySymbol.hpp:30
const char * to_string(AuxiliarySectionDefinition::COMDAT_SELECTION e)
const char * to_string(CODE_PAGES e)
LIEF namespace.
Definition Abstract/Binary.hpp:41
tcb::span< ElementType, Extent > span
Definition span.hpp:22
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which returns a const ref on container's values.
Definition iterators.hpp:316
This structure wraps comdat information which is composed of the symbol associated with the comdat se...
Definition COFF/Section.hpp:50
COMDAT_SELECTION kind
Definition COFF/Section.hpp:52
Symbol * symbol
Definition COFF/Section.hpp:51
#define LIEF_API
Definition visibility.h:45