- clean-up of texture 'loading'
[wine.git] / loader / elf.c
blobc2b1fb4ca862afb75860c56349f4f3cf3a312be1
1 /*
2 * UNIX dynamic loader
3 *
4 * Currently only supports stuff using the dl* API.
6 * Copyright 1998 Marcus Meissner
8 * FIXME: Small reentrancy problem.
9 * IDEA(s): could be used to split up shell32,comctl32...
12 #include "config.h"
14 #include <assert.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <sys/types.h>
19 #include "snoop.h"
20 #include "process.h"
21 #include "neexe.h"
22 #include "peexe.h"
23 #include "heap.h"
24 #include "pe_image.h"
25 #include "module.h"
26 #include "debug.h"
28 WINE_MODREF *ELF_CreateDummyModule( LPCSTR libname, LPCSTR modname )
30 PIMAGE_DOS_HEADER dh;
31 PIMAGE_NT_HEADERS nth;
32 PIMAGE_SECTION_HEADER sh;
33 WINE_MODREF *wm;
34 HMODULE hmod;
36 wm=(WINE_MODREF*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wm) );
37 wm->type = MODULE32_ELF;
39 /* FIXME: hmm, order? */
40 wm->next = PROCESS_Current()->modref_list;
41 PROCESS_Current()->modref_list = wm;
43 wm->modname = HEAP_strdupA( GetProcessHeap(), 0, modname );
44 wm->longname = HEAP_strdupA( GetProcessHeap(), 0, libname );
46 hmod = (HMODULE)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
47 sizeof(IMAGE_DOS_HEADER) +
48 sizeof(IMAGE_NT_HEADERS) +
49 sizeof(IMAGE_SECTION_HEADER) + 100 );
50 dh = (PIMAGE_DOS_HEADER)hmod;
51 dh->e_magic = IMAGE_DOS_SIGNATURE;
52 dh->e_lfanew = sizeof(IMAGE_DOS_HEADER);
53 nth = PE_HEADER(hmod);
54 nth->Signature = IMAGE_NT_SIGNATURE;
55 nth->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
56 nth->FileHeader.NumberOfSections = 1;
57 nth->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
58 nth->FileHeader.Characteristics =
59 IMAGE_FILE_RELOCS_STRIPPED|IMAGE_FILE_LINE_NUMS_STRIPPED|
60 IMAGE_FILE_LOCAL_SYMS_STRIPPED|IMAGE_FILE_32BIT_MACHINE|
61 IMAGE_FILE_DLL|IMAGE_FILE_DEBUG_STRIPPED;
62 nth->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
63 nth->OptionalHeader.SizeOfCode = 0;
64 nth->OptionalHeader.SizeOfInitializedData = 0;
65 nth->OptionalHeader.SizeOfUninitializedData = 0;
66 nth->OptionalHeader.AddressOfEntryPoint = 0;
67 nth->OptionalHeader.BaseOfCode = 0;
68 nth->OptionalHeader.MajorOperatingSystemVersion = 4;
69 nth->OptionalHeader.MajorImageVersion = 4;
70 nth->OptionalHeader.SizeOfImage = 0;
71 nth->OptionalHeader.SizeOfHeaders = 0;
72 nth->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_NATIVE;
73 nth->OptionalHeader.DllCharacteristics = 0;
74 nth->OptionalHeader.NumberOfRvaAndSizes = 0;
76 /* allocate one code section that crosses the whole process range
77 * (we could find out from internal tables ... hmm )
79 sh=(PIMAGE_SECTION_HEADER)(nth+1);
80 strcpy(sh->Name,".text");
81 sh->Misc.VirtualSize = 0x7fffffff;
82 sh->VirtualAddress = 0x42; /* so snoop can use it ... */
83 sh->SizeOfRawData = 0x7fffffff;
84 sh->PointerToRawData = 0;
85 sh->Characteristics = IMAGE_SCN_CNT_CODE|IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_EXECUTE|IMAGE_SCN_MEM_READ;
86 wm->module = hmod;
87 return wm;
91 #if defined(HAVE_LIBDL) && defined(HAVE_DLFCN_H)
93 #define UNIX_DLL_ENDING "so"
95 #define STUBSIZE 4095
97 #include <dlfcn.h>
99 HMODULE ELF_LoadLibraryExA( LPCSTR libname, HANDLE hf, DWORD flags )
101 WINE_MODREF *wm;
102 char *modname,*s,*t,*x;
103 LPVOID *dlhandle;
105 t = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
106 strlen(libname) + strlen("lib.so") + 1 );
107 *t = '\0';
108 /* copy path to tempvar ... */
109 s=strrchr(libname,'/');
110 if (!s)
111 s=strrchr(libname,'\\');
112 if (s) {
113 strncpy(t,libname,s-libname+1);
114 t[s-libname+1]= '\0';
115 } else
116 s = (LPSTR)libname;
117 modname = s;
118 /* append "lib" foo ".so" */
119 strcat(t,"lib");
120 x = t+strlen(t);
121 strcat(t,s);
122 s = strchr(x,'.');
123 if (s) {
124 while (s) {
125 if (!strcasecmp(s,".dll")) {
126 strcpy(s+1,UNIX_DLL_ENDING);
127 break;
129 s=strchr(s+1,'.');
131 } else {
132 strcat(x,"."UNIX_DLL_ENDING);
135 /* FIXME: make UNIX filename from DOS fn? */
137 /* ... and open it */
138 dlhandle = dlopen(t,RTLD_NOW);
139 if (!dlhandle) {
140 HeapFree( GetProcessHeap(), 0, t );
141 return 0;
144 wm = ELF_CreateDummyModule( t, modname );
145 wm->binfmt.elf.dlhandle = dlhandle;
147 SNOOP_RegisterDLL(wm->module,libname,STUBSIZE/sizeof(ELF_STDCALL_STUB));
148 return wm->module;
151 FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName)
153 LPVOID fun;
154 int i,nrofargs = 0;
155 ELF_STDCALL_STUB *stub;
157 assert(wm->type == MODULE32_ELF);
158 if (!HIWORD(funcName)) {
159 ERR(win32,"Can't import from UNIX dynamic libs by ordinal, sorry.\n");
160 return (FARPROC)0;
162 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
163 /* we sometimes have an excess '_' at the beginning of the name */
164 if (!fun && (funcName[0]=='_')) {
165 funcName++ ;
166 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
168 if (!fun) {
169 /* Function@nrofargs usually marks a stdcall function
170 * with nrofargs bytes that are popped at the end
172 if (strchr(funcName,'@')) {
173 LPSTR t,fn = HEAP_strdupA( GetProcessHeap(), 0, funcName );
175 t = strchr(fn,'@');
176 *t = '\0';
177 nrofargs = 0;
178 sscanf(t+1,"%d",&nrofargs);
179 fun = dlsym(wm->binfmt.elf.dlhandle,fn);
180 HeapFree( GetProcessHeap(), 0, fn );
183 /* We sometimes have Win32 dlls implemented using stdcall but UNIX
184 * dlls using cdecl. If we find out the number of args the function
185 * uses, we remove them from the stack using two small stubs.
187 if (!wm->binfmt.elf.stubs) {
188 /* one page should suffice */
189 wm->binfmt.elf.stubs = VirtualAlloc(NULL,STUBSIZE,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
190 memset(wm->binfmt.elf.stubs,0,STUBSIZE);
192 stub = wm->binfmt.elf.stubs;
193 for (i=0;i<STUBSIZE/sizeof(ELF_STDCALL_STUB);i++) {
194 if (!stub->origfun)
195 break;
196 if (stub->origfun == (DWORD)fun)
197 break;
198 stub++;
200 if (i==STUBSIZE/sizeof(ELF_STDCALL_STUB)) {
201 ERR(win32,"please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
202 assert(i<STUBSIZE/sizeof(ELF_STDCALL_STUB));
204 if (!stub->origfun)
205 stub->origfun=(DWORD)fun; /* just a marker */
207 if (fun && nrofargs) { /* we don't need it for 0 args */
208 /* Selfmodifying entry/return stub for stdcall -> cdecl
209 * conversion.
210 * - Pop returnaddress directly into our return code
211 * popl <into code below>
212 * - Replace it by pointer to start of our returncode
213 * push $newret
214 * - And call the original function
215 * jmp $orgfun
216 * - Remove the arguments no longer needed
217 * newret: add esp, <nrofargs>
218 * - Push the original returnvalue on the stack
219 * pushl <poppedvalue>
220 * - And return to it.
221 * ret
224 /* FIXME: The function stub is not reentrant. */
226 ((LPBYTE)&(stub->popl))[0] = 0x8f;
227 ((LPBYTE)&(stub->popl))[1] = 0x05;
228 stub->addr_popped = (DWORD)&(stub->oldret);
229 stub->pushl1 = 0x68;
230 stub->newret = (DWORD)&(stub->addesp);
231 stub->pushl2 = 0x68;
232 stub->origfun = (DWORD)fun;
233 stub->ret1 = 0xc3;
234 ((LPBYTE)&(stub->addesp))[0]=0x83;
235 ((LPBYTE)&(stub->addesp))[1]=0xc4;
236 stub->nrofargs = nrofargs;
237 stub->pushl3 = 0x68;
238 /* filled out by entrycode */
239 stub->oldret = 0xdeadbeef;
240 stub->ret2 = 0xc3;
241 fun=(FARPROC)stub;
243 if (!fun) {
244 FIXME(win32,"function %s not found: %s\n",funcName,dlerror());
245 return fun;
247 fun = SNOOP_GetProcAddress(wm->module,funcName,stub-wm->binfmt.elf.stubs,fun);
248 return (FARPROC)fun;
250 #else
252 HMODULE ELF_LoadLibraryExA( LPCSTR libname, HANDLE hf, DWORD flags)
254 return 0;
256 FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName)
258 return (FARPROC)0;
261 #endif