| @@ -23,201 +23,92 @@ · 22 unmodified lines |
| 23 | 23 | #define WIN32_LEAN_AND_MEAN |
| 24 | 24 | #include <windows.h> |
| 25 | 25 | #include <stdio.h> |
| 26 | +#include <malloc.h> |
| 27 | +#include "../../config.h" |
| 26 | 28 | |
| 27 | | −/* Offset to PE file signature */ |
| 28 | | −#define NTSIGNATURE(a) ((LPVOID)((BYTE *)a + \ |
| 29 | | − ((PIMAGE_DOS_HEADER)a)->e_lfanew)) |
| 30 | | − |
| 31 | | −/* MS-OS header identifies the NT PEFile signature dword; |
| 32 | | − the PEFILE header exists just after that dword. */ |
| 33 | | −#define PEFHDROFFSET(a) ((LPVOID)((BYTE *)a + \ |
| 34 | | − ((PIMAGE_DOS_HEADER)a)->e_lfanew + \ |
| 35 | | − SIZE_OF_NT_SIGNATURE)) |
| 36 | | − |
| 37 | | −/* PE optional header is immediately after PEFile header. */ |
| 38 | | −#define OPTHDROFFSET(a) ((LPVOID)((BYTE *)a + \ |
| 39 | | − ((PIMAGE_DOS_HEADER)a)->e_lfanew + \ |
| 40 | | − SIZE_OF_NT_SIGNATURE + \ |
| 41 | | − sizeof (IMAGE_FILE_HEADER))) |
| 42 | | − |
| 43 | | −/* Section headers are immediately after PE optional header. */ |
| 44 | | −#define SECHDROFFSET(a) ((LPVOID)((BYTE *)a + \ |
| 45 | | − ((PIMAGE_DOS_HEADER)a)->e_lfanew + \ |
| 46 | | − SIZE_OF_NT_SIGNATURE + \ |
| 47 | | − sizeof (IMAGE_FILE_HEADER) + \ |
| 48 | | − sizeof (IMAGE_OPTIONAL_HEADER))) |
| 49 | | − |
| 50 | | − |
| 51 | | −#define SIZE_OF_NT_SIGNATURE 4 |
| 52 | | − |
| 53 | | −/* -------------------------------------------------------------- */ |
| 54 | | − |
| 55 | | −int WINAPI NumOfSections ( |
| 56 | | − LPVOID lpFile) |
| 29 | +int read_mem(FILE *fp, unsigned offset, void *buffer, unsigned len) |
| 57 | 30 | { |
| 58 | | − /* Number of sections is indicated in file header. */ |
| 59 | | − return (int) |
| 60 | | − ((PIMAGE_FILE_HEADER) |
| 61 | | − PEFHDROFFSET(lpFile))->NumberOfSections; |
| 31 | + fseek(fp, offset, 0); |
| 32 | + return len == fread(buffer, 1, len, fp); |
| 62 | 33 | } |
| 63 | 34 | |
| 64 | | − |
| 65 | | −/* -------------------------------------------------------------- */ |
| 66 | | − |
| 67 | | −LPVOID WINAPI ImageDirectoryOffset ( |
| 68 | | − LPVOID lpFile, |
| 69 | | − DWORD dwIMAGE_DIRECTORY) |
| 35 | +char *get_export_names(FILE *fp) |
| 70 | 36 | { |
| 71 | | − PIMAGE_OPTIONAL_HEADER poh; |
| 72 | | − PIMAGE_SECTION_HEADER psh; |
| 73 | | − int nSections = NumOfSections (lpFile); |
| 74 | | − int i = 0; |
| 75 | | − LPVOID VAImageDir; |
| 76 | | − |
| 77 | | − /* Retrieve offsets to optional and section headers. */ |
| 78 | | − poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile); |
| 79 | | − psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile); |
| 80 | | − |
| 81 | | − /* Must be 0 thru (NumberOfRvaAndSizes-1). */ |
| 82 | | − if (dwIMAGE_DIRECTORY >= poh->NumberOfRvaAndSizes) |
| 83 | | − return NULL; |
| 84 | | − |
| 85 | | − /* Locate image directory's relative virtual address. */ |
| 86 | | − VAImageDir = (LPVOID)poh->DataDirectory[dwIMAGE_DIRECTORY].VirtualAddress; |
| 87 | | − |
| 88 | | − /* Locate section containing image directory. */ |
| 89 | | − while (i++<nSections) |
| 90 | | − { |
| 91 | | − if (psh->VirtualAddress <= (DWORD)VAImageDir |
| 92 | | − && psh->VirtualAddress + psh->SizeOfRawData > (DWORD)VAImageDir) |
| 93 | | − break; |
| 94 | | − psh++; |
| 95 | | − } |
| 96 | | − |
| 97 | | − if (i > nSections) |
| 98 | | − return NULL; |
| 99 | | − |
| 100 | | − /* Return image import directory offset. */ |
| 101 | | − return (LPVOID)(((int)lpFile + |
| 102 | | − (int)VAImageDir - psh->VirtualAddress) + |
| 103 | | − (int)psh->PointerToRawData); |
| 104 | | −} |
| 37 | + int l, i, n, n0; |
| 38 | + char *p; |
| 105 | 39 | |
| 106 | | −/* -------------------------------------------------------------- */ |
| 40 | + IMAGE_SECTION_HEADER ish; |
| 41 | + IMAGE_EXPORT_DIRECTORY ied; |
| 42 | + IMAGE_DOS_HEADER dh; |
| 43 | + IMAGE_FILE_HEADER ih; |
| 44 | + DWORD sig, ref, addr, ptr, namep; |
| 45 | +#ifdef TCC_TARGET_X86_64 |
| 46 | + IMAGE_OPTIONAL_HEADER64 oh; |
| 47 | + const int MACHINE = 0x8664; |
| 48 | +#else |
| 49 | + IMAGE_OPTIONAL_HEADER32 oh; |
| 50 | + const int MACHINE = 0x014C; |
| 51 | +#endif |
| 52 | + int pef_hdroffset, opt_hdroffset, sec_hdroffset; |
| 53 | + |
| 54 | + n = n0 = 0; |
| 55 | + p = NULL; |
| 56 | + |
| 57 | + if (!read_mem(fp, 0, &dh, sizeof dh)) |
| 58 | + goto the_end; |
| 59 | + if (!read_mem(fp, dh.e_lfanew, &sig, sizeof sig)) |
| 60 | + goto the_end; |
| 61 | + if (sig != 0x00004550) |
| 62 | + goto the_end; |
| 63 | + pef_hdroffset = dh.e_lfanew + sizeof sig; |
| 64 | + if (!read_mem(fp, pef_hdroffset, &ih, sizeof ih)) |
| 65 | + goto the_end; |
| 66 | + if (MACHINE != ih.Machine) |
| 67 | + goto the_end; |
| 68 | + opt_hdroffset = pef_hdroffset + sizeof ih; |
| 69 | + sec_hdroffset = opt_hdroffset + sizeof oh; |
| 70 | + if (!read_mem(fp, opt_hdroffset, &oh, sizeof oh)) |
| 71 | + goto the_end; |
| 107 | 72 | |
| 108 | | −BOOL WINAPI GetSectionHdrByName ( |
| 109 | | − LPVOID lpFile, |
| 110 | | − IMAGE_SECTION_HEADER *sh, |
| 111 | | − char *szSection) |
| 112 | | −{ |
| 113 | | − PIMAGE_SECTION_HEADER psh; |
| 114 | | − int nSections = NumOfSections (lpFile); |
| 115 | | − int i; |
| 73 | + if (IMAGE_DIRECTORY_ENTRY_EXPORT >= oh.NumberOfRvaAndSizes) |
| 74 | + goto the_end; |
| 116 | 75 | |
| 117 | | − if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != NULL) |
| 118 | | − { |
| 119 | | − /* find the section by name */ |
| 120 | | − for (i=0; i<nSections; i++) |
| 121 | | − { |
| 122 | | − if (!strcmp (psh->Name, szSection)) |
| 123 | | − { |
| 124 | | − /* copy data to header */ |
| 125 | | − memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER)); |
| 126 | | − return TRUE; |
| 127 | | − } |
| 128 | | − else |
| 129 | | − psh++; |
| 130 | | − } |
| 76 | + addr = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress; |
| 77 | + //printf("addr: %08x\n", addr); |
| 78 | + for (i = 0; i < ih.NumberOfSections; ++i) { |
| 79 | + if (!read_mem(fp, sec_hdroffset + i * sizeof ish, &ish, sizeof ish)) |
| 80 | + goto the_end; |
| 81 | + //printf("vaddr: %08x\n", ish.VirtualAddress); |
| 82 | + if (addr >= ish.VirtualAddress && addr < ish.VirtualAddress + ish.SizeOfRawData) |
| 83 | + goto found; |
| 131 | 84 | } |
| 132 | | − return FALSE; |
| 133 | | −} |
| 85 | + goto the_end; |
| 134 | 86 | |
| 135 | | −/* -------------------------------------------------------------- */ |
| 136 | | − |
| 137 | | −BOOL WINAPI GetSectionHdrByAddress ( |
| 138 | | − LPVOID lpFile, |
| 139 | | − IMAGE_SECTION_HEADER *sh, |
| 140 | | − DWORD addr) |
| 141 | | −{ |
| 142 | | − PIMAGE_SECTION_HEADER psh; |
| 143 | | − int nSections = NumOfSections (lpFile); |
| 144 | | − int i; |
| 87 | +found: |
| 88 | + ref = ish.VirtualAddress - ish.PointerToRawData; |
| 89 | + if (!read_mem(fp, addr - ref, &ied, sizeof ied)) |
| 90 | + goto the_end; |
| 145 | 91 | |
| 146 | | − if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != NULL) |
| 147 | | − { |
| 148 | | − /* find the section by name */ |
| 149 | | − for (i=0; i<nSections; i++) |
| 150 | | − { |
| 151 | | − if (addr >= psh->VirtualAddress |
| 152 | | − && addr < psh->VirtualAddress + psh->SizeOfRawData) |
| 153 | | − { |
| 154 | | − /* copy data to header */ |
| 155 | | − memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER)); |
| 156 | | − return TRUE; |
| 92 | + namep = ied.AddressOfNames - ref; |
| 93 | + for (i = 0; i < ied.NumberOfNames; ++i) { |
| 94 | + if (!read_mem(fp, namep, &ptr, sizeof ptr)) |
| 95 | + goto the_end; |
| 96 | + namep += sizeof ptr; |
| 97 | + for (l = 0;;) { |
| 98 | + if (n+1 >= n0) |
| 99 | + p = realloc(p, n0 = n0 ? n0 * 2 : 256); |
| 100 | + if (!read_mem(fp, ptr - ref + l, p + n, 1) || ++l >= 80) { |
| 101 | + free(p), p = NULL; |
| 102 | + goto the_end; |
| 157 | 103 | } |
| 158 | | − else |
| 159 | | − psh++; |
| 160 | | − } |
| 161 | | − } |
| 162 | | − return FALSE; |
| 163 | | −} |
| 164 | | − |
| 165 | | −/* -------------------------------------------------------------- */ |
| 166 | | − |
| 167 | | −int WINAPI GetExportFunctionNames ( |
| 168 | | − LPVOID lpFile, |
| 169 | | − HANDLE hHeap, |
| 170 | | − char **pszFunctions) |
| 171 | | −{ |
| 172 | | − IMAGE_SECTION_HEADER sh; |
| 173 | | − PIMAGE_EXPORT_DIRECTORY ped; |
| 174 | | − int *pNames, *pCnt; |
| 175 | | − char *pSrc, *pDest; |
| 176 | | − int i, nCnt; |
| 177 | | − DWORD VAImageDir; |
| 178 | | − PIMAGE_OPTIONAL_HEADER poh; |
| 179 | | − char *pOffset; |
| 180 | | − |
| 181 | | − /* Get section header and pointer to data directory |
| 182 | | − for .edata section. */ |
| 183 | | − if (NULL == (ped = (PIMAGE_EXPORT_DIRECTORY) |
| 184 | | − ImageDirectoryOffset (lpFile, IMAGE_DIRECTORY_ENTRY_EXPORT))) |
| 185 | | − return 0; |
| 186 | | − |
| 187 | | − poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile); |
| 188 | | − VAImageDir = poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress; |
| 189 | | − |
| 190 | | − if (FALSE == GetSectionHdrByAddress (lpFile, &sh, VAImageDir)) |
| 191 | | − return 0; |
| 192 | | − |
| 193 | | − pOffset = (char *)lpFile + (sh.PointerToRawData - sh.VirtualAddress); |
| 194 | | − |
| 195 | | − pNames = (int *)(pOffset + (DWORD)ped->AddressOfNames); |
| 196 | | − |
| 197 | | − /* Figure out how much memory to allocate for all strings. */ |
| 198 | | − nCnt = 1; |
| 199 | | − for (i=0, pCnt = pNames; i<(int)ped->NumberOfNames; i++) |
| 200 | | − { |
| 201 | | − pSrc = (pOffset + *pCnt++); |
| 202 | | − if (pSrc) |
| 203 | | − nCnt += strlen(pSrc)+1; |
| 204 | | − } |
| 205 | | − |
| 206 | | − /* Allocate memory off heap for function names. */ |
| 207 | | − pDest = *pszFunctions = HeapAlloc (hHeap, HEAP_ZERO_MEMORY, nCnt); |
| 208 | | − |
| 209 | | − /* Copy all strings to buffer. */ |
| 210 | | − for (i=0, pCnt = pNames; i<(int)ped->NumberOfNames; i++) |
| 211 | | − { |
| 212 | | − pSrc = (pOffset + *pCnt++); |
| 213 | | − if (pSrc) { |
| 214 | | − strcpy(pDest, pSrc); |
| 215 | | − pDest += strlen(pSrc)+1; |
| 104 | + if (p[n++] == 0) |
| 105 | + break; |
| 216 | 106 | } |
| 217 | 107 | } |
| 218 | | − *pDest = 0; |
| 219 | | − |
| 220 | | − return ped->NumberOfNames; |
| 108 | + if (p) |
| 109 | + p[n] = 0; |
| 110 | +the_end: |
| 111 | + return p; |
| 221 | 112 | } |
| 222 | 113 | |
| 223 | 114 | /* -------------------------------------------------------------- */ |
| @@ -238,61 +129,49 @@ static char *file_basename(const char *name) · 14 unmodified lines |
| 238 | 129 | |
| 239 | 130 | int main(int argc, char **argv) |
| 240 | 131 | { |
| 241 | | − HANDLE hHeap; |
| 242 | | − HANDLE hFile; |
| 243 | | − HANDLE hMapObject; |
| 244 | | − VOID *pMem; |
| 245 | | − |
| 246 | | − int nCnt, ret, n; |
| 247 | | − char *pNames; |
| 132 | + int ret, v, i; |
| 248 | 133 | char infile[MAX_PATH]; |
| 249 | | − char buffer[MAX_PATH]; |
| 250 | 134 | char outfile[MAX_PATH]; |
| 251 | | − FILE *op; |
| 252 | | − char *p; |
| 253 | 135 | |
| 254 | | − hHeap = NULL; |
| 255 | | − hFile = NULL; |
| 256 | | − hMapObject = NULL; |
| 257 | | − pMem = NULL; |
| 136 | + static const char *ext[] = { ".dll", ".exe", NULL }; |
| 137 | + const char *file, **pp; |
| 138 | + char path[MAX_PATH], *p, *q; |
| 139 | + FILE *fp, *op; |
| 140 | + |
| 258 | 141 | infile[0] = 0; |
| 259 | 142 | outfile[0] = 0; |
| 143 | + fp = op = NULL; |
| 144 | + v = 0; |
| 260 | 145 | ret = 1; |
| 261 | 146 | |
| 262 | | − for (n = 1; n < argc; ++n) |
| 263 | | − { |
| 264 | | − const char *a = argv[n]; |
| 147 | + for (i = 1; i < argc; ++i) { |
| 148 | + const char *a = argv[i]; |
| 265 | 149 | if ('-' == a[0]) { |
| 266 | | − if (0 == strcmp(a, "-o")) { |
| 267 | | − if (++n == argc) |
| 150 | + if (0 == strcmp(a, "-v")) { |
| 151 | + v = 1; |
| 152 | + } else if (0 == strcmp(a, "-o")) { |
| 153 | + if (++i == argc) |
| 268 | 154 | goto usage; |
| 269 | | − strcpy(outfile, argv[n]); |
| 270 | | − } |
| 271 | | − else |
| 155 | + strcpy(outfile, argv[i]); |
| 156 | + } else |
| 272 | 157 | goto usage; |
| 273 | | − |
| 274 | 158 | } else if (0 == infile[0]) |
| 275 | 159 | strcpy(infile, a); |
| 276 | 160 | else |
| 277 | 161 | goto usage; |
| 278 | 162 | } |
| 279 | 163 | |
| 280 | | − if (0 == infile[0]) |
| 281 | | − { |
| 164 | + if (0 == infile[0]) { |
| 282 | 165 | usage: |
| 283 | 166 | fprintf(stderr, |
| 284 | | − "tiny_impdef creates an export definition file (.def) from a dll\n" |
| 167 | + "tiny_impdef: create export definition file (.def) from a dll\n" |
| 285 | 168 | "Usage: tiny_impdef library.dll [-o outputfile]\n" |
| 286 | 169 | ); |
| 287 | 170 | goto the_end; |
| 288 | 171 | } |
| 289 | 172 | |
| 290 | | − if (SearchPath(NULL, infile, ".dll", sizeof buffer, buffer, NULL)) |
| 291 | | − strcpy(infile, buffer); |
| 292 | | − |
| 293 | 173 | if (0 == outfile[0]) |
| 294 | 174 | { |
| 295 | | − char *p; |
| 296 | 175 | strcpy(outfile, file_basename(infile)); |
| 297 | 176 | p = strrchr(outfile, '.'); |
| 298 | 177 | if (NULL == p) |
| @@ -300,93 +179,55 @@ usage: · 1 unmodified line |
| 300 | 179 | strcpy(p, ".def"); |
| 301 | 180 | } |
| 302 | 181 | |
| 303 | | − hFile = CreateFile( |
| 304 | | − infile, |
| 305 | | − GENERIC_READ, |
| 306 | | − FILE_SHARE_READ, |
| 307 | | − NULL, |
| 308 | | − OPEN_EXISTING, |
| 309 | | − 0, |
| 310 | | − NULL |
| 311 | | − ); |
| 182 | + file = infile; |
| 183 | +#ifdef _WIN32 |
| 184 | + for (pp = ext; *pp; ++pp) |
| 185 | + if (SearchPath(NULL, file, *pp, sizeof path, path, NULL)) { |
| 186 | + file = path; |
| 187 | + break; |
| 188 | + } |
| 189 | +#endif |
| 312 | 190 | |
| 313 | | − if (hFile == INVALID_HANDLE_VALUE) |
| 314 | | − { |
| 315 | | − fprintf(stderr, "No such file: %s\n", infile); |
| 191 | + fp = fopen(file, "rb"); |
| 192 | + if (NULL == fp) { |
| 193 | + fprintf(stderr, "tiny_impdef: no such file: %s\n", infile); |
| 316 | 194 | goto the_end; |
| 317 | 195 | } |
| 318 | 196 | |
| 319 | | − |
| 320 | | − hMapObject = CreateFileMapping( |
| 321 | | − hFile, |
| 322 | | − NULL, |
| 323 | | − PAGE_READONLY, |
| 324 | | − 0, 0, |
| 325 | | − NULL |
| 326 | | − ); |
| 327 | | − |
| 328 | | − if (NULL == hMapObject) |
| 329 | | − { |
| 330 | | − fprintf(stderr, "Could not create file mapping: %s\n", infile); |
| 197 | + op = fopen(outfile, "w"); |
| 198 | + if (NULL == op) { |
| 199 | + fprintf(stderr, "tiny_impdef: could not create output file: %s\n", outfile); |
| 331 | 200 | goto the_end; |
| 332 | 201 | } |
| 333 | 202 | |
| 334 | | − pMem = MapViewOfFile( |
| 335 | | − hMapObject, // object to map view of |
| 336 | | − FILE_MAP_READ, // read access |
| 337 | | − 0, // high offset: map from |
| 338 | | − 0, // low offset: beginning |
| 339 | | − 0); // default: map entire file |
| 203 | + if (v) |
| 204 | + printf("--> %s\n", infile); |
| 340 | 205 | |
| 341 | | − if (NULL == pMem) |
| 342 | | − { |
| 343 | | − fprintf(stderr, "Could not map view of file: %s\n", infile); |
| 206 | + fprintf(op, "LIBRARY %s\n\nEXPORTS\n", file_basename(infile)); |
| 207 | + p = get_export_names(fp); |
| 208 | + if (NULL == p) { |
| 209 | + fprintf(stderr, "tiny_impdef: could not get exported function names.\n"); |
| 344 | 210 | goto the_end; |
| 345 | 211 | } |
| 346 | 212 | |
| 347 | | − if (0 != strncmp(NTSIGNATURE(pMem), "PE", 2)) |
| 348 | | − { |
| 349 | | − fprintf(stderr, "Not a PE file: %s\n", infile); |
| 350 | | − goto the_end; |
| 213 | + for (q = p, i = 0; *q; ++i) { |
| 214 | + fprintf(op, "%s\n", q); |
| 215 | + q += strlen(q) + 1; |
| 351 | 216 | } |
| 217 | + free(p); |
| 352 | 218 | |
| 353 | | − |
| 354 | | − hHeap = GetProcessHeap(); |
| 355 | | − nCnt = GetExportFunctionNames(pMem, hHeap, &pNames); |
| 356 | | − if (0 == nCnt) { |
| 357 | | − fprintf(stderr, "Could not get exported function names: %s\n", infile); |
| 358 | | − goto the_end; |
| 219 | + if (v) { |
| 220 | + printf("<-- %s\n", outfile); |
| 221 | + printf("%d symbol(s) found\n", i); |
| 359 | 222 | } |
| 360 | 223 | |
| 361 | | − printf("--> %s\n", infile); |
| 362 | | − |
| 363 | | − op = fopen(outfile, "w"); |
| 364 | | − if (NULL == op) |
| 365 | | − { |
| 366 | | − fprintf(stderr, "Could not create file: %s\n", outfile); |
| 367 | | − goto the_end; |
| 368 | | − } |
| 369 | | − |
| 370 | | − printf("<-- %s\n", outfile); |
| 371 | | − |
| 372 | | − fprintf(op, "LIBRARY %s\n\nEXPORTS\n", file_basename(infile)); |
| 373 | | − for (n = 0, p = pNames; n < nCnt; ++n) |
| 374 | | − { |
| 375 | | − fprintf(op, "%s\n", p); |
| 376 | | − while (*p++); |
| 377 | | − } |
| 378 | 224 | ret = 0; |
| 379 | 225 | |
| 380 | 226 | the_end: |
| 381 | | − if (pMem) |
| 382 | | − UnmapViewOfFile(pMem); |
| 383 | | − |
| 384 | | − if (hMapObject) |
| 385 | | − CloseHandle(hMapObject); |
| 386 | | − |
| 387 | | − if (hFile) |
| 388 | | − CloseHandle(hFile); |
| 389 | | − |
| 227 | + if (fp) |
| 228 | + fclose(fp); |
| 229 | + if (op) |
| 230 | + fclose(op); |
| 390 | 231 | return ret; |
| 391 | 232 | } |
| 392 | 233 | |