13 CollectCodeRelocs(BYTE *pe_buffer,
size_t buffer_size, IN
bool _is64bit, OUT std::set<ULONGLONG> &_relocs)
24 ULONGLONG reloc_addr = (relocField - (ULONGLONG)
peBuffer);
25 const bool is_in_code = (reloc_addr >=
codeSec->VirtualAddress) && (reloc_addr < codeSec->Misc.VirtualSize);
32 ULONGLONG* relocateAddr = (ULONGLONG*)((ULONG_PTR)relocField);
33 rva = (*relocateAddr);
37 DWORD* relocateAddr = (DWORD*)((ULONG_PTR)relocField);
38 rva = ULONGLONG(*relocateAddr);
48 for (
size_t i = 0; i < sec_count; i++) {
51 if (hdr->VirtualAddress == 0 || hdr->SizeOfRawData == 0) {
54 if (hdr->Characteristics & IMAGE_SCN_MEM_EXECUTE) {
72 if (moduleSize == 0) {
75 if (moduleSize == 0)
return 0;
78 std::set<ULONGLONG> relocs;
83 if (relocs.size() == 0) {
91 const ULONGLONG mask = ~ULONGLONG(0xFFFF);
92 std::map<ULONGLONG, size_t>base_candidates;
94 std::set<ULONGLONG>::iterator itr = relocs.begin();
96 for (itr = relocs.begin(); itr != relocs.end(); ++itr) {
97 const ULONGLONG guessed_base = (*itr) & mask;
98 std::map<ULONGLONG, size_t>::iterator found = base_candidates.find(guessed_base);
99 if (found == base_candidates.end()) {
100 base_candidates[guessed_base] = 0;
102 base_candidates[guessed_base]++;
104 ULONGLONG most_freqent = 0;
106 std::map<ULONGLONG, size_t>::iterator mapItr;
107 for (mapItr = base_candidates.begin(); mapItr != base_candidates.end(); ++mapItr) {
108 if (mapItr->second >= max_freq) {
109 most_freqent = mapItr->first;
110 max_freq = mapItr->second;
113 for (itr = relocs.begin(); itr != relocs.end(); ++itr) {
114 ULONGLONG first = *itr;
115 ULONGLONG first_base = first & mask;
116 if (first_base > most_freqent) {
119 ULONGLONG delta = most_freqent - first_base;
120 if (delta < moduleSize) {
CollectCodeRelocs(BYTE *pe_buffer, size_t buffer_size, IN bool _is64bit, OUT std::set< ULONGLONG > &_relocs)
virtual bool processRelocField(ULONG_PTR relocField)
static PIMAGE_SECTION_HEADER getCodeSection(BYTE *peBuffer, size_t bufferSize)
PIMAGE_SECTION_HEADER codeSec
std::set< ULONGLONG > & relocs
Functions related to finding a base to which the module was relocated.
ULONGLONG find_base_candidate(IN BYTE *module_ptr, IN size_t module_size)
PIMAGE_SECTION_HEADER get_section_hdr(IN const BYTE *pe_buffer, IN const size_t buffer_size, IN size_t section_num)
DWORD get_image_size(IN const BYTE *payload)
bool process_relocation_table(IN PVOID modulePtr, IN SIZE_T moduleSize, IN RelocBlockCallback *callback)
bool is64bit(IN const BYTE *pe_buffer)
size_t get_sections_count(IN const BYTE *buffer, IN const size_t buffer_size)
Wrappers over various fields in the PE header. Read, write, parse PE headers.
Operating on PE file's relocations table.