Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. PE file format + relocations

PE file format + relocations

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zildjohn01
    wrote on last edited by
    #1

    I'm writing a custom PE loader, and I finished everything except relocations, which I can't find anywhere in the EXE. I'm using Windows's notepad.exe as my initial test. Both the BASERELOC directory entry (DataDirectory[5]) and the PointerToRelocations field of the .text section are set to all 0's. Also, there is no ".reloc" section. Any ideas where these relocations would be located?

    Z S 2 Replies Last reply
    0
    • Z zildjohn01

      I'm writing a custom PE loader, and I finished everything except relocations, which I can't find anywhere in the EXE. I'm using Windows's notepad.exe as my initial test. Both the BASERELOC directory entry (DataDirectory[5]) and the PointerToRelocations field of the .text section are set to all 0's. Also, there is no ".reloc" section. Any ideas where these relocations would be located?

      Z Offline
      Z Offline
      zildjohn01
      wrote on last edited by
      #2

      Anyone? A possibility is that modern compilers assume the base image address will never change, and forego creating relocations. Can anyone confirm or deny this?

      1 Reply Last reply
      0
      • Z zildjohn01

        I'm writing a custom PE loader, and I finished everything except relocations, which I can't find anywhere in the EXE. I'm using Windows's notepad.exe as my initial test. Both the BASERELOC directory entry (DataDirectory[5]) and the PointerToRelocations field of the .text section are set to all 0's. Also, there is no ".reloc" section. Any ideas where these relocations would be located?

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        This program will list all the addresses in "Kernel32.dll" that would need relocating if it didn't load at its preferred base address:

        // Relocations.cpp : Defines the entry point for the console application.
        //
         
        #include "stdafx.h"
        #include <iostream>
        #include <windows.h>
        using namespace std;
         
        int main(int argc, char* argv[])
        {
        // First get the DOS header.
        char *pBase = (char*)GetModuleHandle("kernel32.dll");
        PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)pBase;
        assert(pDOS->e_magic == IMAGE_DOS_SIGNATURE);
         
        // Get the NT headers.
        PIMAGE_NT_HEADERS pNT = (PIMAGE_NT_HEADERS)(pBase + pDOS->e_lfanew);
        assert(pNT->Signature == IMAGE_NT_SIGNATURE);
         
        // Now get the "optional" header.
        PIMAGE_OPTIONAL_HEADER pOpt = &(IMAGE_OPTIONAL_HEADER)(pNT->OptionalHeader);
        assert(pOpt->Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC);
         
        // Get a pointer to the relocation table and its size.
        PIMAGE_DATA_DIRECTORY pRelocDD = &(pOpt->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]);
        DWORD SizeOfRelocs = pRelocDD->Size;
        PIMAGE_BASE_RELOCATION pReloc = (PIMAGE_BASE_RELOCATION)(pBase + pRelocDD->VirtualAddress);
         
        for (DWORD ToGo = SizeOfRelocs; ToGo>0; /*Empty*/)
        {
        WORD *pRelocArray = (WORD*)(pReloc + 1);
        DWORD NumRelocs = (pReloc->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(WORD);
         
        for (DWORD i=0; i<NumRelocs; ++i)
        {
        WORD val = pRelocArray[i];
        WORD Type = val >> 12;
        WORD RVA = val & 0x0FFF;
         
        cout << hex << "Type = " << Type << ", Address = " << (DWORD)(pBase + pReloc->VirtualAddress + RVA) << endl;
        }
         
        ToGo -= pReloc->SizeOfBlock;
        pReloc = (PIMAGE_BASE_RELOCATION)((char*)pReloc + pReloc->SizeOfBlock);
        }
         
        return 0;
        }

        See here[^] for for information.

        Steve

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups