丁同亚的博客
夺朱非正色

1.为什么有这个重定位表?

因为有些地址在编译的时候已经写死了,并不是相对imageBase的偏移,这样的话当程序加载到非imageBase的位置时,导致这些地址没法用,所以编译器搞了个重定位表,告诉加载程序那些地址是写死的,需要修改

修改的话:计算一下RVA,然后 计算出新的imageBase下的绝对地址就可以了.

 

 

AI牛逼,找了两个小时的bug,被 deepseek 2分钟找到了.低级bug

惭愧呀! TypeOffset在每次循环的时候没有初始化,导致的bug.

void* repairRectionTable(void* fileBuffer, int offset){

	PE pe = resolvePE(fileBuffer);

	int RVA = pe.dataDir[5].value;
	if (RVA == 0){
		printf("没有重定位表");
		return NULL;
	}

	IMAGE_BASE_RELOCATION* pRelocation = (IMAGE_BASE_RELOCATION*)(VA2FA(fileBuffer, RVA) + (int)fileBuffer);

	DWORD* tmp;
	int couter = 0;
	while (pRelocation->VirtualAddress)
	{
		WORD* TypeOffset = (WORD*)((int)pRelocation + 8);
		printf("\n VA:%X\n", pRelocation->VirtualAddress);
		int size = ((pRelocation->SizeOfBlock - 8) / 2);
		printf("\n size:%X\n", size);

		for (size_t i = 0; i < size; i++)
		{
			if (*TypeOffset >> 12 == 3){
				tmp = (DWORD*)(VA2FA(fileBuffer, (*TypeOffset & 0x0FFF) + pRelocation->VirtualAddress) + (int)fileBuffer);
				*tmp =*tmp  + offset;
				
			}
			couter++;
			TypeOffset+=1;
		}


		pRelocation = (IMAGE_BASE_RELOCATION*)((BYTE*)pRelocation + pRelocation->SizeOfBlock);
	}
	printf("\nnumber:%d\n", couter);
	return fileBuffer;
}

 

posted on 2025-03-16 18:25  丁同亚的博客  阅读(40)  评论(0)    收藏  举报