aboutsummaryrefslogtreecommitdiff
path: root/tiny_impdef.c
blob: d287b16a05cbf56f5c0376dd67e7da4516fa605e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* -------------------------------------------------------------- */
/*
	"tiny_impdef creates a .def file from a dll"

	"Usage: tiny_impdef [-p] <library.dll> [-o outputfile]"
	"Options:"
	" -p print to stdout"
*/

#include <windows.h>
#include <stdio.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)
{
	/* Number of sections is indicated in file header. */
	return (int)
		((PIMAGE_FILE_HEADER)
			PEFHDROFFSET(lpFile))->NumberOfSections;
}


/* -------------------------------------------------------------- */

LPVOID  WINAPI ImageDirectoryOffset (
		LPVOID    lpFile,
		DWORD     dwIMAGE_DIRECTORY)
{
	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++;
		}

	if (i > nSections)
		return NULL;

	/* Return image import directory offset. */
	return (LPVOID)(((int)lpFile +
					 (int)VAImageDir - psh->VirtualAddress) +
					(int)psh->PointerToRawData);
}

/* -------------------------------------------------------------- */

BOOL    WINAPI GetSectionHdrByName (
	LPVOID                   lpFile,
	IMAGE_SECTION_HEADER     *sh,
	char                     *szSection)
{
	PIMAGE_SECTION_HEADER    psh;
	int                      nSections = NumOfSections (lpFile);
	int                      i;

	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++;
			}
		}

	return FALSE;
}

/* -------------------------------------------------------------- */

BOOL    WINAPI GetSectionHdrByAddress (
	LPVOID                   lpFile,
	IMAGE_SECTION_HEADER     *sh,
	DWORD                    addr)
{
	PIMAGE_SECTION_HEADER    psh;
	int                      nSections = NumOfSections (lpFile);
	int                      i;

	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;
				}
			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 ((ped = (PIMAGE_EXPORT_DIRECTORY)ImageDirectoryOffset
			(lpFile, IMAGE_DIRECTORY_ENTRY_EXPORT)) == NULL)
		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; }
	}
	*pDest = 0;

	return ped->NumberOfNames;
}

/* -------------------------------------------------------------- */

int main(int argc, char **argv)
{

	HANDLE hHeap; HANDLE hFile; HANDLE hMapObject; VOID *pMem;
	int nCnt, ret, argind, std;
	char *pNames;
	char infile[MAX_PATH];
	char buffer[MAX_PATH];
	char outfile[MAX_PATH];
	char libname[80];

	hHeap = NULL;
	hFile = NULL;
	hMapObject = NULL;
	pMem = NULL;
	infile[0] = 0;
	outfile[0] = 0;
	ret = 0;
	std = 0;

	for (argind = 1; argind < argc; ++argind)
	{
		const char *a = argv[argind];
		if ('-' == a[0])
		{
			if (0 == strcmp(a, "-p"))
				std = 1;
			else
			if (0 == strcmp(a, "-o"))
			{
				if (++argind == argc) goto usage;
				strcpy(outfile, argv[argind]);
			}
			else
				goto usage;
		}
		else
		if (0 == infile[0])
			strcpy(infile, a);
		else
			goto usage;
	}

	if (0 == infile[0])
	{
usage:
		fprintf(stderr,
			"tiny_impdef creates a .def file from a dll\n"
			"Usage: tiny_impdef [-p] <library.dll> [-o outputfile]\n"
			"Options:\n"
			" -p print to stdout\n"
			);
error:
		ret = 1;
		goto the_end;
	}

	if (SearchPath(NULL, infile, ".dll", sizeof buffer, buffer, NULL))
		strcpy(infile, buffer);

	if (0 == outfile[0])
	{
		char *p;
		p = strrchr(strcpy(outfile, infile), '\\');
		if (NULL == p)
		p = strrchr(outfile, '/');
		if (p) strcpy(outfile, p+1);

		p = strrchr(outfile, '.');
		if (NULL == p) p = strchr(outfile, 0);
		strcpy(p, ".def");
	}

	hFile=CreateFile(
		infile,
		GENERIC_READ,
		FILE_SHARE_READ,
		NULL,
		OPEN_EXISTING,
		0,
		NULL
		);

	if (hFile == INVALID_HANDLE_VALUE)
	{
		fprintf(stderr, "file not found: %s\n", infile);
		goto error;
	}

	if (!std) printf("--> %s\n", infile);

	hMapObject = CreateFileMapping(
		hFile,
		NULL,
		PAGE_READONLY,
		0, 0,
		NULL
		);

	if (NULL == hMapObject)
	{
		fprintf(stderr, "could not create file mapping.\n");
		goto error;
	}

	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 (NULL == pMem)
	{
		fprintf(stderr, "could not map view of file.\n");
		goto error;
	}

	hHeap = GetProcessHeap();
	nCnt = GetExportFunctionNames(pMem, hHeap, &pNames);
	{
		FILE *op; char *p; int n;
		if (!std) printf("<-- %s\n", outfile);

		if (std)
			op = stdout;
		else
			op = fopen(outfile, "wt");

		if (NULL == op)
		{
			fprintf(stderr, "could not create file: %s\n", outfile);
			goto error;
		}

		p = strrchr(infile, '\\');
		if (NULL == p)
		p = strrchr(infile, '/');
		if (NULL == p) p = infile; else ++p;

		fprintf(op, "LIBRARY %s\n\nEXPORTS", p);
		if (std) fprintf(op, " (%d)", nCnt);
		fprintf(op, "\n");
		for (n = 0, p = pNames; n < nCnt; ++n)
		{
			fprintf(op, "%s\n", p);
			while (*p++);
		}
		if (!std) fclose(op);
	}

the_end:
	if (pMem) UnmapViewOfFile(pMem);
	if (hMapObject) CloseHandle(hMapObject);
	if (hFile) CloseHandle(hFile);
	return ret;
}
/* -------------------------------------------------------------- */