-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathSection.cpp
More file actions
143 lines (119 loc) Β· 4.84 KB
/
Section.cpp
File metadata and controls
143 lines (119 loc) Β· 4.84 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
/* 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 <sstream>
#include <spdlog/fmt/fmt.h>
#include "LIEF/COFF/Section.hpp"
#include "LIEF/COFF/Symbol.hpp"
#include "LIEF/COFF/AuxiliarySymbol.hpp"
#include "LIEF/COFF/AuxiliarySymbols/AuxiliarySectionDefinition.hpp"
#include "LIEF/BinaryStream/BinaryStream.hpp"
#include "PE/Structures.hpp"
#include "logging.hpp"
namespace LIEF::COFF {
std::unique_ptr<Section> Section::parse(BinaryStream& stream) {
auto raw_sec = stream.read<LIEF::PE::details::pe_section>();
if (!raw_sec) {
return nullptr;
}
auto sec = std::unique_ptr<Section>(new Section());
sec->virtual_size_ = raw_sec->VirtualSize;
sec->pointer_to_relocations_ = raw_sec->PointerToRelocations;
sec->pointer_to_linenumbers_ = raw_sec->PointerToLineNumbers;
sec->number_of_relocations_ = raw_sec->NumberOfRelocations;
sec->number_of_linenumbers_ = raw_sec->NumberOfLineNumbers;
sec->characteristics_ = raw_sec->Characteristics;
sec->name_ = std::string(raw_sec->Name, sizeof(raw_sec->Name));
sec->virtual_address_ = raw_sec->VirtualAddress;
sec->size_ = raw_sec->SizeOfRawData;
sec->offset_ = raw_sec->PointerToRawData;
stream.peek_data(sec->content_, sec->offset(), sec->size(),
sec->virtual_address());
return sec;
}
void Section::name(std::string name) {
if (name.size() > LIEF::PE::Section::MAX_SECTION_NAME) {
LIEF_ERR("The max size of a section's name is {} vs {}",
LIEF::PE::Section::MAX_SECTION_NAME, name.size());
return;
}
name_ = std::move(name);
}
optional<Section::ComdatInfo> Section::comdat_info() const {
// Implementation following MS documentation:
// https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#comdat-sections-object-only
if (!has_characteristic(Section::CHARACTERISTICS::LNK_COMDAT)) {
return nullopt();
}
// [...] The first symbol that has the section value of the COMDAT section
// must be the section symbol
if (symbols_.size() < 2) {
return nullopt();
}
ComdatInfo info;
{
const Symbol* sym = symbols_[0];
if (sym->value() != 0) {
return nullopt();
}
if (sym->base_type() != Symbol::BASE_TYPE::TY_NULL) {
return nullopt();
}
if (sym->storage_class() != Symbol::STORAGE_CLASS::STATIC) {
return nullopt();
}
if (sym->auxiliary_symbols().empty()) {
return nullopt();
}
const AuxiliarySymbol& aux = sym->auxiliary_symbols()[0];
const auto* aux_secdef = aux.as<AuxiliarySectionDefinition>();
if (aux_secdef == nullptr) {
return nullopt();
}
info.kind = aux_secdef->selection();
}
info.symbol = symbols_[1];
return info;
}
std::string Section::to_string() const {
using namespace fmt;
static constexpr auto WIDTH = 24;
std::ostringstream os;
const auto& list = 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 COFF::to_string(c); });
std::vector<std::string> fullname_hex;
fullname_hex.reserve(name().size());
std::transform(fullname().begin(), fullname().end(),
std::back_inserter(fullname_hex),
[] (const char c) { return format("{:02x}", c); });
os << format("{:{}} {} ({})\n", "Name:", WIDTH, name(),
fmt::to_string(join(fullname_hex, " ")));
os << format("{:{}} 0x{:x}\n", "Virtual Size", WIDTH, virtual_size())
<< format("{:{}} 0x{:x}\n", "Virtual Address", WIDTH, virtual_address())
<< format("{:{}} 0x{:x}\n", "Size of raw data", WIDTH, sizeof_raw_data())
<< format("{:{}} 0x{:x}\n", "Pointer to raw data", WIDTH, pointerto_raw_data())
<< format("{:{}} [0x{:08x}, 0x{:08x}]\n", "Range", WIDTH,
pointerto_raw_data(), pointerto_raw_data() + sizeof_raw_data())
<< format("{:{}} 0x{:x}\n", "Pointer to relocations", WIDTH, pointerto_relocation())
<< format("{:{}} 0x{:x}\n", "Pointer to line numbers", WIDTH, pointerto_line_numbers())
<< format("{:{}} 0x{:x}\n", "Number of relocations", WIDTH, numberof_relocations())
<< format("{:{}} 0x{:x}\n", "Number of lines", WIDTH, numberof_line_numbers())
<< format("{:{}} {}", "Characteristics", WIDTH, fmt::to_string(join(list_str, ", ")));
return os.str();
}
}