| @@ -23,201 +23,92 @@ · 22 unmodified lines |
| 23 | #define WIN32_LEAN_AND_MEAN | 23 | #define WIN32_LEAN_AND_MEAN |
| 24 | #include <windows.h> | 24 | #include <windows.h> |
| 25 | #include <stdio.h> | 25 | #include <stdio.h> |
| | 26 | #include <malloc.h> |
| | 27 | #include "../../config.h" |
| 26 | | 28 | |
| 27 | /* Offset to PE file signature */ | 29 | int read_mem(FILE *fp, unsigned offset, void *buffer, unsigned len) |
| 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) | | |
| 57 | { | 30 | { |
| 58 | /* Number of sections is indicated in file header. */ | 31 | fseek(fp, offset, 0); |
| 59 | return (int) | 32 | return len == fread(buffer, 1, len, fp); |
| 60 | ((PIMAGE_FILE_HEADER) | | |
| 61 | PEFHDROFFSET(lpFile))->NumberOfSections; | | |
| 62 | } | 33 | } |
| 63 | | 34 | |
| 64 | | 35 | char *get_export_names(FILE *fp) |
| 65 | /* -------------------------------------------------------------- */ | | |
| 66 | | | |
| 67 | LPVOID WINAPI ImageDirectoryOffset ( | | |
| 68 | LPVOID lpFile, | | |
| 69 | DWORD dwIMAGE_DIRECTORY) | | |
| 70 | { | 36 | { |
| 71 | PIMAGE_OPTIONAL_HEADER poh; | 37 | int l, i, n, n0; |
| 72 | PIMAGE_SECTION_HEADER psh; | 38 | char *p; |
| 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 | } | | |
| 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 ( | 73 | if (IMAGE_DIRECTORY_ENTRY_EXPORT >= oh.NumberOfRvaAndSizes) |
| 109 | LPVOID lpFile, | 74 | goto the_end; |
| 110 | IMAGE_SECTION_HEADER *sh, | | |
| 111 | char *szSection) | | |
| 112 | { | | |
| 113 | PIMAGE_SECTION_HEADER psh; | | |
| 114 | int nSections = NumOfSections (lpFile); | | |
| 115 | int i; | | |
| 116 | | 75 | |
| 117 | if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != NULL) | 76 | addr = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress; |
| 118 | { | 77 | //printf("addr: %08x\n", addr); |
| 119 | /* find the section by name */ | 78 | for (i = 0; i < ih.NumberOfSections; ++i) { |
| 120 | for (i=0; i<nSections; i++) | 79 | if (!read_mem(fp, sec_hdroffset + i * sizeof ish, &ish, sizeof ish)) |
| 121 | { | 80 | goto the_end; |
| 122 | if (!strcmp (psh->Name, szSection)) | 81 | //printf("vaddr: %08x\n", ish.VirtualAddress); |
| 123 | { | 82 | if (addr >= ish.VirtualAddress && addr < ish.VirtualAddress + ish.SizeOfRawData) |
| 124 | /* copy data to header */ | 83 | goto found; |
| 125 | memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER)); | | |
| 126 | return TRUE; | | |
| 127 | } | | |
| 128 | else | | |
| 129 | psh++; | | |
| 130 | } | | |
| 131 | } | 84 | } |
| 132 | return FALSE; | 85 | goto the_end; |
| 133 | } | | |
| 134 | | 86 | |
| 135 | /* -------------------------------------------------------------- */ | 87 | found: |
| 136 | | 88 | ref = ish.VirtualAddress - ish.PointerToRawData; |
| 137 | BOOL WINAPI GetSectionHdrByAddress ( | 89 | if (!read_mem(fp, addr - ref, &ied, sizeof ied)) |
| 138 | LPVOID lpFile, | 90 | goto the_end; |
| 139 | IMAGE_SECTION_HEADER *sh, | | |
| 140 | DWORD addr) | | |
| 141 | { | | |
| 142 | PIMAGE_SECTION_HEADER psh; | | |
| 143 | int nSections = NumOfSections (lpFile); | | |
| 144 | int i; | | |
| 145 | | 91 | |
| 146 | if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != NULL) | 92 | namep = ied.AddressOfNames - ref; |
| 147 | { | 93 | for (i = 0; i < ied.NumberOfNames; ++i) { |
| 148 | /* find the section by name */ | 94 | if (!read_mem(fp, namep, &ptr, sizeof ptr)) |
| 149 | for (i=0; i<nSections; i++) | 95 | goto the_end; |
| 150 | { | 96 | namep += sizeof ptr; |
| 151 | if (addr >= psh->VirtualAddress | 97 | for (l = 0;;) { |
| 152 | && addr < psh->VirtualAddress + psh->SizeOfRawData) | 98 | if (n+1 >= n0) |
| 153 | { | 99 | p = realloc(p, n0 = n0 ? n0 * 2 : 256); |
| 154 | /* copy data to header */ | 100 | if (!read_mem(fp, ptr - ref + l, p + n, 1) || ++l >= 80) { |
| 155 | memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER)); | 101 | free(p), p = NULL; |
| 156 | return TRUE; | 102 | goto the_end; |
| 157 | } | 103 | } |
| 158 | else | 104 | if (p[n++] == 0) |
| 159 | psh++; | 105 | break; |
| 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; | | |
| 216 | } | 106 | } |
| 217 | } | 107 | } |
| 218 | *pDest = 0; | 108 | if (p) |
| 219 | | 109 | p[n] = 0; |
| 220 | return ped->NumberOfNames; | 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 | int main(int argc, char **argv) | 130 | int main(int argc, char **argv) |
| 240 | { | 131 | { |
| 241 | HANDLE hHeap; | 132 | int ret, v, i; |
| 242 | HANDLE hFile; | | |
| 243 | HANDLE hMapObject; | | |
| 244 | VOID *pMem; | | |
| 245 | | | |
| 246 | int nCnt, ret, n; | | |
| 247 | char *pNames; | | |
| 248 | char infile[MAX_PATH]; | 133 | char infile[MAX_PATH]; |
| 249 | char buffer[MAX_PATH]; | | |
| 250 | char outfile[MAX_PATH]; | 134 | char outfile[MAX_PATH]; |
| 251 | FILE *op; | | |
| 252 | char *p; | | |
| 253 | | 135 | |
| 254 | hHeap = NULL; | 136 | static const char *ext[] = { ".dll", ".exe", NULL }; |
| 255 | hFile = NULL; | 137 | const char *file, **pp; |
| 256 | hMapObject = NULL; | 138 | char path[MAX_PATH], *p, *q; |
| 257 | pMem = NULL; | 139 | FILE *fp, *op; |
| | 140 | |
| 258 | infile[0] = 0; | 141 | infile[0] = 0; |
| 259 | outfile[0] = 0; | 142 | outfile[0] = 0; |
| | 143 | fp = op = NULL; |
| | 144 | v = 0; |
| 260 | ret = 1; | 145 | ret = 1; |
| 261 | | 146 | |
| 262 | for (n = 1; n < argc; ++n) | 147 | for (i = 1; i < argc; ++i) { |
| 263 | { | 148 | const char *a = argv[i]; |
| 264 | const char *a = argv[n]; | | |
| 265 | if ('-' == a[0]) { | 149 | if ('-' == a[0]) { |
| 266 | if (0 == strcmp(a, "-o")) { | 150 | if (0 == strcmp(a, "-v")) { |
| 267 | if (++n == argc) | 151 | v = 1; |
| | 152 | } else if (0 == strcmp(a, "-o")) { |
| | 153 | if (++i == argc) |
| 268 | goto usage; | 154 | goto usage; |
| 269 | strcpy(outfile, argv[n]); | 155 | strcpy(outfile, argv[i]); |
| 270 | } | 156 | } else |
| 271 | else | | |
| 272 | goto usage; | 157 | goto usage; |
| 273 | | | |
| 274 | } else if (0 == infile[0]) | 158 | } else if (0 == infile[0]) |
| 275 | strcpy(infile, a); | 159 | strcpy(infile, a); |
| 276 | else | 160 | else |
| 277 | goto usage; | 161 | goto usage; |
| 278 | } | 162 | } |
| 279 | | 163 | |
| 280 | if (0 == infile[0]) | 164 | if (0 == infile[0]) { |
| 281 | { | | |
| 282 | usage: | 165 | usage: |
| 283 | fprintf(stderr, | 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 | "Usage: tiny_impdef library.dll [-o outputfile]\n" | 168 | "Usage: tiny_impdef library.dll [-o outputfile]\n" |
| 286 | ); | 169 | ); |
| 287 | goto the_end; | 170 | goto the_end; |
| 288 | } | 171 | } |
| 289 | | 172 | |
| 290 | if (SearchPath(NULL, infile, ".dll", sizeof buffer, buffer, NULL)) | | |
| 291 | strcpy(infile, buffer); | | |
| 292 | | | |
| 293 | if (0 == outfile[0]) | 173 | if (0 == outfile[0]) |
| 294 | { | 174 | { |
| 295 | char *p; | | |
| 296 | strcpy(outfile, file_basename(infile)); | 175 | strcpy(outfile, file_basename(infile)); |
| 297 | p = strrchr(outfile, '.'); | 176 | p = strrchr(outfile, '.'); |
| 298 | if (NULL == p) | 177 | if (NULL == p) |
| @@ -300,93 +179,55 @@ usage: · 1 unmodified line |
| 300 | strcpy(p, ".def"); | 179 | strcpy(p, ".def"); |
| 301 | } | 180 | } |
| 302 | | 181 | |
| 303 | hFile = CreateFile( | 182 | file = infile; |
| 304 | infile, | 183 | #ifdef _WIN32 |
| 305 | GENERIC_READ, | 184 | for (pp = ext; *pp; ++pp) |
| 306 | FILE_SHARE_READ, | 185 | if (SearchPath(NULL, file, *pp, sizeof path, path, NULL)) { |
| 307 | NULL, | 186 | file = path; |
| 308 | OPEN_EXISTING, | 187 | break; |
| 309 | 0, | 188 | } |
| 310 | NULL | 189 | #endif |
| 311 | ); | | |
| 312 | | 190 | |
| 313 | if (hFile == INVALID_HANDLE_VALUE) | 191 | fp = fopen(file, "rb"); |
| 314 | { | 192 | if (NULL == fp) { |
| 315 | fprintf(stderr, "No such file: %s\n", infile); | 193 | fprintf(stderr, "tiny_impdef: no such file: %s\n", infile); |
| 316 | goto the_end; | 194 | goto the_end; |
| 317 | } | 195 | } |
| 318 | | 196 | |
| 319 | | 197 | op = fopen(outfile, "w"); |
| 320 | hMapObject = CreateFileMapping( | 198 | if (NULL == op) { |
| 321 | hFile, | 199 | fprintf(stderr, "tiny_impdef: could not create output file: %s\n", outfile); |
| 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); | | |
| 331 | goto the_end; | 200 | goto the_end; |
| 332 | } | 201 | } |
| 333 | | 202 | |
| 334 | pMem = MapViewOfFile( | 203 | if (v) |
| 335 | hMapObject, // object to map view of | 204 | printf("--> %s\n", infile); |
| 336 | FILE_MAP_READ, // read access | | |
| 337 | 0, // high offset: map from | | |
| 338 | 0, // low offset: beginning | | |
| 339 | 0); // default: map entire file | | |
| 340 | | 205 | |
| 341 | if (NULL == pMem) | 206 | fprintf(op, "LIBRARY %s\n\nEXPORTS\n", file_basename(infile)); |
| 342 | { | 207 | p = get_export_names(fp); |
| 343 | fprintf(stderr, "Could not map view of file: %s\n", infile); | 208 | if (NULL == p) { |
| | 209 | fprintf(stderr, "tiny_impdef: could not get exported function names.\n"); |
| 344 | goto the_end; | 210 | goto the_end; |
| 345 | } | 211 | } |
| 346 | | 212 | |
| 347 | if (0 != strncmp(NTSIGNATURE(pMem), "PE", 2)) | 213 | for (q = p, i = 0; *q; ++i) { |
| 348 | { | 214 | fprintf(op, "%s\n", q); |
| 349 | fprintf(stderr, "Not a PE file: %s\n", infile); | 215 | q += strlen(q) + 1; |
| 350 | goto the_end; | | |
| 351 | } | 216 | } |
| | 217 | free(p); |
| 352 | | 218 | |
| 353 | | 219 | if (v) { |
| 354 | hHeap = GetProcessHeap(); | 220 | printf("<-- %s\n", outfile); |
| 355 | nCnt = GetExportFunctionNames(pMem, hHeap, &pNames); | 221 | printf("%d symbol(s) found\n", i); |
| 356 | if (0 == nCnt) { | | |
| 357 | fprintf(stderr, "Could not get exported function names: %s\n", infile); | | |
| 358 | goto the_end; | | |
| 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 | ret = 0; | 224 | ret = 0; |
| 379 | | 225 | |
| 380 | the_end: | 226 | the_end: |
| 381 | if (pMem) | 227 | if (fp) |
| 382 | UnmapViewOfFile(pMem); | 228 | fclose(fp); |
| 383 | | 229 | if (op) |
| 384 | if (hMapObject) | 230 | fclose(op); |
| 385 | CloseHandle(hMapObject); | | |
| 386 | | | |
| 387 | if (hFile) | | |
| 388 | CloseHandle(hFile); | | |
| 389 | | | |
| 390 | return ret; | 231 | return ret; |
| 391 | } | 232 | } |
| 392 | | 233 | |