forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebug.cpp
More file actions
127 lines (111 loc) · 3.5 KB
/
Copy pathDebug.cpp
File metadata and controls
127 lines (111 loc) · 3.5 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
/* 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 "LIEF/Visitor.hpp"
#include "LIEF/PE/debug/Debug.hpp"
#include "LIEF/PE/Section.hpp"
#include "PE/Structures.hpp"
#include "frozen.hpp"
#include "logging.hpp"
#include "spdlog/fmt/fmt.h"
#include "overflow_check.hpp"
namespace LIEF {
namespace PE {
span<uint8_t> Debug::get_payload(Section& section, uint32_t /*rva*/,
uint32_t offset, uint32_t size)
{
span<uint8_t> content = section.writable_content();
if (size == 0 || content.empty() || content.size() < size) {
return {};
}
int32_t rel_offset = (int32_t)offset - (int32_t)section.offset();
if (rel_offset < 0) {
return {};
}
if ((size_t)rel_offset >= content.size() || (rel_offset + size) > content.size()) {
return {};
}
if (check_overflow<uint64_t>((uint32_t)rel_offset, size, content.size())) {
return {};
}
return content.subspan((uint32_t)rel_offset, size);
}
span<uint8_t> Debug::get_payload(Section& section, const details::pe_debug& hdr) {
return get_payload(section, hdr.AddressOfRawData, hdr.PointerToRawData,
hdr.SizeOfData);
}
Debug::Debug(const details::pe_debug& debug_s, Section* sec) :
type_{static_cast<TYPES>(debug_s.Type)},
characteristics_{debug_s.Characteristics},
timestamp_{debug_s.TimeDateStamp},
major_version_{debug_s.MajorVersion},
minor_version_{debug_s.MinorVersion},
sizeof_data_{debug_s.SizeOfData},
addressof_rawdata_{debug_s.AddressOfRawData},
pointerto_rawdata_{debug_s.PointerToRawData},
section_{sec}
{}
span<uint8_t> Debug::payload() {
if (section_ == nullptr) {
return {};
}
return get_payload(*section_, *this);
}
void Debug::accept(Visitor& visitor) const {
visitor.visit(*this);
}
std::string Debug::to_string() const {
using namespace fmt;
std::ostringstream os;
os << format("Characteristics: 0x{:x}\n", characteristics())
<< format("Timestamp: 0x{:x}\n", timestamp())
<< format("Major/Minor version: {}.{}\n", major_version(), minor_version())
<< format("Type: {}\n", PE::to_string(type()))
<< format("Size of data: 0x{:x}\n", sizeof_data())
<< format("Address of rawdata: 0x{:x}\n", addressof_rawdata())
<< format("Pointer to rawdata: 0x{:x}", pointerto_rawdata());
return os.str();
}
const char* to_string(Debug::TYPES e) {
#define ENTRY(X) std::pair(Debug::TYPES::X, #X)
STRING_MAP enums2str {
ENTRY(UNKNOWN),
ENTRY(COFF),
ENTRY(CODEVIEW),
ENTRY(FPO),
ENTRY(MISC),
ENTRY(EXCEPTION),
ENTRY(FIXUP),
ENTRY(OMAP_TO_SRC),
ENTRY(OMAP_FROM_SRC),
ENTRY(BORLAND),
ENTRY(RESERVED10),
ENTRY(CLSID),
ENTRY(VC_FEATURE),
ENTRY(POGO),
ENTRY(ILTCG),
ENTRY(MPX),
ENTRY(REPRO),
ENTRY(PDBCHECKSUM),
ENTRY(EX_DLLCHARACTERISTICS),
};
#undef ENTRY
if (const auto it = enums2str.find(e); it != enums2str.end()) {
return it->second;
}
return "UNKNOWN";
}
}
}