BearParser
Portable Executable parsing library (from PE-bear)
Loading...
Searching...
No Matches
ExceptionDirWrapper.cpp
Go to the documentation of this file.
2#include "pe/PEFile.h"
3
4/*
5typedef struct _IMAGE_IA64_RUNTIME_FUNCTION_ENTRY {
6 DWORD BeginAddress;
7 DWORD EndAddress;
8 DWORD UnwindInfoAddress;
9} IMAGE_IA64_RUNTIME_FUNCTION_ENTRY, *PIMAGE_IA64_RUNTIME_FUNCTION_ENTRY;
10*/
11
16
17
19{
20 clear();
21 parsedSize = 0;
22 bufsize_t maxSize = getDirEntrySize(true);
23 if (maxSize == 0) return false; // nothing to parse
24
25 if (!getPtr()) return false;
26
27 size_t entrySize = 0;
28 if (this->m_Exe->getArch() == Executable::ARCH_INTEL) {
29 entrySize = sizeof(IMAGE_IA64_RUNTIME_FUNCTION_ENTRY);
30 }
31 else if (this->m_Exe->getArch() == Executable::ARCH_ARM && this->m_Exe->getBitMode() == 64) {
32 entrySize = 8;
33 }
34 size_t entryId = 0;
35 while (parsedSize < maxSize) {
36 ExceptionEntryWrapper* entry = new ExceptionEntryWrapper(this->m_Exe, this, entryId++);
37
38 if (entry->getPtr() == NULL) {
39 delete entry;
40 break;
41 }
42 this->parsedSize += entrySize;
43 this->entries.push_back(entry);
44 }
46 "Entries num = %lu, parsedSize = %lX",
47 static_cast<unsigned long>(entries.size()),
48 static_cast<unsigned long>(parsedSize)
49 );
50 return true;
51}
52
54{
55 size_t entrySize = 0;
56 if (this->m_Exe->getArch() == Executable::ARCH_INTEL) {
57 entrySize = sizeof(IMAGE_IA64_RUNTIME_FUNCTION_ENTRY);
58 }
59 else if (this->m_Exe->getArch() == Executable::ARCH_ARM && this->m_Exe->getBitMode() == 64) {
60 entrySize = sizeof(uint64_t);
61 }
62 const offset_t rva = getDirEntryAddress();
63 BYTE* first = m_Exe->getContentAt(rva, Executable::RVA, entrySize);
64 if (!first || !entrySize) {
65 return NULL;
66 }
67 return first;
68}
69/*
70IMAGE_IA64_RUNTIME_FUNCTION_ENTRY* ExceptionDirWrapper::exceptFunc64()
71{
72 offset_t rva = getDirEntryAddress();
73 BYTE *ptr = m_Exe->getContentAt(rva, Executable::RVA, sizeof(IMAGE_IA64_RUNTIME_FUNCTION_ENTRY));
74 if (ptr == NULL) return NULL;
75
76 IMAGE_IA64_RUNTIME_FUNCTION_ENTRY* exc = (IMAGE_IA64_RUNTIME_FUNCTION_ENTRY*) ptr;
77 return exc;
78}
79*/
80//----------------
81
83{
84 if (!this->parentDir) {
85 return NULL;
86 }
87 size_t entrySize = 0;
88 if (this->m_Exe->getArch() == Executable::ARCH_INTEL) {
89 entrySize = sizeof(IMAGE_IA64_RUNTIME_FUNCTION_ENTRY);
90 }
91 else if (this->m_Exe->getArch() == Executable::ARCH_ARM && this->m_Exe->getBitMode() == 64) {
92 entrySize = 8;
93 }
94 void* first = parentDir->getPtr();
95 if (!first || !entrySize) {
96 return NULL;
97 }
98
99 uint64_t firstOffset = this->getOffset(first);
100 uint64_t myOffset = firstOffset + this->entryNum * entrySize;
101
102 BYTE* ptr = m_Exe->getContentAt(myOffset, Executable::RAW, entrySize);
103 return ptr;
104}
105
107{
108 if (!this->parentDir) return 0;
109 if (!this->getPtr()) return 0;
110
111 if (this->m_Exe->getArch() == Executable::ARCH_INTEL) {
112 return sizeof(IMAGE_IA64_RUNTIME_FUNCTION_ENTRY);
113 }
114 if (this->m_Exe->getArch() == Executable::ARCH_ARM && this->m_Exe->getBitMode() == 64) {
115 return 8;
116 }
117 return 0;
118}
119
121{
122 if (this->m_Exe->getArch() == Executable::ARCH_INTEL) {
123 return FIELD_COUNTER;
124 }
125 else if (this->m_Exe->getArch() == Executable::ARCH_ARM && this->m_Exe->getBitMode() == 64) {
126 return 2;
127 }
128 return 0;
129}
130
131void* ExceptionEntryWrapper::getFieldPtr(size_t fieldId, size_t subField)
132{
133 void *ptr = this->getPtr();
134 if (!ptr) return nullptr;
135
136 if (this->m_Exe->getArch() == Executable::ARCH_INTEL) {
137 IMAGE_IA64_RUNTIME_FUNCTION_ENTRY* exc = (IMAGE_IA64_RUNTIME_FUNCTION_ENTRY*) ptr;
138 if (!exc) return NULL;
139
140 switch (fieldId) {
141 case BEGIN_ADDR : return &exc->BeginAddress;
142 case END_ADDR : return &exc->EndAddress;
143 case UNWIND_INFO_ADDR : return &exc->UnwindInfoAddress;
144 }
145 }
146 else if (this->m_Exe->getArch() == Executable::ARCH_ARM && this->m_Exe->getBitMode() == 64) {
148 if (!rec) return NULL;
149
150 switch (fieldId) {
151 case BEGIN_ADDR : return &rec->Start;
152 case END_ADDR : return &rec->Xdata;
153 }
154 }
155 return ptr;
156}
157
159{
160 if (this->m_Exe->getArch() == Executable::ARCH_INTEL) {
161 switch (fieldId) {
162 case BEGIN_ADDR : return "BeginAddress";
163 case END_ADDR : return "EndAddress";
164 case UNWIND_INFO_ADDR : return "UnwindInfoAddress";
165 }
166 return "";
167 }
168 else if (this->m_Exe->getArch() == Executable::ARCH_ARM && this->m_Exe->getBitMode() == 64) {
169 if (fieldId == BEGIN_ADDR) return "Start";
170 if (fieldId == END_ADDR) return "XData";
171 }
172 return getName();
173}
174
176{
177 if (this->m_Exe->getArch() == Executable::ARCH_INTEL) {
178 switch (fieldId) {
179 case BEGIN_ADDR :
180 case END_ADDR :
181 case UNWIND_INFO_ADDR :
182 return Executable::RVA;
183 }
184 }
185 else if (this->m_Exe->getArch() == Executable::ARCH_ARM && this->m_Exe->getBitMode() == 64) {
186 switch (fieldId) {
187 case BEGIN_ADDR :
188 case END_ADDR :
189 return Executable::RVA;
190 }
191 }
193}
194
uint32_t bufsize_t
uint64_t offset_t
struct _ARM_EXCEPT_RECORD ARM_EXCEPT_RECORD
bufsize_t getDirEntrySize(bool trimToExeSize=false)
offset_t getDirEntryAddress()
friend class ExceptionEntryWrapper
virtual void * getPtr()
virtual QString getName()
virtual void * getFieldPtr(size_t fieldId, size_t subField=FIELD_NONE)
@ BEGIN_ADDR
@ UNWIND_INFO_ADDR
@ FIELD_COUNTER
@ END_ADDR
virtual size_t getFieldsCount()
virtual Executable::addr_type containsAddrType(size_t fieldId, size_t subField)
virtual QString getFieldName(size_t fieldId)
virtual bufsize_t getSize()
virtual offset_t getOffset()
std::vector< ExeNodeWrapper * > entries
virtual void clear()
virtual exe_bits getBitMode()
Definition Executable.h:56
virtual exe_arch getArch()=0
BYTE * getContentAt(offset_t offset, bufsize_t size, bool allowExceptions=false)
Definition Executable.h:65
bool append(dbg_level lvl, const char *format,...)
Definition Util.cpp:8
@ D_INFO
Definition Util.h:26