Small fixes.
[wine/multimedia.git] / loader / elf.c
blob395f603ed5f344f8caa4c3b68ffa316eccf91ec5
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 "windows.h"
20 #include "snoop.h"
21 #include "process.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 *
29 ELF_CreateDummyModule( LPCSTR libname, LPCSTR modname, PDB32 *process )
31 PIMAGE_DOS_HEADER dh;
32 PIMAGE_NT_HEADERS nth;
33 PIMAGE_SECTION_HEADER sh;
34 WINE_MODREF *wm;
35 HMODULE32 hmod;
37 wm=(WINE_MODREF*)HeapAlloc(process->heap,HEAP_ZERO_MEMORY,sizeof(*wm));
38 wm->type = MODULE32_ELF;
40 /* FIXME: hmm, order? */
41 wm->next = process->modref_list;
42 process->modref_list = wm;
44 wm->modname = HEAP_strdupA(process->heap,0,modname);
45 wm->longname = HEAP_strdupA(process->heap,0,libname);
47 hmod = (HMODULE32)HeapAlloc(process->heap,HEAP_ZERO_MEMORY,sizeof(IMAGE_DOS_HEADER)+sizeof(IMAGE_NT_HEADERS)+sizeof(IMAGE_SECTION_HEADER)+100);
48 dh = (PIMAGE_DOS_HEADER)hmod;
49 dh->e_magic = IMAGE_DOS_SIGNATURE;
50 dh->e_lfanew = sizeof(IMAGE_DOS_HEADER);
51 nth = PE_HEADER(hmod);
52 nth->Signature = IMAGE_NT_SIGNATURE;
53 nth->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
54 nth->FileHeader.NumberOfSections = 1;
55 nth->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
56 nth->FileHeader.Characteristics =
57 IMAGE_FILE_RELOCS_STRIPPED|IMAGE_FILE_LINE_NUMS_STRIPPED|
58 IMAGE_FILE_LOCAL_SYMS_STRIPPED|IMAGE_FILE_32BIT_MACHINE|
59 IMAGE_FILE_DLL|IMAGE_FILE_DEBUG_STRIPPED;
60 nth->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
61 nth->OptionalHeader.SizeOfCode = 0;
62 nth->OptionalHeader.SizeOfInitializedData = 0;
63 nth->OptionalHeader.SizeOfUninitializedData = 0;
64 nth->OptionalHeader.AddressOfEntryPoint = 0;
65 nth->OptionalHeader.BaseOfCode = 0;
66 nth->OptionalHeader.MajorOperatingSystemVersion = 4;
67 nth->OptionalHeader.MajorImageVersion = 4;
68 nth->OptionalHeader.SizeOfImage = 0;
69 nth->OptionalHeader.SizeOfHeaders = 0;
70 nth->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_NATIVE;
71 nth->OptionalHeader.DllCharacteristics = 0;
72 nth->OptionalHeader.NumberOfRvaAndSizes = 0;
74 /* allocate one code section that crosses the whole process range
75 * (we could find out from internal tables ... hmm )
77 sh=(PIMAGE_SECTION_HEADER)(nth+1);
78 strcpy(sh->Name,".text");
79 sh->Misc.VirtualSize = 0x7fffffff;
80 sh->VirtualAddress = 0x42; /* so snoop can use it ... */
81 sh->SizeOfRawData = 0x7fffffff;
82 sh->PointerToRawData = 0;
83 sh->Characteristics = IMAGE_SCN_CNT_CODE|IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_EXECUTE|IMAGE_SCN_MEM_READ;
84 wm->module = hmod;
85 return wm;
89 #if defined(HAVE_LIBDL) && defined(HAVE_DLFCN_H)
91 #define UNIX_DLL_ENDING "so"
93 #define STUBSIZE 4095
95 #include <dlfcn.h>
97 HMODULE32
98 ELF_LoadLibraryEx32A(LPCSTR libname,PDB32 *process,HANDLE32 hf,DWORD flags) {
99 WINE_MODREF *wm;
100 char *modname,*s,*t,*x;
101 LPVOID *dlhandle;
103 t = HeapAlloc(process->heap,HEAP_ZERO_MEMORY,strlen(libname)+strlen("lib.so")+1);
104 *t = '\0';
105 /* copy path to tempvar ... */
106 s=strrchr(libname,'/');
107 if (!s)
108 s=strrchr(libname,'\\');
109 if (s) {
110 strncpy(t,libname,s-libname+1);
111 t[s-libname+1]= '\0';
112 } else
113 s = (LPSTR)libname;
114 modname = s;
115 /* append "lib" foo ".so" */
116 strcat(t,"lib");
117 x = t+strlen(t);
118 strcat(t,s);
119 s = strchr(x,'.');
120 while (s) {
121 if (!strcasecmp(s,".dll")) {
122 strcpy(s+1,UNIX_DLL_ENDING);
123 break;
125 s=strchr(s+1,'.');
128 /* FIXME: make UNIX filename from DOS fn? */
130 /* ... and open it */
131 dlhandle = dlopen(t,RTLD_NOW);
132 if (!dlhandle) {
133 HeapFree(process->heap,0,t);
134 return 0;
137 wm = ELF_CreateDummyModule( t, modname, process );
138 wm->binfmt.elf.dlhandle = dlhandle;
140 SNOOP_RegisterDLL(wm->module,libname,STUBSIZE/sizeof(ELF_STDCALL_STUB));
141 return wm->module;
144 FARPROC32
145 ELF_FindExportedFunction( PDB32 *process,WINE_MODREF *wm, LPCSTR funcName) {
146 LPVOID fun;
147 int i,nrofargs = 0;
148 ELF_STDCALL_STUB *stub;
150 assert(wm->type == MODULE32_ELF);
151 if (!HIWORD(funcName)) {
152 ERR(win32,"Can't import from UNIX dynamic libs by ordinal, sorry.\n");
153 return (FARPROC32)0;
155 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
156 /* we sometimes have an excess '_' at the beginning of the name */
157 if (!fun && (funcName[0]=='_')) {
158 funcName++ ;
159 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
161 if (!fun) {
162 /* Function@nrofargs usually marks a stdcall function
163 * with nrofargs bytes that are popped at the end
165 if (strchr(funcName,'@')) {
166 LPSTR t,fn = HEAP_strdupA(process->heap,0,funcName);
168 t = strchr(fn,'@');
169 *t = '\0';
170 nrofargs = 0;
171 sscanf(t+1,"%d",&nrofargs);
172 fun = dlsym(wm->binfmt.elf.dlhandle,fn);
173 HeapFree(process->heap,0,fn);
176 /* We sometimes have Win32 dlls implemented using stdcall but UNIX
177 * dlls using cdecl. If we find out the number of args the function
178 * uses, we remove them from the stack using two small stubs.
180 if (!wm->binfmt.elf.stubs) {
181 /* one page should suffice */
182 wm->binfmt.elf.stubs = VirtualAlloc(NULL,STUBSIZE,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
183 memset(wm->binfmt.elf.stubs,0,STUBSIZE);
185 stub = wm->binfmt.elf.stubs;
186 for (i=0;i<STUBSIZE/sizeof(ELF_STDCALL_STUB);i++) {
187 if (!stub->origfun)
188 break;
189 if (stub->origfun == (DWORD)fun)
190 break;
191 stub++;
193 if (i==STUBSIZE/sizeof(ELF_STDCALL_STUB)) {
194 ERR(win32,"please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
195 assert(i<STUBSIZE/sizeof(ELF_STDCALL_STUB));
197 if (!stub->origfun)
198 stub->origfun=(DWORD)fun; /* just a marker */
200 if (fun && nrofargs) { /* we don't need it for 0 args */
201 /* Selfmodifying entry/return stub for stdcall -> cdecl
202 * conversion.
203 * - Pop returnaddress directly into our return code
204 * popl <into code below>
205 * - Replace it by pointer to start of our returncode
206 * push $newret
207 * - And call the original function
208 * jmp $orgfun
209 * - Remove the arguments no longer needed
210 * newret: add esp, <nrofargs>
211 * - Push the original returnvalue on the stack
212 * pushl <poppedvalue>
213 * - And return to it.
214 * ret
217 /* FIXME: The function stub is not reentrant. */
219 ((LPBYTE)&(stub->popl))[0] = 0x8f;
220 ((LPBYTE)&(stub->popl))[1] = 0x05;
221 stub->addr_popped = (DWORD)&(stub->oldret);
222 stub->pushl1 = 0x68;
223 stub->newret = (DWORD)&(stub->addesp);
224 stub->pushl2 = 0x68;
225 stub->origfun = (DWORD)fun;
226 stub->ret1 = 0xc3;
227 ((LPBYTE)&(stub->addesp))[0]=0x83;
228 ((LPBYTE)&(stub->addesp))[1]=0xc4;
229 stub->nrofargs = nrofargs;
230 stub->pushl3 = 0x68;
231 /* filled out by entrycode */
232 stub->oldret = 0xdeadbeef;
233 stub->ret2 = 0xc3;
234 fun=(FARPROC32)stub;
236 if (!fun) {
237 FIXME(win32,"function %s not found: %s\n",funcName,dlerror());
238 return fun;
240 fun = SNOOP_GetProcAddress32(wm->module,funcName,stub-wm->binfmt.elf.stubs,fun);
241 return (FARPROC32)fun;
243 #else
245 HMODULE32
246 ELF_LoadLibraryEx32A(LPCSTR libname,PDB32 *process,HANDLE32 hf,DWORD flags) {
247 return 0;
249 FARPROC32
250 ELF_FindExportedFunction( PDB32 *process,WINE_MODREF *wm, LPCSTR funcName) {
251 return (FARPROC32)0;
254 #endif