x86_64: Create proper PLT and GOT also for -run
[tinycc.git] / win32 / tools / tiny_impdef.c
blob1739549f1afe38057036347c358c3cf4348a3746
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 <io.h>
29 #include <malloc.h>
31 char *get_export_names(int fd);
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;
63 p = NULL;
65 for (i = 1; i < argc; ++i) {
66 const char *a = argv[i];
67 if ('-' == a[0]) {
68 if (0 == strcmp(a, "-v")) {
69 v = 1;
70 } else if (0 == strcmp(a, "-o")) {
71 if (++i == argc)
72 goto usage;
73 strcpy(outfile, argv[i]);
74 } else
75 goto usage;
76 } else if (0 == infile[0])
77 strcpy(infile, a);
78 else
79 goto usage;
82 if (0 == infile[0]) {
83 usage:
84 fprintf(stderr,
85 "tiny_impdef: create export definition file (.def) from a dll\n"
86 "Usage: tiny_impdef library.dll [-o outputfile]\n"
88 goto the_end;
91 if (0 == outfile[0])
93 strcpy(outfile, file_basename(infile));
94 q = strrchr(outfile, '.');
95 if (NULL == q)
96 q = strchr(outfile, 0);
97 strcpy(q, ".def");
100 file = infile;
102 #ifdef _WIN32
103 pp = ext;
104 do if (SearchPath(NULL, file, *pp, sizeof path, path, NULL)) {
105 file = path;
106 break;
107 } while (*pp++);
108 #endif
110 fp = fopen(file, "rb");
111 if (NULL == fp) {
112 fprintf(stderr, "tiny_impdef: no such file: %s\n", infile);
113 goto the_end;
115 if (v)
116 printf("--> %s\n", file);
118 p = get_export_names(fileno(fp));
119 if (NULL == p) {
120 fprintf(stderr, "tiny_impdef: could not get exported function names.\n");
121 goto the_end;
124 op = fopen(outfile, "w");
125 if (NULL == op) {
126 fprintf(stderr, "tiny_impdef: could not create output file: %s\n", outfile);
127 goto the_end;
130 fprintf(op, "LIBRARY %s\n\nEXPORTS\n", file_basename(file));
131 for (q = p, i = 0; *q; ++i) {
132 fprintf(op, "%s\n", q);
133 q += strlen(q) + 1;
136 if (v) {
137 printf("<-- %s\n", outfile);
138 printf("%d symbol(s) found\n", i);
141 ret = 0;
143 the_end:
144 if (p)
145 free(p);
146 if (fp)
147 fclose(fp);
148 if (op)
149 fclose(op);
150 return ret;
153 int read_mem(int fd, unsigned offset, void *buffer, unsigned len)
155 lseek(fd, offset, SEEK_SET);
156 return len == read(fd, buffer, len);
159 /* -------------------------------------------------------------- */
161 /* -------------------------------------------------------------- */
162 #endif
164 char *get_export_names(int fd)
166 int l, i, n, n0;
167 char *p;
169 IMAGE_SECTION_HEADER ish;
170 IMAGE_EXPORT_DIRECTORY ied;
171 IMAGE_DOS_HEADER dh;
172 IMAGE_FILE_HEADER ih;
173 DWORD sig, ref, addr, ptr, namep;
174 #ifdef TCC_TARGET_X86_64
175 IMAGE_OPTIONAL_HEADER64 oh;
176 const int MACHINE = 0x8664;
177 #else
178 IMAGE_OPTIONAL_HEADER32 oh;
179 const int MACHINE = 0x014C;
180 #endif
181 int pef_hdroffset, opt_hdroffset, sec_hdroffset;
183 n = n0 = 0;
184 p = NULL;
186 if (!read_mem(fd, 0, &dh, sizeof dh))
187 goto the_end;
188 if (!read_mem(fd, dh.e_lfanew, &sig, sizeof sig))
189 goto the_end;
190 if (sig != 0x00004550)
191 goto the_end;
192 pef_hdroffset = dh.e_lfanew + sizeof sig;
193 if (!read_mem(fd, pef_hdroffset, &ih, sizeof ih))
194 goto the_end;
195 if (MACHINE != ih.Machine)
196 goto the_end;
197 opt_hdroffset = pef_hdroffset + sizeof ih;
198 sec_hdroffset = opt_hdroffset + sizeof oh;
199 if (!read_mem(fd, opt_hdroffset, &oh, sizeof oh))
200 goto the_end;
202 if (IMAGE_DIRECTORY_ENTRY_EXPORT >= oh.NumberOfRvaAndSizes)
203 goto the_end;
205 addr = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
206 //printf("addr: %08x\n", addr);
207 for (i = 0; i < ih.NumberOfSections; ++i) {
208 if (!read_mem(fd, sec_hdroffset + i * sizeof ish, &ish, sizeof ish))
209 goto the_end;
210 //printf("vaddr: %08x\n", ish.VirtualAddress);
211 if (addr >= ish.VirtualAddress && addr < ish.VirtualAddress + ish.SizeOfRawData)
212 goto found;
214 goto the_end;
216 found:
217 ref = ish.VirtualAddress - ish.PointerToRawData;
218 if (!read_mem(fd, addr - ref, &ied, sizeof ied))
219 goto the_end;
221 namep = ied.AddressOfNames - ref;
222 for (i = 0; i < ied.NumberOfNames; ++i) {
223 if (!read_mem(fd, namep, &ptr, sizeof ptr))
224 goto the_end;
225 namep += sizeof ptr;
226 for (l = 0;;) {
227 if (n+1 >= n0)
228 p = tcc_realloc(p, n0 = n0 ? n0 * 2 : 256);
229 if (!read_mem(fd, ptr - ref + l, p + n, 1) || ++l >= 80) {
230 tcc_free(p), p = NULL;
231 goto the_end;
233 if (p[n++] == 0)
234 break;
237 if (p)
238 p[n] = 0;
239 the_end:
240 return p;
243 /* -------------------------------------------------------------- */