aboutsummaryrefslogtreecommitdiff
path: root/win32/tools/tiny_impdef.c
diff options
context:
space:
mode:
Diffstat (limited to 'win32/tools/tiny_impdef.c')
-rw-r--r--win32/tools/tiny_impdef.c397
1 files changed, 119 insertions, 278 deletions
diff --git a/win32/tools/tiny_impdef.c b/win32/tools/tiny_impdef.c
index 040c53a..53fb255 100644
--- a/win32/tools/tiny_impdef.c
+++ b/win32/tools/tiny_impdef.c
@@ -23,201 +23,92 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
+#include <malloc.h>
+#include "../../config.h"
-/* Offset to PE file signature */
-#define NTSIGNATURE(a) ((LPVOID)((BYTE *)a + \
- ((PIMAGE_DOS_HEADER)a)->e_lfanew))
-
-/* MS-OS header identifies the NT PEFile signature dword;
- the PEFILE header exists just after that dword. */
-#define PEFHDROFFSET(a) ((LPVOID)((BYTE *)a + \
- ((PIMAGE_DOS_HEADER)a)->e_lfanew + \
- SIZE_OF_NT_SIGNATURE))
-
-/* PE optional header is immediately after PEFile header. */
-#define OPTHDROFFSET(a) ((LPVOID)((BYTE *)a + \
- ((PIMAGE_DOS_HEADER)a)->e_lfanew + \
- SIZE_OF_NT_SIGNATURE + \
- sizeof (IMAGE_FILE_HEADER)))
-
-/* Section headers are immediately after PE optional header. */
-#define SECHDROFFSET(a) ((LPVOID)((BYTE *)a + \
- ((PIMAGE_DOS_HEADER)a)->e_lfanew + \
- SIZE_OF_NT_SIGNATURE + \
- sizeof (IMAGE_FILE_HEADER) + \
- sizeof (IMAGE_OPTIONAL_HEADER)))
-
-
-#define SIZE_OF_NT_SIGNATURE 4
-
-/* -------------------------------------------------------------- */
-
-int WINAPI NumOfSections (
- LPVOID lpFile)
+int read_mem(FILE *fp, unsigned offset, void *buffer, unsigned len)
{
- /* Number of sections is indicated in file header. */
- return (int)
- ((PIMAGE_FILE_HEADER)
- PEFHDROFFSET(lpFile))->NumberOfSections;
+ fseek(fp, offset, 0);
+ return len == fread(buffer, 1, len, fp);
}
-
-/* -------------------------------------------------------------- */
-
-LPVOID WINAPI ImageDirectoryOffset (
- LPVOID lpFile,
- DWORD dwIMAGE_DIRECTORY)
+char *get_export_names(FILE *fp)
{
- PIMAGE_OPTIONAL_HEADER poh;
- PIMAGE_SECTION_HEADER psh;
- int nSections = NumOfSections (lpFile);
- int i = 0;
- LPVOID VAImageDir;
-
- /* Retrieve offsets to optional and section headers. */
- poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile);
- psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile);
-
- /* Must be 0 thru (NumberOfRvaAndSizes-1). */
- if (dwIMAGE_DIRECTORY >= poh->NumberOfRvaAndSizes)
- return NULL;
-
- /* Locate image directory's relative virtual address. */
- VAImageDir = (LPVOID)poh->DataDirectory[dwIMAGE_DIRECTORY].VirtualAddress;
-
- /* Locate section containing image directory. */
- while (i++<nSections)
- {
- if (psh->VirtualAddress <= (DWORD)VAImageDir
- && psh->VirtualAddress + psh->SizeOfRawData > (DWORD)VAImageDir)
- break;
- psh++;
- }
+ int l, i, n, n0;
+ char *p;
- if (i > nSections)
- return NULL;
+ IMAGE_SECTION_HEADER ish;
+ IMAGE_EXPORT_DIRECTORY ied;
+ IMAGE_DOS_HEADER dh;
+ IMAGE_FILE_HEADER ih;
+ DWORD sig, ref, addr, ptr, namep;
+#ifdef TCC_TARGET_X86_64
+ IMAGE_OPTIONAL_HEADER64 oh;
+ const int MACHINE = 0x8664;
+#else
+ IMAGE_OPTIONAL_HEADER32 oh;
+ const int MACHINE = 0x014C;
+#endif
+ int pef_hdroffset, opt_hdroffset, sec_hdroffset;
- /* Return image import directory offset. */
- return (LPVOID)(((int)lpFile +
- (int)VAImageDir - psh->VirtualAddress) +
- (int)psh->PointerToRawData);
-}
+ n = n0 = 0;
+ p = NULL;
-/* -------------------------------------------------------------- */
+ if (!read_mem(fp, 0, &dh, sizeof dh))
+ goto the_end;
+ if (!read_mem(fp, dh.e_lfanew, &sig, sizeof sig))
+ goto the_end;
+ if (sig != 0x00004550)
+ goto the_end;
+ pef_hdroffset = dh.e_lfanew + sizeof sig;
+ if (!read_mem(fp, pef_hdroffset, &ih, sizeof ih))
+ goto the_end;
+ if (MACHINE != ih.Machine)
+ goto the_end;
+ opt_hdroffset = pef_hdroffset + sizeof ih;
+ sec_hdroffset = opt_hdroffset + sizeof oh;
+ if (!read_mem(fp, opt_hdroffset, &oh, sizeof oh))
+ goto the_end;
-BOOL WINAPI GetSectionHdrByName (
- LPVOID lpFile,
- IMAGE_SECTION_HEADER *sh,
- char *szSection)
-{
- PIMAGE_SECTION_HEADER psh;
- int nSections = NumOfSections (lpFile);
- int i;
+ if (IMAGE_DIRECTORY_ENTRY_EXPORT >= oh.NumberOfRvaAndSizes)
+ goto the_end;
- if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != NULL)
- {
- /* find the section by name */
- for (i=0; i<nSections; i++)
- {
- if (!strcmp (psh->Name, szSection))
- {
- /* copy data to header */
- memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER));
- return TRUE;
- }
- else
- psh++;
- }
+ addr = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
+ //printf("addr: %08x\n", addr);
+ for (i = 0; i < ih.NumberOfSections; ++i) {
+ if (!read_mem(fp, sec_hdroffset + i * sizeof ish, &ish, sizeof ish))
+ goto the_end;
+ //printf("vaddr: %08x\n", ish.VirtualAddress);
+ if (addr >= ish.VirtualAddress && addr < ish.VirtualAddress + ish.SizeOfRawData)
+ goto found;
}
- return FALSE;
-}
-
-/* -------------------------------------------------------------- */
+ goto the_end;
-BOOL WINAPI GetSectionHdrByAddress (
- LPVOID lpFile,
- IMAGE_SECTION_HEADER *sh,
- DWORD addr)
-{
- PIMAGE_SECTION_HEADER psh;
- int nSections = NumOfSections (lpFile);
- int i;
+found:
+ ref = ish.VirtualAddress - ish.PointerToRawData;
+ if (!read_mem(fp, addr - ref, &ied, sizeof ied))
+ goto the_end;
- if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != NULL)
- {
- /* find the section by name */
- for (i=0; i<nSections; i++)
- {
- if (addr >= psh->VirtualAddress
- && addr < psh->VirtualAddress + psh->SizeOfRawData)
- {
- /* copy data to header */
- memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER));
- return TRUE;
+ namep = ied.AddressOfNames - ref;
+ for (i = 0; i < ied.NumberOfNames; ++i) {
+ if (!read_mem(fp, namep, &ptr, sizeof ptr))
+ goto the_end;
+ namep += sizeof ptr;
+ for (l = 0;;) {
+ if (n+1 >= n0)
+ p = realloc(p, n0 = n0 ? n0 * 2 : 256);
+ if (!read_mem(fp, ptr - ref + l, p + n, 1) || ++l >= 80) {
+ free(p), p = NULL;
+ goto the_end;
}
- else
- psh++;
- }
- }
- return FALSE;
-}
-
-/* -------------------------------------------------------------- */
-
-int WINAPI GetExportFunctionNames (
- LPVOID lpFile,
- HANDLE hHeap,
- char **pszFunctions)
-{
- IMAGE_SECTION_HEADER sh;
- PIMAGE_EXPORT_DIRECTORY ped;
- int *pNames, *pCnt;
- char *pSrc, *pDest;
- int i, nCnt;
- DWORD VAImageDir;
- PIMAGE_OPTIONAL_HEADER poh;
- char *pOffset;
-
- /* Get section header and pointer to data directory
- for .edata section. */
- if (NULL == (ped = (PIMAGE_EXPORT_DIRECTORY)
- ImageDirectoryOffset (lpFile, IMAGE_DIRECTORY_ENTRY_EXPORT)))
- return 0;
-
- poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile);
- VAImageDir = poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
-
- if (FALSE == GetSectionHdrByAddress (lpFile, &sh, VAImageDir))
- return 0;
-
- pOffset = (char *)lpFile + (sh.PointerToRawData - sh.VirtualAddress);
-
- pNames = (int *)(pOffset + (DWORD)ped->AddressOfNames);
-
- /* Figure out how much memory to allocate for all strings. */
- nCnt = 1;
- for (i=0, pCnt = pNames; i<(int)ped->NumberOfNames; i++)
- {
- pSrc = (pOffset + *pCnt++);
- if (pSrc)
- nCnt += strlen(pSrc)+1;
- }
-
- /* Allocate memory off heap for function names. */
- pDest = *pszFunctions = HeapAlloc (hHeap, HEAP_ZERO_MEMORY, nCnt);
-
- /* Copy all strings to buffer. */
- for (i=0, pCnt = pNames; i<(int)ped->NumberOfNames; i++)
- {
- pSrc = (pOffset + *pCnt++);
- if (pSrc) {
- strcpy(pDest, pSrc);
- pDest += strlen(pSrc)+1;
+ if (p[n++] == 0)
+ break;
}
}
- *pDest = 0;
-
- return ped->NumberOfNames;
+ if (p)
+ p[n] = 0;
+the_end:
+ return p;
}
/* -------------------------------------------------------------- */
@@ -238,61 +129,49 @@ static char *file_basename(const char *name)
int main(int argc, char **argv)
{
- HANDLE hHeap;
- HANDLE hFile;
- HANDLE hMapObject;
- VOID *pMem;
-
- int nCnt, ret, n;
- char *pNames;
+ int ret, v, i;
char infile[MAX_PATH];
- char buffer[MAX_PATH];
char outfile[MAX_PATH];
- FILE *op;
- char *p;
- hHeap = NULL;
- hFile = NULL;
- hMapObject = NULL;
- pMem = NULL;
+ static const char *ext[] = { ".dll", ".exe", NULL };
+ const char *file, **pp;
+ char path[MAX_PATH], *p, *q;
+ FILE *fp, *op;
+
infile[0] = 0;
outfile[0] = 0;
+ fp = op = NULL;
+ v = 0;
ret = 1;
- for (n = 1; n < argc; ++n)
- {
- const char *a = argv[n];
+ for (i = 1; i < argc; ++i) {
+ const char *a = argv[i];
if ('-' == a[0]) {
- if (0 == strcmp(a, "-o")) {
- if (++n == argc)
+ if (0 == strcmp(a, "-v")) {
+ v = 1;
+ } else if (0 == strcmp(a, "-o")) {
+ if (++i == argc)
goto usage;
- strcpy(outfile, argv[n]);
- }
- else
+ strcpy(outfile, argv[i]);
+ } else
goto usage;
-
} else if (0 == infile[0])
strcpy(infile, a);
else
goto usage;
}
- if (0 == infile[0])
- {
+ if (0 == infile[0]) {
usage:
fprintf(stderr,
- "tiny_impdef creates an export definition file (.def) from a dll\n"
+ "tiny_impdef: create export definition file (.def) from a dll\n"
"Usage: tiny_impdef library.dll [-o outputfile]\n"
);
goto the_end;
}
- if (SearchPath(NULL, infile, ".dll", sizeof buffer, buffer, NULL))
- strcpy(infile, buffer);
-
if (0 == outfile[0])
{
- char *p;
strcpy(outfile, file_basename(infile));
p = strrchr(outfile, '.');
if (NULL == p)
@@ -300,93 +179,55 @@ usage:
strcpy(p, ".def");
}
- hFile = CreateFile(
- infile,
- GENERIC_READ,
- FILE_SHARE_READ,
- NULL,
- OPEN_EXISTING,
- 0,
- NULL
- );
+ file = infile;
+#ifdef _WIN32
+ for (pp = ext; *pp; ++pp)
+ if (SearchPath(NULL, file, *pp, sizeof path, path, NULL)) {
+ file = path;
+ break;
+ }
+#endif
- if (hFile == INVALID_HANDLE_VALUE)
- {
- fprintf(stderr, "No such file: %s\n", infile);
+ fp = fopen(file, "rb");
+ if (NULL == fp) {
+ fprintf(stderr, "tiny_impdef: no such file: %s\n", infile);
goto the_end;
}
-
- hMapObject = CreateFileMapping(
- hFile,
- NULL,
- PAGE_READONLY,
- 0, 0,
- NULL
- );
-
- if (NULL == hMapObject)
- {
- fprintf(stderr, "Could not create file mapping: %s\n", infile);
+ op = fopen(outfile, "w");
+ if (NULL == op) {
+ fprintf(stderr, "tiny_impdef: could not create output file: %s\n", outfile);
goto the_end;
}
- pMem = MapViewOfFile(
- hMapObject, // object to map view of
- FILE_MAP_READ, // read access
- 0, // high offset: map from
- 0, // low offset: beginning
- 0); // default: map entire file
+ if (v)
+ printf("--> %s\n", infile);
- if (NULL == pMem)
- {
- fprintf(stderr, "Could not map view of file: %s\n", infile);
+ fprintf(op, "LIBRARY %s\n\nEXPORTS\n", file_basename(infile));
+ p = get_export_names(fp);
+ if (NULL == p) {
+ fprintf(stderr, "tiny_impdef: could not get exported function names.\n");
goto the_end;
}
- if (0 != strncmp(NTSIGNATURE(pMem), "PE", 2))
- {
- fprintf(stderr, "Not a PE file: %s\n", infile);
- goto the_end;
+ for (q = p, i = 0; *q; ++i) {
+ fprintf(op, "%s\n", q);
+ q += strlen(q) + 1;
}
+ free(p);
-
- hHeap = GetProcessHeap();
- nCnt = GetExportFunctionNames(pMem, hHeap, &pNames);
- if (0 == nCnt) {
- fprintf(stderr, "Could not get exported function names: %s\n", infile);
- goto the_end;
+ if (v) {
+ printf("<-- %s\n", outfile);
+ printf("%d symbol(s) found\n", i);
}
- printf("--> %s\n", infile);
-
- op = fopen(outfile, "w");
- if (NULL == op)
- {
- fprintf(stderr, "Could not create file: %s\n", outfile);
- goto the_end;
- }
-
- printf("<-- %s\n", outfile);
-
- fprintf(op, "LIBRARY %s\n\nEXPORTS\n", file_basename(infile));
- for (n = 0, p = pNames; n < nCnt; ++n)
- {
- fprintf(op, "%s\n", p);
- while (*p++);
- }
ret = 0;
the_end:
- if (pMem)
- UnmapViewOfFile(pMem);
-
- if (hMapObject)
- CloseHandle(hMapObject);
-
- if (hFile)
- CloseHandle(hFile);
-
+ if (fp)
+ fclose(fp);
+ if (op)
+ fclose(op);
return ret;
}