forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFPO.cpp
More file actions
104 lines (92 loc) · 3.31 KB
/
Copy pathFPO.cpp
File metadata and controls
104 lines (92 loc) · 3.31 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
/* 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/PE/debug/FPO.hpp"
#include "LIEF/BinaryStream/SpanStream.hpp"
#include "logging.hpp"
#include "PE/Structures.hpp"
namespace LIEF::PE {
namespace details {
// From um/winnt.h -- typedef struct _FPO_DATA { ... } FPO_DATA, *PFPO_DATA
struct FPO_DATA {
uint32_t ulOffStart; // offset 1st byte of function code
uint32_t cbProcSize; // # bytes in function
uint32_t cdwLocals; // # bytes in locals/4
uint16_t cdwParams; // # bytes in params/4
uint16_t cbProlog : 8; // # bytes in prolog
uint16_t cbRegs : 3; // # regs saved
uint16_t fHasSEH : 1; // TRUE if SEH in func
uint16_t fUseBP : 1; // TRUE if EBP has been allocated
uint16_t reserved : 1; // reserved for future use
uint16_t cbFrame : 2; // frame type
};
static_assert(sizeof(FPO_DATA) == 16, "Wrong size");
}
std::unique_ptr<FPO> FPO::parse(
const details::pe_debug& hdr, Section* section, span<uint8_t> payload)
{
SpanStream stream(payload);
auto fpo = std::make_unique<FPO>(hdr, section);
while (stream) {
auto entry = stream.read<details::FPO_DATA>();
if (!entry) {
break;
}
fpo->entries_.push_back({
/*rva=*/ entry->ulOffStart,
/*proc_size=*/ entry->cbProcSize,
/*nb_locals=*/entry->cdwLocals * 4,
/*parameters_size=*/(uint32_t)entry->cdwParams * 4,
/*prolog_size=*/(uint16_t)entry->cbProcSize,
/*nb_saved_regs=*/(uint16_t)entry->cbRegs,
/*use_seh=*/(bool)entry->fHasSEH,
/*use_bp=*/(bool)entry->fUseBP,
/*reserved=*/(uint16_t)entry->reserved,
/*type=*/(FRAME_TYPE)entry->cbFrame,
});
}
return fpo;
}
std::string FPO::entry_t::to_string() const {
using namespace fmt;
std::ostringstream os;
os << format("{:10} {:<13} {:<9} {:<8} {:<9} {:<5} {:<6} {:<8} {}",
format("0x{:08x}", rva),
proc_size, nb_locals, nb_saved_regs, prolog_size, use_bp ? 'Y' : 'N',
use_seh ? 'Y' : 'N', PE::to_string(type), parameters_size
);
return os.str();
}
std::string FPO::to_string() const {
using namespace fmt;
std::ostringstream os;
os << format("{:10} {:13} {:9} {:8} {:9} {:5} {:6} {:8} {}\n", "RVA", "Proc Size",
"Locals", "Regs", "Prolog", "BP", "SEH", "Type", "Params");
for (const entry_t& entry : entries()) {
os << entry.to_string() << '\n';
}
return os.str();
}
const char* to_string(FPO::FRAME_TYPE e) {
switch (e) {
default: return "UNKNOWN";
case FPO::FRAME_TYPE::FPO: return "FPO";
case FPO::FRAME_TYPE::TRAP: return "TRAP";
case FPO::FRAME_TYPE::TSS: return "TSS";
case FPO::FRAME_TYPE::NON_FPO: return "NON_FPO";
}
return "UNKNOWN";
}
}