tccpe: load dll on the fly
[tinycc.git] / win32 / tools / tiny_impdef.c
blobb024a9003eff313453b0e83b10bca0ed112e6b88
1 /* -------------------------------------------------------------- */
2 /*
3 * tiny_impdef creates an export definition file (.def) from a dll
4 * on MS-Windows. Usage: tiny_impdef library.dll [-o outputfile]"
5 *
6 * Copyright (c) 2005,2007 grischka
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #ifndef TINY_IMPDEF_GET_EXPORT_NAMES_ONLY
25 #define WIN32_LEAN_AND_MEAN
26 #include <windows.h>
27 #include <stdio.h>
28 #include <malloc.h>
29 #include "../../config.h"
31 char *get_export_names(FILE *fp);
32 #define tcc_free free
33 #define tcc_realloc realloc
35 /* extract the basename of a file */
36 static char *file_basename(const char *name)
38 const char *p = strchr(name, 0);
39 while (p > name
40 && p[-1] != '/'
41 && p[-1] != '\\'
43 --p;
44 return (char*)p;
47 int main(int argc, char **argv)
49 int ret, v, i;
50 char infile[MAX_PATH];
51 char outfile[MAX_PATH];
53 static const char *ext[] = { ".dll", ".exe", NULL };
54 const char *file, **pp;
55 char path[MAX_PATH], *p, *q;
56 FILE *fp, *op;
58 infile[0] = 0;
59 outfile[0] = 0;
60 fp = op = NULL;
61 v = 0;
62 ret = 1;
64 for (i = 1; i < argc; ++i) {
65 const char *a = argv[i];
66 if ('-' == a[0]) {
67 if (0 == strcmp(a, "-v")) {
68 v = 1;
69 } else if (0 == strcmp(a, "-o")) {
70 if (++i == argc)
71 goto usage;
72 strcpy(outfile, argv[i]);
73 } else
74 goto usage;
75 } else if (0 == infile[0])
76 strcpy(infile, a);
77 else
78 goto usage;
81 if (0 == infile[0]) {
82 usage:
83 fprintf(stderr,
84 "tiny_impdef: create export definition file (.def) from a dll\n"
85 "Usage: tiny_impdef library.dll [-o outputfile]\n"
87 goto the_end;
90 if (0 == outfile[0])
92 strcpy(outfile, file_basename(infile));
93 p = strrchr(outfile, '.');
94 if (NULL == p)
95 p = strchr(outfile, 0);
96 strcpy(p, ".def");
99 file = infile;
100 #ifdef _WIN32
101 for (pp = ext; *pp; ++pp)
102 if (SearchPath(NULL, file, *pp, sizeof path, path, NULL)) {
103 file = path;
104 break;
106 #endif
108 fp = fopen(file, "rb");
109 if (NULL == fp) {
110 fprintf(stderr, "tiny_impdef: no such file: %s\n", infile);
111 goto the_end;
114 op = fopen(outfile, "w");
115 if (NULL == op) {
116 fprintf(stderr, "tiny_impdef: could not create output file: %s\n", outfile);
117 goto the_end;
120 if (v)
121 printf("--> %s\n", infile);
123 fprintf(op, "LIBRARY %s\n\nEXPORTS\n", file_basename(infile));
124 p = get_export_names(fp);
125 if (NULL == p) {
126 fprintf(stderr, "tiny_impdef: could not get exported function names.\n");
127 goto the_end;
130 for (q = p, i = 0; *q; ++i) {
131 fprintf(op, "%s\n", q);
132 q += strlen(q) + 1;
134 free(p);
136 if (v) {
137 printf("<-- %s\n", outfile);
138 printf("%d symbol(s) found\n", i);
141 ret = 0;
143 the_end:
144 if (fp)
145 fclose(fp);
146 if (op)
147 fclose(op);
148 return ret;
151 /* -------------------------------------------------------------- */
153 int read_mem(FILE *fp, unsigned offset, void *buffer, unsigned len)
155 fseek(fp, offset, 0);
156 return len == fread(buffer, 1, len, fp);
159 /* -------------------------------------------------------------- */
160 #endif
162 char *get_export_names(FILE *fp)
164 int l, i, n, n0;
165 char *p;
167 IMAGE_SECTION_HEADER ish;
168 IMAGE_EXPORT_DIRECTORY ied;
169 IMAGE_DOS_HEADER dh;
170 IMAGE_FILE_HEADER ih;
171 DWORD sig, ref, addr, ptr, namep;
172 #ifdef TCC_TARGET_X86_64
173 IMAGE_OPTIONAL_HEADER64 oh;
174 const int MACHINE = 0x8664;
175 #else
176 IMAGE_OPTIONAL_HEADER32 oh;
177 const int MACHINE = 0x014C;
178 #endif
179 int pef_hdroffset, opt_hdroffset, sec_hdroffset;
181 n = n0 = 0;
182 p = NULL;
184 if (!read_mem(fp, 0, &dh, sizeof dh))
185 goto the_end;
186 if (!read_mem(fp, dh.e_lfanew, &sig, sizeof sig))
187 goto the_end;
188 if (sig != 0x00004550)
189 goto the_end;
190 pef_hdroffset = dh.e_lfanew + sizeof sig;
191 if (!read_mem(fp, pef_hdroffset, &ih, sizeof ih))
192 goto the_end;
193 if (MACHINE != ih.Machine)
194 goto the_end;
195 opt_hdroffset = pef_hdroffset + sizeof ih;
196 sec_hdroffset = opt_hdroffset + sizeof oh;
197 if (!read_mem(fp, opt_hdroffset, &oh, sizeof oh))
198 goto the_end;
200 if (IMAGE_DIRECTORY_ENTRY_EXPORT >= oh.NumberOfRvaAndSizes)
201 goto the_end;
203 addr = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
204 //printf("addr: %08x\n", addr);
205 for (i = 0; i < ih.NumberOfSections; ++i) {
206 if (!read_mem(fp, sec_hdroffset + i * sizeof ish, &ish, sizeof ish))
207 goto the_end;
208 //printf("vaddr: %08x\n", ish.VirtualAddress);
209 if (addr >= ish.VirtualAddress && addr < ish.VirtualAddress + ish.SizeOfRawData)
210 goto found;
212 goto the_end;
214 found:
215 ref = ish.VirtualAddress - ish.PointerToRawData;
216 if (!read_mem(fp, addr - ref, &ied, sizeof ied))
217 goto the_end;
219 namep = ied.AddressOfNames - ref;
220 for (i = 0; i < ied.NumberOfNames; ++i) {
221 if (!read_mem(fp, namep, &ptr, sizeof ptr))
222 goto the_end;
223 namep += sizeof ptr;
224 for (l = 0;;) {
225 if (n+1 >= n0)
226 p = tcc_realloc(p, n0 = n0 ? n0 * 2 : 256);
227 if (!read_mem(fp, ptr - ref + l, p + n, 1) || ++l >= 80) {
228 tcc_free(p), p = NULL;
229 goto the_end;
231 if (p[n++] == 0)
232 break;
235 if (p)
236 p[n] = 0;
237 the_end:
238 return p;
241 /* -------------------------------------------------------------- */