2 * Copyright 1994 Eric Youndale & Erik Bos
4 * based on Eric Youndale's pe-test and:
6 * ftp.microsoft.com:/pub/developer/MSDN/CD8/PEFILE.ZIP
14 #include <sys/types.h>
22 #define MAP_ANONYMOUS 0x20
24 struct w_files *wine_files = NULL;
26 unsigned int load_addr;
28 void my_wcstombs(char * result, u_short * source, int len)
31 if(isascii(*source)) *result++ = *source++;
33 printf("Unable to handle unicode right now\n");
39 char * xmmap(char * vaddr, unsigned int v_size, unsigned int r_size,
40 int prot, int flags, int fd, unsigned int file_offset)
43 /* .bss has no associated storage in the PE file */
48 result = mmap(vaddr, v_size, prot, flags, fd, file_offset);
49 if((unsigned int) result != 0xffffffff) return result;
51 /* Sigh. Alignment must be wrong for mmap. Do this the hard way. */
52 if(!(flags & MAP_FIXED)) {
53 vaddr = (char *)0x40000000;
57 mmap(vaddr, v_size, prot, MAP_ANONYMOUS | flags, 0, 0);
58 lseek(fd, file_offset, SEEK_SET);
59 read(fd, vaddr, v_size);
63 void dump_exports(struct PE_Export_Directory * pe_exports)
69 u_char ** name, *ename;
71 Module = ((char *) load_addr) + pe_exports->Name;
72 printf("\n*******EXPORT DATA*******\nModule name is %s, %ld functions, %ld names\n",
74 pe_exports->Number_Of_Functions,
75 pe_exports->Number_Of_Names);
77 ordinal = (u_short *) (((char *) load_addr) + (int) pe_exports->Address_Of_Name_Ordinals);
78 function = (u_long *) (((char *) load_addr) + (int) pe_exports->AddressOfFunctions);
79 name = (u_char **) (((char *) load_addr) + (int) pe_exports->AddressOfNames);
81 printf("%-32s Ordinal Virt Addr\n", "Function Name");
82 for(i=0; i< pe_exports->Number_Of_Functions; i++)
84 ename = (char *) (((char *) load_addr) + (int) *name++);
85 printf("%-32s %4d %8.8lx\n", ename, *ordinal++, *function++);
89 void fixup_imports(struct PE_Import_Directory *pe_imports)
91 struct PE_Import_Directory * pe_imp;
94 /* OK, now dump the import list */
95 printf("\nDumping imports list\n");
97 while (pe_imp->ModuleName)
100 struct pe_import_name * pe_name;
101 unsigned int * import_list, *thunk_list;
104 Module = ((char *) load_addr) + pe_imp->ModuleName;
105 printf("%s\n", Module);
106 c = strchr(Module, '.');
109 import_list = (unsigned int *)
110 (((unsigned int) load_addr) + pe_imp->Import_List);
111 thunk_list = (unsigned int *)
112 (((unsigned int) load_addr) + pe_imp->Thunk_List);
117 pe_name = (struct pe_import_name *) ((int) load_addr + *import_list);
118 if((unsigned)pe_name & 0x80000000)
120 fprintf(stderr,"Import by ordinal not supported\n");
123 printf("--- %s %s.%d\n", pe_name->Name, Module, pe_name->Hint);
124 *thunk_list=RELAY32_GetEntryPoint(Module,pe_name->Name,pe_name->Hint);
127 fprintf(stderr,"No implementation for %s.%d\n",Module, pe_name->Hint);
136 if(fixup_failed)exit(1);
139 static void dump_table(struct w_files *wpnt)
143 printf("Dump of segment table\n");
144 printf(" Name VSz Vaddr SzRaw Fileadr *Reloc *Lineum #Reloc #Linum Char\n");
145 for(i=0; i< wpnt->pe->pe_header->coff.NumberOfSections; i++)
147 printf("%8s: %4.4lx %8.8lx %8.8lx %8.8lx %8.8lx %8.8lx %4.4x %4.4x %8.8lx\n",
148 wpnt->pe->pe_seg[i].Name,
149 wpnt->pe->pe_seg[i].Virtual_Size,
150 wpnt->pe->pe_seg[i].Virtual_Address,
151 wpnt->pe->pe_seg[i].Size_Of_Raw_Data,
152 wpnt->pe->pe_seg[i].PointerToRawData,
153 wpnt->pe->pe_seg[i].PointerToRelocations,
154 wpnt->pe->pe_seg[i].PointerToLinenumbers,
155 wpnt->pe->pe_seg[i].NumberOfRelocations,
156 wpnt->pe->pe_seg[i].NumberOfLinenumbers,
157 wpnt->pe->pe_seg[i].Characteristics);
161 /**********************************************************************
163 * Load one PE format executable into memory
165 HINSTANCE PE_LoadImage(struct w_files *wpnt)
169 wpnt->pe = malloc(sizeof(struct pe_data));
170 wpnt->pe->pe_header = malloc(sizeof(struct pe_header_s));
173 lseek(wpnt->fd, wpnt->mz_header->ne_offset, SEEK_SET);
174 read(wpnt->fd, wpnt->pe->pe_header, sizeof(struct pe_header_s));
177 wpnt->pe->pe_seg = malloc(sizeof(struct pe_segment_table) *
178 wpnt->pe->pe_header->coff.NumberOfSections);
179 read(wpnt->fd, wpnt->pe->pe_seg, sizeof(struct pe_segment_table) *
180 wpnt->pe->pe_header->coff.NumberOfSections);
182 load_addr = wpnt->pe->pe_header->opt_coff.BaseOfImage;
183 printf("Load addr is %x\n",load_addr);
186 for(i=0; i < wpnt->pe->pe_header->coff.NumberOfSections; i++)
189 result = xmmap((char *)0, wpnt->pe->pe_seg[i].Virtual_Size,
190 wpnt->pe->pe_seg[i].Size_Of_Raw_Data, 7,
191 MAP_PRIVATE, wpnt->fd, wpnt->pe->pe_seg[i].PointerToRawData);
192 load_addr = (unsigned int) result - wpnt->pe->pe_seg[i].Virtual_Address;
194 result = xmmap((char *) load_addr + wpnt->pe->pe_seg[i].Virtual_Address,
195 wpnt->pe->pe_seg[i].Virtual_Size,
196 wpnt->pe->pe_seg[i].Size_Of_Raw_Data, 7, MAP_PRIVATE | MAP_FIXED,
197 wpnt->fd, wpnt->pe->pe_seg[i].PointerToRawData);
200 fprintf(stderr,"Could not load section %x to desired address %lx\n",
201 i, load_addr+wpnt->pe->pe_seg[i].Virtual_Address);
202 fprintf(stderr,"Need to implement relocations now\n");
206 if(strcmp(wpnt->pe->pe_seg[i].Name, ".idata") == 0)
207 wpnt->pe->pe_import = (struct PE_Import_Directory *) result;
209 if(strcmp(wpnt->pe->pe_seg[i].Name, ".edata") == 0)
210 wpnt->pe->pe_export = (struct PE_Export_Directory *) result;
212 if(strcmp(wpnt->pe->pe_seg[i].Name, ".rsrc") == 0) {
213 wpnt->pe->pe_resource = (struct PE_Resource_Directory *) result;
215 /* save offset for PE_FindResource */
216 wpnt->pe->resource_offset = wpnt->pe->pe_seg[i].Virtual_Address -
217 wpnt->pe->pe_seg[i].PointerToRawData;
221 if(wpnt->pe->pe_import) fixup_imports(wpnt->pe->pe_import);
222 if(wpnt->pe->pe_export) dump_exports(wpnt->pe->pe_export);
224 wpnt->hinstance = 0x8000;
225 return (wpnt->hinstance);
228 int PE_UnloadImage(struct w_files *wpnt)
230 printf("PEunloadImage() called!\n");
231 /* free resources, image, unmap */
235 void PE_InitDLL(struct w_files *wpnt)
237 /* Is this a library? */
238 if (wpnt->pe->pe_header->coff.Characteristics & IMAGE_FILE_DLL) {
239 printf("InitPEDLL() called!\n");