Index: /mpich2/trunk/winconfigure.wsf
===================================================================
--- /mpich2/trunk/winconfigure.wsf (revision 4852)
+++ /mpich2/trunk/winconfigure.wsf (revision 4862)
@@ -6884,6 +6884,6 @@
 	mfile.WriteLine("# impgen")
 	mfile.WriteLine
-	mfile.WriteLine("$(GCC_OUTDIR)\gccimpgen.obj: ..\maint\gccimpgen.c")
-	mfile.WriteLine("	$(cc) $(cflags) /Fo""$(GCC_OUTDIR)\\"" /Fd""$(GCC_OUTDIR)\\"" ..\maint\gccimpgen.c")
+	mfile.WriteLine("$(GCC_OUTDIR)\gccimpgen.obj: ..\maint\gccimpgen.cpp")
+	mfile.WriteLine("	$(cc) $(cflags) /Fo""$(GCC_OUTDIR)\\"" /Fd""$(GCC_OUTDIR)\\"" ..\maint\gccimpgen.cpp")
 	mfile.WriteLine
 	mfile.WriteLine("$(GCC_OUTDIR)\impgen.exe: $(GCC_OUTDIR)\gccimpgen.obj")
Index: /mpich2/trunk/maint/impgen.vcproj
===================================================================
--- /mpich2/trunk/maint/impgen.vcproj (revision 3902)
+++ /mpich2/trunk/maint/impgen.vcproj (revision 4862)
@@ -258,5 +258,5 @@
 			>
 			<File
-				RelativePath=".\gccimpgen.c"
+				RelativePath=".\gccimpgen.cpp"
 				>
 			</File>
Index: /mpich2/trunk/maint/gccimpgen.cpp
===================================================================
--- /mpich2/trunk/maint/gccimpgen.cpp (revision 4862)
+++ /mpich2/trunk/maint/gccimpgen.cpp (revision 4862)
@@ -0,0 +1,132 @@
+/* -*- Mode: C++; c-basic-offset:4 ; -*- */
+/*  
+ *  (C) 2001 by Argonne National Laboratory.
+ *      See COPYRIGHT in top-level directory.
+ */
+
+#include <windows.h>
+#include <stdio.h>
+#include <time.h>
+
+/* This function returns a pointer to the section header containing the RVA. Return NULL on error */
+template <typename NTHeaderType> static inline PIMAGE_SECTION_HEADER GetSecHeader(DWORD rva, NTHeaderType* pNTHeader)
+{
+    PIMAGE_SECTION_HEADER section;
+    section = IMAGE_FIRST_SECTION(pNTHeader);
+    for (int i=0; i < pNTHeader->FileHeader.NumberOfSections; i++, section++){
+		DWORD size = section->Misc.VirtualSize;
+        DWORD secRVA = section->VirtualAddress;
+        if((secRVA <= rva)  && (rva < (secRVA+size))){
+            return section;
+        }
+    }
+    return NULL;
+}
+
+/* This function returns the real address from the RVA. Returns NULL on error */
+template <typename NTHeaderType> static inline LPVOID GetRealAddrFromRVA(DWORD rva, NTHeaderType* pNTHeader, PBYTE imageBase )
+{
+	PIMAGE_SECTION_HEADER pSectionHdr = GetSecHeader(rva, pNTHeader);
+	if (!pSectionHdr){
+		return NULL;
+    }
+    /* Since we map the DLL manually we need to calculate mapShift to get the real addr */
+	INT mapShift = (INT)(pSectionHdr->VirtualAddress - pSectionHdr->PointerToRawData);
+
+	return (PVOID)(imageBase + rva - mapShift);
+}
+
+/* This function reads the exported symbols from a dll/exe and prints a DLL definition file, compatible with gcc,
+   with those symbols.
+    INPUT:
+        pImageBase  : Pointer to the base of the dll image, offset = 0
+        pNTHeader   : Pointer to the NT header (32-bit/64-bit) section of image.
+*/
+template <typename NTHeaderType> void PrintDefFileWithExpSymbols(PBYTE pImageBase, NTHeaderType *pNTHeader)
+{
+    /* Export Section RVA */
+    DWORD expSecRVA = pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
+
+    /* From RVA get the real addrs for the various components of the exports section */
+    PIMAGE_EXPORT_DIRECTORY pExportDir = (PIMAGE_EXPORT_DIRECTORY )GetRealAddrFromRVA(expSecRVA, pNTHeader, pImageBase);
+    PDWORD pszFuncNames =	(PDWORD ) GetRealAddrFromRVA( pExportDir->AddressOfNames, pNTHeader, pImageBase );
+
+    printf("EXPORTS\n");
+
+    for (unsigned int i=0; i < pExportDir->NumberOfFunctions; i++){
+        /* FIXME: Assumes that the current DLL does not have ordinal nums */
+        if(i < pExportDir->NumberOfNames){
+            /* Ordinal numbers start at 1 - not 0*/
+            printf("\t%s @ %d ;\n", GetRealAddrFromRVA(pszFuncNames[i], pNTHeader, pImageBase), i+1);
+        }
+        else{
+            break;
+        }
+    }
+}
+
+static void HelpUsage(char *exe_name)
+{
+    printf("Usage: %s <DLLNAME>\n", exe_name);
+    printf("\t\t The tool reads the export section from <DLLNAME> and prints the definition file for gcc to stdout\n");
+}
+
+int main(int argc, char *argv[])
+{
+    HANDLE hFile, hFileMapping;
+    LPVOID lpFileBase;
+    PIMAGE_DOS_HEADER   pImgHeader;
+    PIMAGE_NT_HEADERS   pImgNTHeader;
+    PIMAGE_NT_HEADERS64 pImgNTHeader64;
+
+    if((argc < 2) || (strcmp(argv[1], "/?") == 0) || (strcmp(argv[1], "--help") == 0)){
+        HelpUsage(argv[0]);
+        exit(1);
+    }
+    
+    /* Open the DLL and map it to memory */
+    hFile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
+    if(hFile == INVALID_HANDLE_VALUE){
+        printf("Unable to open dll, %s\n", argv[1]);
+        exit(-1);
+    }
+    
+    hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
+    if(hFileMapping == 0){
+        CloseHandle(hFile);
+        printf("Cannot open file mapping for dll, %s\n", argv[1]);
+        exit(-1);
+    }
+
+    lpFileBase = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
+    if(lpFileBase == 0){
+        CloseHandle(hFileMapping);
+        CloseHandle(hFile);
+        printf("Cannot map view of dll, %s\n", argv[1]);
+        exit(-1);
+    }
+
+    /* pImgHeader points to the MS-DOS HEADER section, offset=0, of dll */
+    pImgHeader = (PIMAGE_DOS_HEADER )lpFileBase;
+    
+    /* pImgNTHeader points to the NT HEADER section of the dll, pImgHeader + <File address of new exe header> */
+    pImgNTHeader = (PIMAGE_NT_HEADERS )((DWORD_PTR )pImgHeader + (DWORD_PTR )pImgHeader->e_lfanew);
+
+    /* In the case of the 64-bit dlls, the Optional header section within NT header has 64-bit fields */
+    pImgNTHeader64 = (PIMAGE_NT_HEADERS64) pImgNTHeader;
+
+    /* The Magic number is always a WORD. This number is used to determine if the dll is 32-bit or 64-bit */
+    if(pImgNTHeader->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC){
+        /* 64-bit dll */
+        PrintDefFileWithExpSymbols((PBYTE )pImgHeader, pImgNTHeader64);
+    }
+    else{
+        /* 32-bit dll */
+        PrintDefFileWithExpSymbols((PBYTE )pImgHeader, pImgNTHeader);
+    }
+
+    UnmapViewOfFile(lpFileBase);
+    CloseHandle(hFileMapping);
+    CloseHandle(hFile);
+}
+
Index: /ich2/trunk/maint/gccimpgen.c
===================================================================
--- /mpich2/trunk/maint/gccimpgen.c (revision 100)
+++  (revision )
@@ -1,127 +1,0 @@
-/* -*- Mode: C; c-basic-offset:4 ; -*- */
-/*  
- *  (C) 2001 by Argonne National Laboratory.
- *      See COPYRIGHT in top-level directory.
- */
-#include <stdio.h>
-#include <windows.h>
-
-unsigned int read16(HANDLE fd, int offset)
-{
-    DWORD num_read;
-    unsigned char b[2];
-
-    SetFilePointer(fd, offset, NULL, FILE_BEGIN);
-    if (!ReadFile(fd, b, 2, &num_read, NULL))
-    {
-	ExitProcess(-1);
-    }
-    return b[0] + (b[1] << 8);
-}
-
-unsigned int read32(HANDLE fd, int offset)
-{
-    DWORD num_read;
-    unsigned char b[4];
-
-    SetFilePointer(fd, offset, NULL, FILE_BEGIN);
-    if (!ReadFile(fd, b, 4, &num_read, NULL))
-    {
-	ExitProcess(-1);
-    }
-    return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
-}
-
-unsigned int to32(void *p)
-{
-    unsigned char *b = p;
-    return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
-}
-
-int main(int argc, char *argv[])
-{
-    DWORD num_read;
-    HANDLE fd;
-    unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
-    unsigned long export_rva, export_size, nsections, secptr, expptr;
-    unsigned long name_rvas, nexp;
-    unsigned char *expdata, *erva;
-    unsigned long name_rva;
-    unsigned long secptr1;
-    unsigned long vaddr;
-    unsigned long vsize;
-    unsigned long fptr;
-    char sname[8];
-
-    if (argc < 2)
-    {
-	printf("usage: %s <filename>.dll\n", argv[0]);
-	return 0;
-    }
-
-    fd = CreateFile(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY | FILE_FLAG_RANDOM_ACCESS, NULL);
-    if (fd == INVALID_HANDLE_VALUE)
-    {
-	return 1;
-    }
-
-    pe_header_offset = read32(fd, 0x3c);
-    opthdr_ofs = pe_header_offset + 4 + 20;
-    num_entries = read32(fd, opthdr_ofs + 92);
-
-    if (num_entries < 1)
-    {
-	/* no exports */
-	CloseHandle(fd);
-	return 1;
-    }
-
-    export_rva = read32(fd, opthdr_ofs + 96);
-    export_size = read32(fd, opthdr_ofs + 100);
-    nsections = read16(fd, pe_header_offset + 4 + 2);
-    secptr = (pe_header_offset + 4 + 20 + read16(fd, pe_header_offset + 4 + 16));
-
-    expptr = 0;
-    for (i = 0; i < nsections; i++)
-    {
-	secptr1 = secptr + 40 * i;
-	vaddr = read32(fd, secptr1 + 12);
-	vsize = read32(fd, secptr1 + 16);
-	fptr = read32(fd, secptr1 + 20);
-	SetFilePointer(fd, secptr1, NULL, FILE_BEGIN);
-	if (!ReadFile(fd, sname, 8, &num_read, NULL))
-	{
-	    ExitProcess(-1);
-	}
-	if (vaddr <= export_rva && vaddr+vsize > export_rva)
-	{
-	    expptr = fptr + (export_rva - vaddr);
-	    if (export_rva + export_size > vaddr + vsize)
-	    {
-		export_size = vsize - (export_rva - vaddr);
-	    }
-	    break;
-	}
-    }
-
-    expdata = (unsigned char*)malloc(export_size);
-    SetFilePointer(fd, expptr, NULL, FILE_BEGIN);
-    if (!ReadFile(fd, expdata, export_size, &num_read, NULL))
-    {
-	ExitProcess(-1);
-    }
-    erva = expdata - export_rva;
-
-    nexp = to32(expdata + 24);
-    name_rvas = to32(expdata + 32);
-
-    printf ("EXPORTS\n");
-    for (i = 0; i<nexp; i++)
-    {
-	name_rva = to32(erva + name_rvas + i * 4);
-	printf ("\t%s @ %ld ;\n", erva + name_rva, 1 + i);
-    }
-
-    CloseHandle(fd);
-    return 0;
-}
