LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
COFF/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_COFF_SECTION_H
17#define LIEF_COFF_SECTION_H
18#include <cstdint>
19#include <memory>
20#include <ostream>
21
22#include "LIEF/visibility.h"
24#include "LIEF/PE/Section.hpp"
26#include "LIEF/iterators.hpp"
27#include "LIEF/optional.hpp"
28
29namespace LIEF {
30class BinaryStream;
31
32namespace COFF {
33class Relocation;
34class Parser;
35class Symbol;
36
37class LIEF_API Section : public LIEF::Section {
40 public:
41 friend class Parser;
43
44 using COMDAT_SELECTION = AuxiliarySectionDefinition::COMDAT_SELECTION;
45 struct LIEF_API ComdatInfo {
49 Symbol* symbol = nullptr;
50 COMDAT_SELECTION kind = COMDAT_SELECTION::NONE;
51 };
52 using CHARACTERISTICS = LIEF::PE::Section::CHARACTERISTICS;
55 using relocations_t = std::vector<Relocation*>;
58 using it_relocations = ref_iterator<relocations_t&, Relocation*>;
61 using it_const_relocations = const_ref_iterator<const relocations_t&, const Relocation*>;
64 using symbols_t = std::vector<Symbol*>;
67 using it_symbols = ref_iterator<symbols_t&, Symbol*>;
70 using it_const_symbols = const_ref_iterator<const symbols_t&, const Symbol*>;
73 static std::unique_ptr<Section> parse(BinaryStream& stream);
76 uint32_t sizeof_raw_data() const {
79 return size_;
80 }
81 uint32_t virtual_size() const {
84 return virtual_size_;
85 }
86 span<const uint8_t> content() const override {
89 return content_;
90 }
91 uint32_t pointerto_raw_data() const {
94 return offset_;
95 }
96 uint32_t pointerto_relocation() const {
99 return pointer_to_relocations_;
100 }
101 uint32_t pointerto_line_numbers() const {
108 return pointer_to_linenumbers_;
109 }
110 uint16_t numberof_relocations() const {
117 return number_of_relocations_;
118 }
119 uint16_t numberof_line_numbers() const {
122 return number_of_linenumbers_;
123 }
124 uint32_t characteristics() const {
129 return characteristics_;
130 }
131 bool has_characteristic(CHARACTERISTICS c) const {
134 return (characteristics() & (uint32_t)c) > 0;
135 }
136 std::vector<CHARACTERISTICS> characteristics_list() const {
139 return LIEF::PE::Section::characteristics_to_list(characteristics_);
140 }
141 bool is_discardable() const {
146 return has_characteristic(CHARACTERISTICS::MEM_DISCARDABLE);
147 }
148
149 void clear(uint8_t c) {
150 std::fill(content_.begin(), content_.end(), c);
151 }
152 it_relocations relocations() {
155 return relocations_;
156 }
157
158 it_const_relocations relocations() const {
159 return relocations_;
160 }
161 it_symbols symbols() {
164 return symbols_;
165 }
166
167 it_const_symbols symbols() const {
168 return symbols_;
169 }
170 optional<ComdatInfo> comdat_info() const;
174 bool has_extended_relocations() const {
178 return has_characteristic(CHARACTERISTICS::LNK_NRELOC_OVFL) &&
179 numberof_relocations() == std::numeric_limits<uint16_t>::max();
180 }
181
182 void content(const std::vector<uint8_t>& data) override {
183 content_ = data;
184 }
185
186 void name(std::string name) override;
187
188 void virtual_size(uint32_t virtual_sz) {
189 virtual_size_ = virtual_sz;
190 }
191
192 void pointerto_raw_data(uint32_t ptr) {
193 offset(ptr);
194 }
195
196 void pointerto_relocation(uint32_t ptr) {
197 pointer_to_relocations_ = ptr;
198 }
199
200 void pointerto_line_numbers(uint32_t ptr) {
201 pointer_to_linenumbers_ = ptr;
202 }
203
204 void numberof_relocations(uint16_t nb) {
205 number_of_relocations_ = nb;
206 }
207
208 void numberof_line_numbers(uint16_t nb) {
209 number_of_linenumbers_ = nb;
210 }
211
212 void sizeof_raw_data(uint32_t size) {
213 this->size(size);
214 }
215
216 void characteristics(uint32_t characteristics) {
217 characteristics_ = characteristics;
218 }
219
220 std::string to_string() const;
221
222 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Section& sec) {
223 os << sec.to_string();
224 return os;
225 }
226
227 ~Section() override = default;
228
229 private:
230 Section() = default;
231
232 std::vector<uint8_t> content_;
233 uint32_t virtual_size_ = 0;
234 uint32_t pointer_to_relocations_ = 0;
235 uint32_t pointer_to_linenumbers_ = 0;
236 uint16_t number_of_relocations_ = 0;
237 uint16_t number_of_linenumbers_ = 0;
238 uint32_t characteristics_ = 0;
239
240 relocations_t relocations_;
241 symbols_t symbols_;
242};
243
244inline const char* to_string(Section::CHARACTERISTICS e) {
245 return LIEF::PE::to_string(e);
246}
247
248}
249}
250#endif
Section.hpp
AuxiliarySectionDefinition.hpp
Section.hpp
LIEF::BinaryStream
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
LIEF::COFF::Parser
Definition COFF/Parser.hpp:34
LIEF::COFF::Relocation
This class represents a COFF relocation.
Definition COFF/Relocation.hpp:34
LIEF::COFF::Section
This class represents a COFF section.
Definition COFF/Section.hpp:39
LIEF::COFF::Section::operator<<
friend std::ostream & operator<<(std::ostream &os, const Section &sec)
Definition COFF/Section.hpp:222
LIEF::COFF::Section::pointerto_relocation
void pointerto_relocation(uint32_t ptr)
Definition COFF/Section.hpp:196
LIEF::COFF::Section::characteristics
uint32_t characteristics() const
Characteristics of the section: it provides information about the permissions of the section when map...
Definition COFF/Section.hpp:128
LIEF::COFF::Section::numberof_line_numbers
uint16_t numberof_line_numbers() const
Number of line number entries (if any).
Definition COFF/Section.hpp:121
LIEF::COFF::Section::name
void name(std::string name) override
Change the section's name.
LIEF::COFF::Section::to_string
std::string to_string() const
LIEF::COFF::Section::relocations
it_relocations relocations()
Iterator over the relocations associated with this section.
Definition COFF/Section.hpp:154
LIEF::COFF::Section::relocations
it_const_relocations relocations() const
Definition COFF/Section.hpp:158
LIEF::COFF::Section::has_extended_relocations
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:177
LIEF::COFF::Section::virtual_size
void virtual_size(uint32_t virtual_sz)
Definition COFF/Section.hpp:188
LIEF::COFF::Section::content
void content(const std::vector< uint8_t > &data) override
Change section content.
Definition COFF/Section.hpp:182
LIEF::COFF::Section::symbols
it_const_symbols symbols() const
Definition COFF/Section.hpp:167
LIEF::COFF::Section::pointerto_relocation
uint32_t pointerto_relocation() const
Offset to the relocation table.
Definition COFF/Section.hpp:98
LIEF::COFF::Section::parse
static std::unique_ptr< Section > parse(BinaryStream &stream)
Parse a section from the given stream.
LIEF::COFF::Section::pointerto_raw_data
uint32_t pointerto_raw_data() const
Offset to the section's content.
Definition COFF/Section.hpp:93
LIEF::COFF::Section::sizeof_raw_data
void sizeof_raw_data(uint32_t size)
Definition COFF/Section.hpp:212
LIEF::COFF::Section::numberof_relocations
void numberof_relocations(uint16_t nb)
Definition COFF/Section.hpp:204
LIEF::COFF::Section::content
span< const uint8_t > content() const override
Content wrapped by this section.
Definition COFF/Section.hpp:88
LIEF::COFF::Section::characteristics_list
std::vector< CHARACTERISTICS > characteristics_list() const
List of the section characteristics.
Definition COFF/Section.hpp:138
LIEF::COFF::Section::pointerto_raw_data
void pointerto_raw_data(uint32_t ptr)
Definition COFF/Section.hpp:192
LIEF::COFF::Section::numberof_line_numbers
void numberof_line_numbers(uint16_t nb)
Definition COFF/Section.hpp:208
LIEF::COFF::Section::~Section
~Section() override=default
LIEF::COFF::Section::pointerto_line_numbers
uint32_t pointerto_line_numbers() const
The file pointer to the beginning of line-number entries for the section.
Definition COFF/Section.hpp:107
LIEF::COFF::Section::clear
void clear(uint8_t c)
Definition COFF/Section.hpp:149
LIEF::COFF::Section::has_characteristic
bool has_characteristic(CHARACTERISTICS c) const
Check if the section has the given CHARACTERISTICS.
Definition COFF/Section.hpp:133
LIEF::COFF::Section::virtual_size
uint32_t virtual_size() const
Virtual size of the section (should be 0)
Definition COFF/Section.hpp:83
LIEF::COFF::Section::numberof_relocations
uint16_t numberof_relocations() const
Number of relocations.
Definition COFF/Section.hpp:116
LIEF::COFF::Section::comdat_info
optional< ComdatInfo > comdat_info() const
Return comdat infomration (only if the section has the CHARACTERISTICS::LNK_COMDAT characteristic)
LIEF::COFF::Section::characteristics
void characteristics(uint32_t characteristics)
Definition COFF/Section.hpp:216
LIEF::COFF::Section::CHARACTERISTICS
LIEF::PE::Section::CHARACTERISTICS CHARACTERISTICS
Mirror Characteristics from PE.
Definition COFF/Section.hpp:54
LIEF::COFF::Section::pointerto_line_numbers
void pointerto_line_numbers(uint32_t ptr)
Definition COFF/Section.hpp:200
LIEF::COFF::Section::sizeof_raw_data
uint32_t sizeof_raw_data() const
Return the size of the data in the section.
Definition COFF/Section.hpp:78
LIEF::COFF::Section::is_discardable
bool is_discardable() const
True if the section can be discarded as needed.
Definition COFF/Section.hpp:145
LIEF::COFF::Section::symbols
it_symbols symbols()
Iterator over the symbols associated with this section.
Definition COFF/Section.hpp:163
LIEF::COFF::Symbol
This class represents a COFF symbol.
Definition COFF/Symbol.hpp:35
LIEF::PE::Section::characteristics_to_list
static std::vector< CHARACTERISTICS > characteristics_to_list(uint32_t value)
LIEF::PE::Section::CHARACTERISTICS
CHARACTERISTICS
Definition PE/Section.hpp:56
LIEF::Section::name
virtual std::string name() const
section's name
Definition Abstract/Section.hpp:44
iterators.hpp
LIEF::COFF
Definition AuxiliarySymbol.hpp:29
LIEF::COFF::to_string
const char * to_string(AuxiliarySectionDefinition::COMDAT_SELECTION e)
LIEF::PE::to_string
const char * to_string(CODE_PAGES e)
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:39
optional.hpp
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41