-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathSection.cpp
More file actions
215 lines (192 loc) Β· 9.13 KB
/
Section.cpp
File metadata and controls
215 lines (192 loc) Β· 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* Copyright 2017 - 2025 R. Thomas
* Copyright 2017 - 2025 Quarkslab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <algorithm>
#include <iterator>
#include <spdlog/fmt/fmt.h>
#include "frozen.hpp"
#include "logging.hpp"
#include "PE/Structures.hpp"
#include "LIEF/Visitor.hpp"
#include "LIEF/BinaryStream/SpanStream.hpp"
#include "LIEF/PE/Section.hpp"
#include "LIEF/COFF/String.hpp"
namespace LIEF {
namespace PE {
static constexpr std::array CHARACTERISTICS_LIST = {
Section::CHARACTERISTICS::TYPE_NO_PAD,
Section::CHARACTERISTICS::CNT_CODE,
Section::CHARACTERISTICS::CNT_INITIALIZED_DATA,
Section::CHARACTERISTICS::CNT_UNINITIALIZED_DATA,
Section::CHARACTERISTICS::LNK_OTHER,
Section::CHARACTERISTICS::LNK_INFO,
Section::CHARACTERISTICS::LNK_REMOVE,
Section::CHARACTERISTICS::LNK_COMDAT,
Section::CHARACTERISTICS::GPREL,
Section::CHARACTERISTICS::MEM_PURGEABLE,
Section::CHARACTERISTICS::MEM_16BIT,
Section::CHARACTERISTICS::MEM_LOCKED,
Section::CHARACTERISTICS::MEM_PRELOAD,
Section::CHARACTERISTICS::ALIGN_1BYTES,
Section::CHARACTERISTICS::ALIGN_2BYTES,
Section::CHARACTERISTICS::ALIGN_4BYTES,
Section::CHARACTERISTICS::ALIGN_8BYTES,
Section::CHARACTERISTICS::ALIGN_16BYTES,
Section::CHARACTERISTICS::ALIGN_32BYTES,
Section::CHARACTERISTICS::ALIGN_64BYTES,
Section::CHARACTERISTICS::ALIGN_128BYTES,
Section::CHARACTERISTICS::ALIGN_256BYTES,
Section::CHARACTERISTICS::ALIGN_512BYTES,
Section::CHARACTERISTICS::ALIGN_1024BYTES,
Section::CHARACTERISTICS::ALIGN_2048BYTES,
Section::CHARACTERISTICS::ALIGN_4096BYTES,
Section::CHARACTERISTICS::ALIGN_8192BYTES,
Section::CHARACTERISTICS::LNK_NRELOC_OVFL,
Section::CHARACTERISTICS::MEM_DISCARDABLE,
Section::CHARACTERISTICS::MEM_NOT_CACHED,
Section::CHARACTERISTICS::MEM_NOT_PAGED,
Section::CHARACTERISTICS::MEM_SHARED,
Section::CHARACTERISTICS::MEM_EXECUTE,
Section::CHARACTERISTICS::MEM_READ,
Section::CHARACTERISTICS::MEM_WRITE,
};
Section::Section(const details::pe_section& header) :
virtual_size_{header.VirtualSize},
pointer_to_relocations_{header.PointerToRelocations},
pointer_to_linenumbers_{header.PointerToLineNumbers},
number_of_relocations_{header.NumberOfRelocations},
number_of_linenumbers_{header.NumberOfLineNumbers},
characteristics_{header.Characteristics}
{
name_ = std::string(header.Name, sizeof(header.Name));
virtual_address_ = header.VirtualAddress;
size_ = header.SizeOfRawData;
offset_ = header.PointerToRawData;
}
void Section::name(std::string name) {
if (name.size() > MAX_SECTION_NAME) {
LIEF_ERR("The max size of a section's name is {} vs {}", MAX_SECTION_NAME,
name.size());
return;
}
name_ = std::move(name);
}
uint32_t Section::sizeof_raw_data() const {
return size();
}
uint32_t Section::pointerto_raw_data() const {
return offset();
}
std::vector<Section::CHARACTERISTICS> Section::characteristics_to_list(uint32_t value) {
std::vector<Section::CHARACTERISTICS> list;
list.reserve(3);
std::copy_if(CHARACTERISTICS_LIST.begin(), CHARACTERISTICS_LIST.end(),
std::back_inserter(list),
[value] (CHARACTERISTICS c) { return (value & (uint32_t)c) != 0; });
return list;
}
void Section::content(const std::vector<uint8_t>& data) {
content_ = data;
}
void Section::accept(LIEF::Visitor& visitor) const {
visitor.visit(*this);
}
std::unique_ptr<SpanStream> Section::stream() const {
return std::make_unique<SpanStream>(content());
}
void Section::clear(uint8_t c) {
std::fill(std::begin(content_), std::end(content_), c);
}
std::ostream& operator<<(std::ostream& os, const Section& section) {
static constexpr auto WIDTH = 24;
const auto& list = section.characteristics_list();
std::vector<std::string> list_str;
list_str.reserve(list.size());
std::transform(list.begin(), list.end(), std::back_inserter(list_str),
[] (const auto c) { return to_string(c); });
std::vector<std::string> fullname_hex;
fullname_hex.reserve(section.name().size());
std::transform(section.fullname().begin(), section.fullname().end(),
std::back_inserter(fullname_hex),
[] (const char c) { return fmt::format("{:02x}", c); });
const std::string fullname_hex_joined =
fmt::format("{}", fmt::join(fullname_hex, " "));
const std::string characteristics_joined =
fmt::format("{}", fmt::join(list_str, ", "));
if (const COFF::String* coff_str = section.coff_string()) {
os << fmt::format("{:{}} {} ({}, {})\n", "Name:", WIDTH, section.name(),
fullname_hex_joined, coff_str->str());
} else {
os << fmt::format("{:{}} {} ({})\n", "Name:", WIDTH, section.name(),
fullname_hex_joined);
}
os << fmt::format("{:{}} 0x{:x}\n", "Virtual Size", WIDTH, section.virtual_size())
<< fmt::format("{:{}} 0x{:x}\n", "Virtual Address", WIDTH, section.virtual_address())
<< fmt::format("{:{}} [0x{:08x}, 0x{:08x}]\n", "Range", WIDTH,
section.virtual_address(), section.virtual_address() + section.virtual_size())
<< fmt::format("{:{}} 0x{:x}\n", "Size of raw data", WIDTH, section.sizeof_raw_data())
<< fmt::format("{:{}} 0x{:x}\n", "Pointer to raw data", WIDTH, section.pointerto_raw_data())
<< fmt::format("{:{}} [0x{:08x}, 0x{:08x}]\n", "Range", WIDTH,
section.pointerto_raw_data(), section.pointerto_raw_data() + section.sizeof_raw_data())
<< fmt::format("{:{}} 0x{:x}\n", "Pointer to relocations", WIDTH, section.pointerto_relocation())
<< fmt::format("{:{}} 0x{:x}\n", "Pointer to line numbers", WIDTH, section.pointerto_line_numbers())
<< fmt::format("{:{}} 0x{:x}\n", "Number of relocations", WIDTH, section.numberof_relocations())
<< fmt::format("{:{}} 0x{:x}\n", "Number of lines", WIDTH, section.numberof_line_numbers())
<< fmt::format("{:{}} {}", "Characteristics", WIDTH, characteristics_joined);
return os;
}
const char* to_string(Section::CHARACTERISTICS e) {
CONST_MAP(Section::CHARACTERISTICS, const char*, 35) enumStrings {
{ Section::CHARACTERISTICS::TYPE_NO_PAD, "TYPE_NO_PAD" },
{ Section::CHARACTERISTICS::CNT_CODE, "CNT_CODE" },
{ Section::CHARACTERISTICS::CNT_INITIALIZED_DATA, "CNT_INITIALIZED_DATA" },
{ Section::CHARACTERISTICS::CNT_UNINITIALIZED_DATA, "CNT_UNINITIALIZED_DATA" },
{ Section::CHARACTERISTICS::LNK_OTHER, "LNK_OTHER" },
{ Section::CHARACTERISTICS::LNK_INFO, "LNK_INFO" },
{ Section::CHARACTERISTICS::LNK_REMOVE, "LNK_REMOVE" },
{ Section::CHARACTERISTICS::LNK_COMDAT, "LNK_COMDAT" },
{ Section::CHARACTERISTICS::GPREL, "GPREL" },
{ Section::CHARACTERISTICS::MEM_PURGEABLE, "MEM_PURGEABLE" },
{ Section::CHARACTERISTICS::MEM_16BIT, "MEM_16BIT" },
{ Section::CHARACTERISTICS::MEM_LOCKED, "MEM_LOCKED" },
{ Section::CHARACTERISTICS::MEM_PRELOAD, "MEM_PRELOAD" },
{ Section::CHARACTERISTICS::ALIGN_1BYTES, "ALIGN_1BYTES" },
{ Section::CHARACTERISTICS::ALIGN_2BYTES, "ALIGN_2BYTES" },
{ Section::CHARACTERISTICS::ALIGN_4BYTES, "ALIGN_4BYTES" },
{ Section::CHARACTERISTICS::ALIGN_8BYTES, "ALIGN_8BYTES" },
{ Section::CHARACTERISTICS::ALIGN_16BYTES, "ALIGN_16BYTES" },
{ Section::CHARACTERISTICS::ALIGN_32BYTES, "ALIGN_32BYTES" },
{ Section::CHARACTERISTICS::ALIGN_64BYTES, "ALIGN_64BYTES" },
{ Section::CHARACTERISTICS::ALIGN_128BYTES, "ALIGN_128BYTES" },
{ Section::CHARACTERISTICS::ALIGN_256BYTES, "ALIGN_256BYTES" },
{ Section::CHARACTERISTICS::ALIGN_512BYTES, "ALIGN_512BYTES" },
{ Section::CHARACTERISTICS::ALIGN_1024BYTES, "ALIGN_1024BYTES" },
{ Section::CHARACTERISTICS::ALIGN_2048BYTES, "ALIGN_2048BYTES" },
{ Section::CHARACTERISTICS::ALIGN_4096BYTES, "ALIGN_4096BYTES" },
{ Section::CHARACTERISTICS::ALIGN_8192BYTES, "ALIGN_8192BYTES" },
{ Section::CHARACTERISTICS::LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL" },
{ Section::CHARACTERISTICS::MEM_DISCARDABLE, "MEM_DISCARDABLE" },
{ Section::CHARACTERISTICS::MEM_NOT_CACHED, "MEM_NOT_CACHED" },
{ Section::CHARACTERISTICS::MEM_NOT_PAGED, "MEM_NOT_PAGED" },
{ Section::CHARACTERISTICS::MEM_SHARED, "MEM_SHARED" },
{ Section::CHARACTERISTICS::MEM_EXECUTE, "MEM_EXECUTE" },
{ Section::CHARACTERISTICS::MEM_READ, "MEM_READ" },
{ Section::CHARACTERISTICS::MEM_WRITE, "MEM_WRITE" }
};
const auto it = enumStrings.find(e);
return it == enumStrings.end() ? "UNKNOWN" : it->second;
}
}
}