Blame maint/gccimpgen.cpp

Packit 0848f5
/* -*- Mode: C++; c-basic-offset:4 ; -*- */
Packit 0848f5
/*  
Packit 0848f5
 *  (C) 2001 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
Packit 0848f5
#include <windows.h>
Packit 0848f5
#include <stdio.h>
Packit 0848f5
#include <time.h>
Packit 0848f5
Packit 0848f5
/* This function returns a pointer to the section header containing the RVA. Return NULL on error */
Packit 0848f5
template <typename NTHeaderType> static inline PIMAGE_SECTION_HEADER GetSecHeader(DWORD rva, NTHeaderType* pNTHeader)
Packit 0848f5
{
Packit 0848f5
    PIMAGE_SECTION_HEADER section;
Packit 0848f5
    section = IMAGE_FIRST_SECTION(pNTHeader);
Packit 0848f5
    for (int i=0; i < pNTHeader->FileHeader.NumberOfSections; i++, section++){
Packit 0848f5
		DWORD size = section->Misc.VirtualSize;
Packit 0848f5
        DWORD secRVA = section->VirtualAddress;
Packit 0848f5
        if((secRVA <= rva)  && (rva < (secRVA+size))){
Packit 0848f5
            return section;
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
    return NULL;
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
/* This function returns the real address from the RVA. Returns NULL on error */
Packit 0848f5
template <typename NTHeaderType> static inline LPVOID GetRealAddrFromRVA(DWORD rva, NTHeaderType* pNTHeader, PBYTE imageBase )
Packit 0848f5
{
Packit 0848f5
	PIMAGE_SECTION_HEADER pSectionHdr = GetSecHeader(rva, pNTHeader);
Packit 0848f5
	if (!pSectionHdr){
Packit 0848f5
		return NULL;
Packit 0848f5
    }
Packit 0848f5
    /* Since we map the DLL manually we need to calculate mapShift to get the real addr */
Packit 0848f5
	INT mapShift = (INT)(pSectionHdr->VirtualAddress - pSectionHdr->PointerToRawData);
Packit 0848f5
Packit 0848f5
	return (PVOID)(imageBase + rva - mapShift);
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
/* This function reads the exported symbols from a dll/exe and prints a DLL definition file, compatible with gcc,
Packit 0848f5
   with those symbols.
Packit 0848f5
    INPUT:
Packit 0848f5
        pImageBase  : Pointer to the base of the dll image, offset = 0
Packit 0848f5
        pNTHeader   : Pointer to the NT header (32-bit/64-bit) section of image.
Packit 0848f5
*/
Packit 0848f5
template <typename NTHeaderType> void PrintDefFileWithExpSymbols(PBYTE pImageBase, NTHeaderType *pNTHeader)
Packit 0848f5
{
Packit 0848f5
    /* Export Section RVA */
Packit 0848f5
    DWORD expSecRVA = pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
Packit 0848f5
Packit 0848f5
    /* From RVA get the real addrs for the various components of the exports section */
Packit 0848f5
    PIMAGE_EXPORT_DIRECTORY pExportDir = (PIMAGE_EXPORT_DIRECTORY )GetRealAddrFromRVA(expSecRVA, pNTHeader, pImageBase);
Packit 0848f5
    PDWORD pszFuncNames =	(PDWORD ) GetRealAddrFromRVA( pExportDir->AddressOfNames, pNTHeader, pImageBase );
Packit 0848f5
Packit 0848f5
    printf("EXPORTS\n");
Packit 0848f5
Packit 0848f5
    for (unsigned int i=0; i < pExportDir->NumberOfFunctions; i++){
Packit 0848f5
        /* FIXME: Assumes that the current DLL does not have ordinal nums */
Packit 0848f5
        if(i < pExportDir->NumberOfNames){
Packit 0848f5
            /* Ordinal numbers start at 1 - not 0*/
Packit 0848f5
            printf("\t%s @ %d ;\n", GetRealAddrFromRVA(pszFuncNames[i], pNTHeader, pImageBase), i+1);
Packit 0848f5
        }
Packit 0848f5
        else{
Packit 0848f5
            break;
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
static void HelpUsage(char *exe_name)
Packit 0848f5
{
Packit 0848f5
    printf("Usage: %s <DLLNAME>\n", exe_name);
Packit 0848f5
    printf("\t\t The tool reads the export section from <DLLNAME> and prints the definition file for gcc to stdout\n");
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
int main(int argc, char *argv[])
Packit 0848f5
{
Packit 0848f5
    HANDLE hFile, hFileMapping;
Packit 0848f5
    LPVOID lpFileBase;
Packit 0848f5
    PIMAGE_DOS_HEADER   pImgHeader;
Packit 0848f5
    PIMAGE_NT_HEADERS   pImgNTHeader;
Packit 0848f5
    PIMAGE_NT_HEADERS64 pImgNTHeader64;
Packit 0848f5
Packit 0848f5
    if((argc < 2) || (strcmp(argv[1], "/?") == 0) || (strcmp(argv[1], "--help") == 0)){
Packit 0848f5
        HelpUsage(argv[0]);
Packit 0848f5
        exit(1);
Packit 0848f5
    }
Packit 0848f5
    
Packit 0848f5
    /* Open the DLL and map it to memory */
Packit 0848f5
    hFile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
Packit 0848f5
    if(hFile == INVALID_HANDLE_VALUE){
Packit 0848f5
        printf("Unable to open dll, %s\n", argv[1]);
Packit 0848f5
        exit(-1);
Packit 0848f5
    }
Packit 0848f5
    
Packit 0848f5
    hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
Packit 0848f5
    if(hFileMapping == 0){
Packit 0848f5
        CloseHandle(hFile);
Packit 0848f5
        printf("Cannot open file mapping for dll, %s\n", argv[1]);
Packit 0848f5
        exit(-1);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    lpFileBase = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
Packit 0848f5
    if(lpFileBase == 0){
Packit 0848f5
        CloseHandle(hFileMapping);
Packit 0848f5
        CloseHandle(hFile);
Packit 0848f5
        printf("Cannot map view of dll, %s\n", argv[1]);
Packit 0848f5
        exit(-1);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    /* pImgHeader points to the MS-DOS HEADER section, offset=0, of dll */
Packit 0848f5
    pImgHeader = (PIMAGE_DOS_HEADER )lpFileBase;
Packit 0848f5
    
Packit 0848f5
    /* pImgNTHeader points to the NT HEADER section of the dll, pImgHeader + <File address of new exe header> */
Packit 0848f5
    pImgNTHeader = (PIMAGE_NT_HEADERS )((DWORD_PTR )pImgHeader + (DWORD_PTR )pImgHeader->e_lfanew);
Packit 0848f5
Packit 0848f5
    /* In the case of the 64-bit dlls, the Optional header section within NT header has 64-bit fields */
Packit 0848f5
    pImgNTHeader64 = (PIMAGE_NT_HEADERS64) pImgNTHeader;
Packit 0848f5
Packit 0848f5
    /* The Magic number is always a WORD. This number is used to determine if the dll is 32-bit or 64-bit */
Packit 0848f5
    if(pImgNTHeader->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC){
Packit 0848f5
        /* 64-bit dll */
Packit 0848f5
        PrintDefFileWithExpSymbols((PBYTE )pImgHeader, pImgNTHeader64);
Packit 0848f5
    }
Packit 0848f5
    else{
Packit 0848f5
        /* 32-bit dll */
Packit 0848f5
        PrintDefFileWithExpSymbols((PBYTE )pImgHeader, pImgNTHeader);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    UnmapViewOfFile(lpFileBase);
Packit 0848f5
    CloseHandle(hFileMapping);
Packit 0848f5
    CloseHandle(hFile);
Packit 0848f5
}
Packit 0848f5