Moved error codes to cderr.h.
[wine/multimedia.git] / loader / elf.c
blob853ae064da32a29bdd782bb4d76c2659168bae6b
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 while (s) {
124 if (!strcasecmp(s,".dll")) {
125 strcpy(s+1,UNIX_DLL_ENDING);
126 break;
128 s=strchr(s+1,'.');
131 /* FIXME: make UNIX filename from DOS fn? */
133 /* ... and open it */
134 dlhandle = dlopen(t,RTLD_NOW);
135 if (!dlhandle) {
136 HeapFree( GetProcessHeap(), 0, t );
137 return 0;
140 wm = ELF_CreateDummyModule( t, modname );
141 wm->binfmt.elf.dlhandle = dlhandle;
143 SNOOP_RegisterDLL(wm->module,libname,STUBSIZE/sizeof(ELF_STDCALL_STUB));
144 return wm->module;
147 FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName)
149 LPVOID fun;
150 int i,nrofargs = 0;
151 ELF_STDCALL_STUB *stub;
153 assert(wm->type == MODULE32_ELF);
154 if (!HIWORD(funcName)) {
155 ERR(win32,"Can't import from UNIX dynamic libs by ordinal, sorry.\n");
156 return (FARPROC)0;
158 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
159 /* we sometimes have an excess '_' at the beginning of the name */
160 if (!fun && (funcName[0]=='_')) {
161 funcName++ ;
162 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
164 if (!fun) {
165 /* Function@nrofargs usually marks a stdcall function
166 * with nrofargs bytes that are popped at the end
168 if (strchr(funcName,'@')) {
169 LPSTR t,fn = HEAP_strdupA( GetProcessHeap(), 0, funcName );
171 t = strchr(fn,'@');
172 *t = '\0';
173 nrofargs = 0;
174 sscanf(t+1,"%d",&nrofargs);
175 fun = dlsym(wm->binfmt.elf.dlhandle,fn);
176 HeapFree( GetProcessHeap(), 0, fn );
179 /* We sometimes have Win32 dlls implemented using stdcall but UNIX
180 * dlls using cdecl. If we find out the number of args the function
181 * uses, we remove them from the stack using two small stubs.
183 if (!wm->binfmt.elf.stubs) {
184 /* one page should suffice */
185 wm->binfmt.elf.stubs = VirtualAlloc(NULL,STUBSIZE,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
186 memset(wm->binfmt.elf.stubs,0,STUBSIZE);
188 stub = wm->binfmt.elf.stubs;
189 for (i=0;i<STUBSIZE/sizeof(ELF_STDCALL_STUB);i++) {
190 if (!stub->origfun)
191 break;
192 if (stub->origfun == (DWORD)fun)
193 break;
194 stub++;
196 if (i==STUBSIZE/sizeof(ELF_STDCALL_STUB)) {
197 ERR(win32,"please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
198 assert(i<STUBSIZE/sizeof(ELF_STDCALL_STUB));
200 if (!stub->origfun)
201 stub->origfun=(DWORD)fun; /* just a marker */
203 if (fun && nrofargs) { /* we don't need it for 0 args */
204 /* Selfmodifying entry/return stub for stdcall -> cdecl
205 * conversion.
206 * - Pop returnaddress directly into our return code
207 * popl <into code below>
208 * - Replace it by pointer to start of our returncode
209 * push $newret
210 * - And call the original function
211 * jmp $orgfun
212 * - Remove the arguments no longer needed
213 * newret: add esp, <nrofargs>
214 * - Push the original returnvalue on the stack
215 * pushl <poppedvalue>
216 * - And return to it.
217 * ret
220 /* FIXME: The function stub is not reentrant. */
222 ((LPBYTE)&(stub->popl))[0] = 0x8f;
223 ((LPBYTE)&(stub->popl))[1] = 0x05;
224 stub->addr_popped = (DWORD)&(stub->oldret);
225 stub->pushl1 = 0x68;
226 stub->newret = (DWORD)&(stub->addesp);
227 stub->pushl2 = 0x68;
228 stub->origfun = (DWORD)fun;
229 stub->ret1 = 0xc3;
230 ((LPBYTE)&(stub->addesp))[0]=0x83;
231 ((LPBYTE)&(stub->addesp))[1]=0xc4;
232 stub->nrofargs = nrofargs;
233 stub->pushl3 = 0x68;
234 /* filled out by entrycode */
235 stub->oldret = 0xdeadbeef;
236 stub->ret2 = 0xc3;
237 fun=(FARPROC)stub;
239 if (!fun) {
240 FIXME(win32,"function %s not found: %s\n",funcName,dlerror());
241 return fun;
243 fun = SNOOP_GetProcAddress(wm->module,funcName,stub-wm->binfmt.elf.stubs,fun);
244 return (FARPROC)fun;
246 #else
248 HMODULE ELF_LoadLibraryExA( LPCSTR libname, HANDLE hf, DWORD flags)
250 return 0;
252 FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName)
254 return (FARPROC)0;
257 #endif