2007-12-09 21:55:18 +03:00
|
|
|
/* -------------------------------------------------------------- */
|
|
|
|
/*
|
|
|
|
* tiny_impdef creates an export definition file (.def) from a dll
|
|
|
|
* on MS-Windows. Usage: tiny_impdef library.dll [-o outputfile]"
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005,2007 grischka
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
2009-07-19 00:07:25 +04:00
|
|
|
#ifndef TINY_IMPDEF_GET_EXPORT_NAMES_ONLY
|
|
|
|
|
2007-12-09 21:55:18 +03:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
2013-02-12 22:13:28 +04:00
|
|
|
#include <io.h>
|
2009-07-19 00:06:14 +04:00
|
|
|
#include <malloc.h>
|
2007-12-09 21:55:18 +03:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
char *get_export_names(int fd);
|
2009-07-19 00:07:25 +04:00
|
|
|
#define tcc_free free
|
|
|
|
#define tcc_realloc realloc
|
2007-12-09 21:55:18 +03:00
|
|
|
|
|
|
|
/* extract the basename of a file */
|
|
|
|
static char *file_basename(const char *name)
|
|
|
|
{
|
|
|
|
const char *p = strchr(name, 0);
|
|
|
|
while (p > name
|
|
|
|
&& p[-1] != '/'
|
|
|
|
&& p[-1] != '\\'
|
|
|
|
)
|
|
|
|
--p;
|
|
|
|
return (char*)p;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2009-07-19 00:06:14 +04:00
|
|
|
int ret, v, i;
|
2007-12-09 21:55:18 +03:00
|
|
|
char infile[MAX_PATH];
|
|
|
|
char outfile[MAX_PATH];
|
|
|
|
|
2009-07-19 00:06:14 +04:00
|
|
|
static const char *ext[] = { ".dll", ".exe", NULL };
|
|
|
|
const char *file, **pp;
|
|
|
|
char path[MAX_PATH], *p, *q;
|
|
|
|
FILE *fp, *op;
|
|
|
|
|
2007-12-09 21:55:18 +03:00
|
|
|
infile[0] = 0;
|
|
|
|
outfile[0] = 0;
|
2009-07-19 00:06:14 +04:00
|
|
|
fp = op = NULL;
|
|
|
|
v = 0;
|
2007-12-09 21:55:18 +03:00
|
|
|
ret = 1;
|
2010-01-14 22:55:51 +03:00
|
|
|
p = NULL;
|
2007-12-09 21:55:18 +03:00
|
|
|
|
2009-07-19 00:06:14 +04:00
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
const char *a = argv[i];
|
2007-12-09 21:55:18 +03:00
|
|
|
if ('-' == a[0]) {
|
2009-07-19 00:06:14 +04:00
|
|
|
if (0 == strcmp(a, "-v")) {
|
|
|
|
v = 1;
|
|
|
|
} else if (0 == strcmp(a, "-o")) {
|
|
|
|
if (++i == argc)
|
2007-12-09 21:55:18 +03:00
|
|
|
goto usage;
|
2009-07-19 00:06:14 +04:00
|
|
|
strcpy(outfile, argv[i]);
|
|
|
|
} else
|
2007-12-09 21:55:18 +03:00
|
|
|
goto usage;
|
|
|
|
} else if (0 == infile[0])
|
|
|
|
strcpy(infile, a);
|
|
|
|
else
|
|
|
|
goto usage;
|
|
|
|
}
|
|
|
|
|
2009-07-19 00:06:14 +04:00
|
|
|
if (0 == infile[0]) {
|
2007-12-09 21:55:18 +03:00
|
|
|
usage:
|
|
|
|
fprintf(stderr,
|
2009-07-19 00:06:14 +04:00
|
|
|
"tiny_impdef: create export definition file (.def) from a dll\n"
|
2007-12-09 21:55:18 +03:00
|
|
|
"Usage: tiny_impdef library.dll [-o outputfile]\n"
|
|
|
|
);
|
|
|
|
goto the_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == outfile[0])
|
|
|
|
{
|
|
|
|
strcpy(outfile, file_basename(infile));
|
2010-01-14 22:55:51 +03:00
|
|
|
q = strrchr(outfile, '.');
|
|
|
|
if (NULL == q)
|
|
|
|
q = strchr(outfile, 0);
|
|
|
|
strcpy(q, ".def");
|
2007-12-09 21:55:18 +03:00
|
|
|
}
|
|
|
|
|
2009-07-19 00:06:14 +04:00
|
|
|
file = infile;
|
2010-01-14 22:55:51 +03:00
|
|
|
|
2009-07-19 00:06:14 +04:00
|
|
|
#ifdef _WIN32
|
2010-01-14 22:55:51 +03:00
|
|
|
pp = ext;
|
|
|
|
do if (SearchPath(NULL, file, *pp, sizeof path, path, NULL)) {
|
|
|
|
file = path;
|
|
|
|
break;
|
|
|
|
} while (*pp++);
|
2009-07-19 00:06:14 +04:00
|
|
|
#endif
|
2007-12-09 21:55:18 +03:00
|
|
|
|
2009-07-19 00:06:14 +04:00
|
|
|
fp = fopen(file, "rb");
|
|
|
|
if (NULL == fp) {
|
|
|
|
fprintf(stderr, "tiny_impdef: no such file: %s\n", infile);
|
2007-12-09 21:55:18 +03:00
|
|
|
goto the_end;
|
|
|
|
}
|
2009-07-19 00:06:14 +04:00
|
|
|
if (v)
|
2010-01-14 22:55:51 +03:00
|
|
|
printf("--> %s\n", file);
|
2007-12-09 21:55:18 +03:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
p = get_export_names(fileno(fp));
|
2009-07-19 00:06:14 +04:00
|
|
|
if (NULL == p) {
|
|
|
|
fprintf(stderr, "tiny_impdef: could not get exported function names.\n");
|
2007-12-09 21:55:18 +03:00
|
|
|
goto the_end;
|
|
|
|
}
|
|
|
|
|
2010-01-14 22:55:51 +03:00
|
|
|
op = fopen(outfile, "w");
|
|
|
|
if (NULL == op) {
|
|
|
|
fprintf(stderr, "tiny_impdef: could not create output file: %s\n", outfile);
|
|
|
|
goto the_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(op, "LIBRARY %s\n\nEXPORTS\n", file_basename(file));
|
2009-07-19 00:06:14 +04:00
|
|
|
for (q = p, i = 0; *q; ++i) {
|
|
|
|
fprintf(op, "%s\n", q);
|
|
|
|
q += strlen(q) + 1;
|
2007-12-09 21:55:18 +03:00
|
|
|
}
|
|
|
|
|
2009-07-19 00:06:14 +04:00
|
|
|
if (v) {
|
|
|
|
printf("<-- %s\n", outfile);
|
|
|
|
printf("%d symbol(s) found\n", i);
|
2007-12-09 21:55:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
the_end:
|
2010-01-14 22:55:51 +03:00
|
|
|
if (p)
|
|
|
|
free(p);
|
2009-07-19 00:06:14 +04:00
|
|
|
if (fp)
|
|
|
|
fclose(fp);
|
|
|
|
if (op)
|
|
|
|
fclose(op);
|
2007-12-09 21:55:18 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
int read_mem(int fd, unsigned offset, void *buffer, unsigned len)
|
2009-07-19 00:07:25 +04:00
|
|
|
{
|
2013-02-12 22:13:28 +04:00
|
|
|
lseek(fd, offset, SEEK_SET);
|
|
|
|
return len == read(fd, buffer, len);
|
2009-07-19 00:07:25 +04:00
|
|
|
}
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
/* -------------------------------------------------------------- */
|
|
|
|
|
2009-07-19 00:07:25 +04:00
|
|
|
/* -------------------------------------------------------------- */
|
|
|
|
#endif
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
char *get_export_names(int fd)
|
2009-07-19 00:07:25 +04:00
|
|
|
{
|
|
|
|
int l, i, n, n0;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
n = n0 = 0;
|
|
|
|
p = NULL;
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
if (!read_mem(fd, 0, &dh, sizeof dh))
|
2009-07-19 00:07:25 +04:00
|
|
|
goto the_end;
|
2013-02-12 22:13:28 +04:00
|
|
|
if (!read_mem(fd, dh.e_lfanew, &sig, sizeof sig))
|
2009-07-19 00:07:25 +04:00
|
|
|
goto the_end;
|
|
|
|
if (sig != 0x00004550)
|
|
|
|
goto the_end;
|
|
|
|
pef_hdroffset = dh.e_lfanew + sizeof sig;
|
2013-02-12 22:13:28 +04:00
|
|
|
if (!read_mem(fd, pef_hdroffset, &ih, sizeof ih))
|
2009-07-19 00:07:25 +04:00
|
|
|
goto the_end;
|
|
|
|
if (MACHINE != ih.Machine)
|
|
|
|
goto the_end;
|
|
|
|
opt_hdroffset = pef_hdroffset + sizeof ih;
|
|
|
|
sec_hdroffset = opt_hdroffset + sizeof oh;
|
2013-02-12 22:13:28 +04:00
|
|
|
if (!read_mem(fd, opt_hdroffset, &oh, sizeof oh))
|
2009-07-19 00:07:25 +04:00
|
|
|
goto the_end;
|
|
|
|
|
|
|
|
if (IMAGE_DIRECTORY_ENTRY_EXPORT >= oh.NumberOfRvaAndSizes)
|
|
|
|
goto the_end;
|
|
|
|
|
|
|
|
addr = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
|
|
|
|
//printf("addr: %08x\n", addr);
|
|
|
|
for (i = 0; i < ih.NumberOfSections; ++i) {
|
2013-02-12 22:13:28 +04:00
|
|
|
if (!read_mem(fd, sec_hdroffset + i * sizeof ish, &ish, sizeof ish))
|
2009-07-19 00:07:25 +04:00
|
|
|
goto the_end;
|
|
|
|
//printf("vaddr: %08x\n", ish.VirtualAddress);
|
|
|
|
if (addr >= ish.VirtualAddress && addr < ish.VirtualAddress + ish.SizeOfRawData)
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
goto the_end;
|
|
|
|
|
|
|
|
found:
|
|
|
|
ref = ish.VirtualAddress - ish.PointerToRawData;
|
2013-02-12 22:13:28 +04:00
|
|
|
if (!read_mem(fd, addr - ref, &ied, sizeof ied))
|
2009-07-19 00:07:25 +04:00
|
|
|
goto the_end;
|
|
|
|
|
|
|
|
namep = ied.AddressOfNames - ref;
|
|
|
|
for (i = 0; i < ied.NumberOfNames; ++i) {
|
2013-02-12 22:13:28 +04:00
|
|
|
if (!read_mem(fd, namep, &ptr, sizeof ptr))
|
2009-07-19 00:07:25 +04:00
|
|
|
goto the_end;
|
|
|
|
namep += sizeof ptr;
|
|
|
|
for (l = 0;;) {
|
|
|
|
if (n+1 >= n0)
|
|
|
|
p = tcc_realloc(p, n0 = n0 ? n0 * 2 : 256);
|
2013-02-12 22:13:28 +04:00
|
|
|
if (!read_mem(fd, ptr - ref + l, p + n, 1) || ++l >= 80) {
|
2009-07-19 00:07:25 +04:00
|
|
|
tcc_free(p), p = NULL;
|
|
|
|
goto the_end;
|
|
|
|
}
|
|
|
|
if (p[n++] == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p)
|
|
|
|
p[n] = 0;
|
|
|
|
the_end:
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------- */
|