没用的代码
C版本:
char szRelocMsg[] = "\n重定位表所处的节:%s , rva:%08X, foa:%08X\n";
char szRelocOneBlock[] = "\n\n重定位基地址: %08X\n重定位项数量: %d\n需要重定位的地址列表:\n";
char szRelocHighLow[] = "修正:%08X";
char szRelocNone[] = "对齐使用:%08X";
IMAGE_SECTION_HEADER * find_section(void * mem, DWORD rva) {
IMAGE_DOS_HEADER * dos = (IMAGE_DOS_HEADER *)mem;
IMAGE_NT_HEADERS * nt = (IMAGE_NT_HEADERS*)((DWORD)dos + dos->e_lfanew);
int n = nt->FileHeader.NumberOfSections;
IMAGE_SECTION_HEADER * sec = 0;
IMAGE_SECTION_HEADER * header = IMAGE_FIRST_SECTION(nt);
for (int i = 0; i < n; ++i) {
if (rva >= header->VirtualAddress && rva < header->VirtualAddress + header->SizeOfRawData) {
sec = header;
break;
}
header++;
}
return sec;
}
DWORD rva2foa(void * mem, DWORD rva) {
DWORD offset = 0;
IMAGE_SECTION_HEADER * sec = find_section(mem, rva);
if (sec) {
offset = rva - sec->VirtualAddress;
offset += sec->PointerToRawData;
}
return offset;
}
char * get_section_name(void * mem, DWORD rva) {
char * name = 0;
IMAGE_SECTION_HEADER * sec = find_section(mem, rva);
if (sec) {
name = (char*)sec->Name;
}
return name;
}
void printSection(void * mem) {
IMAGE_DOS_HEADER * dos = (IMAGE_DOS_HEADER *)mem;
IMAGE_NT_HEADERS * nt = (IMAGE_NT_HEADERS*)((DWORD)dos + dos->e_lfanew);
int n = nt->FileHeader.NumberOfSections;
DWORD dossize = sizeof(IMAGE_DOS_HEADER);
DWORD optsize= nt->FileHeader.SizeOfOptionalHeader;
DWORD ops = (DWORD)&((IMAGE_NT_HEADERS*)0)->OptionalHeader;
cout << "\n-----Header size:"<< optsize + ops +dossize + n * sizeof(IMAGE_SECTION_HEADER) << endl;
IMAGE_SECTION_HEADER * header = IMAGE_FIRST_SECTION(nt);
printf(szMsgSection);
for (int i = 0; i < n; ++i) {
printf(szSection, header->Name, header->Misc.VirtualSize, header->VirtualAddress,
header->SizeOfRawData, header->PointerToRawData, header->Characteristics);
header++;
}
}
void print_reloc(void * mem) {
IMAGE_DOS_HEADER * dos = (IMAGE_DOS_HEADER *)mem;
IMAGE_NT_HEADERS * nt = (IMAGE_NT_HEADERS *)((DWORD)dos + dos->e_lfanew);
DWORD relocRVA = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress;
if (0 == relocRVA) {
cout << "不包含重定位信息" << endl;
return;
}
DWORD relocFOA = rva2foa(mem, relocRVA);
DWORD relocMem = relocFOA + (DWORD)mem;
IMAGE_BASE_RELOCATION * reloc = (IMAGE_BASE_RELOCATION *)relocMem;
printf(szRelocMsg, get_section_name(mem, relocRVA), relocRVA, relocFOA);
int nBlock = 0;
WORD * addr = 0;
const WORD HighLow = 0x3000;
DWORD reloc_addr = 0;
WORD highbit = 0;
char buffer[128] = {0};
char * info = 0;
while (reloc->VirtualAddress) {
nBlock = (reloc->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / 2;
printf(szRelocOneBlock, reloc->VirtualAddress, nBlock);
addr = (WORD*)((DWORD)reloc + sizeof(IMAGE_BASE_RELOCATION));
for (int i = 0; i < nBlock; ++i) {
highbit = (*addr & 0xF000);
if (highbit== HighLow) {
reloc_addr = (*addr & 0x0fff);
reloc_addr += reloc->VirtualAddress;
info = szRelocHighLow;
}
else {
info = szRelocNone;
reloc_addr = 0;
}
sprintf(buffer,info,reloc_addr);
printf(buffer);
if (i != 0 && i % 4 == 0)
puts("\n");
++addr;
}
reloc = (IMAGE_BASE_RELOCATION *)addr;
}
}
void createFileMapAndPrint(TCHAR * szFileName) {
//没做异常处理
HANDLE hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0);
DWORD dwFileSize = 0;
if (INVALID_HANDLE_VALUE == hFile) {
cout << "create file failed" << endl;
return;
}
HANDLE hMap = CreateFileMapping(hFile, 0, PAGE_READONLY, 0, 0, 0);
if (0 == hMap) {
cout << "CreateFileMapping failed" << endl;
return;
}
void * mem = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
if (0 == mem) {
cout << "MapViewOfFile failed" << endl;
return;
}
//printRes(mem);
print_reloc(mem);
UnmapViewOfFile(mem);
CloseHandle(hMap);
CloseHandle(hFile);
}
int main()
{
TCHAR szFileName[] = L"C:/code/win32/Project3/Debug/win32asmDLL.dll";
createFileMapAndPrint(szFileName);
getchar();
return 0;
}