// PCIe. CPP: output base address relocation information // # include "stdafx. H "# include <windows. h> DWORD rva2offset (export pntheader, DWORD dwrva) {pimage_section_header ction = (pimage_section_header) (DWORD) pntheader + sizeof (image_nt_headers); For (INT I = 0; I <pntheader-> fileheader. numberofsections; I ++) {If (dwrva> = Direction [I]. virtualaddress & dwrva <(ction [I]. virtualaddress + ction [I]. s Izeofrawdata) {return direction ction [I]. pointertorawdata + (dwrva-direction ction [I]. virtualaddress) ;}} return 0;} int _ tmain (INT argc, _ tchar * argv []) {handle hfile = invalid_handle_value; handle hmapping = NULL; pvoid pbfile = NULL; _ Try {tchar sexepath [max_path] = {0}; _ getts_s (sexepath); hfile = createfile (sexepath, generic_all, file_pai_write, null, open_existing, null, null); hmapping = Createfilemapping (hfile, null, page_readwrite, 0, 0, null); pbfile = mapviewoffile (hmapping, file_map_all_access, 0, 0, 0 ); if (invalid_handle_value = hfile | null = hmapping | null = pbfile) {printf ("failed to create memory ing file \ r \ n"); Return-1 ;} pimage_dos_header pdosheader = (pimage_dos_header) pbfile; pimage_nt_headers pntheader = (pimage_nt_headers) (DWORD) pbfile + pdosheader-> e_lfanew ); If (0x00004550! = Pntheader-> signature) {printf ("invalid PE file \ r \ n"); Return-1;} DWORD dwrelocoffset = rva2offset (pntheader, pntheader-> optionalheader. datadirectory [image_directory_entry_basereloc]. virtualaddress); pimage_base_relocation preloc = (pimage_base_relocation) (DWORD) pbfile + dwrelocoffset); If (0 = preloc-> virtualaddress) {printf ("No relocated table \ r \ n"); Return-1;} while (preloc-> virtualaddres S) {printf ("Relocation Address: [0x % 08x] \ r \ n", preloc-> virtualaddress); For (DWORD I = 0; I <(preloc-> sizeofblock-sizeof (image_base_relocation)/2; I ++) {printf ("<% 04d>-0x % 08x \ n", I, preloc-> virtualaddress + (* (word *) (DWORD) preloc + sizeof (image_base_relocation) + I * 2) & 0x0fff);} preloc = (pimage_base_relocation) (DWORD) preloc + preloc-> sizeofblock);} return 0 ;} _ Finally {If (null! = Pbfile) {unmapviewoffile (pbfile);} If (null! = Hmapping) {closehandle (hmapping);} If (invalid_handle_value! = Hfile) {closehandle (hfile );}}}