Implement autorepeat scrolling when button is held down.
[wine/multimedia.git] / loader / elf.c
blob47b1128749de4e8e0da490a2ca0103901af6aa76
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 "heap.h"
21 #include "file.h"
22 #include "module.h"
23 #include "debugtools.h"
24 #include "winerror.h"
25 #include "wine/port.h"
27 DEFAULT_DEBUG_CHANNEL(win32);
29 #ifdef HAVE_DL_API
31 typedef struct {
32 WORD popl WINE_PACKED; /* 0x8f 0x05 */
33 DWORD addr_popped WINE_PACKED;/* ... */
34 BYTE pushl1 WINE_PACKED; /* 0x68 */
35 DWORD newret WINE_PACKED; /* ... */
36 BYTE pushl2 WINE_PACKED; /* 0x68 */
37 DWORD origfun WINE_PACKED; /* original function */
38 BYTE ret1 WINE_PACKED; /* 0xc3 */
39 WORD addesp WINE_PACKED; /* 0x83 0xc4 */
40 BYTE nrofargs WINE_PACKED; /* nr of arguments to add esp, */
41 BYTE pushl3 WINE_PACKED; /* 0x68 */
42 DWORD oldret WINE_PACKED; /* Filled out from popl above */
43 BYTE ret2 WINE_PACKED; /* 0xc3 */
44 } ELF_STDCALL_STUB;
46 #define UNIX_DLL_ENDING "so"
48 #define STUBSIZE 4095
49 #define STUBOFFSET (sizeof(IMAGE_DOS_HEADER) + \
50 sizeof(IMAGE_NT_HEADERS) + \
51 sizeof(IMAGE_SECTION_HEADER))
53 static FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName, BOOL snoop );
55 static HMODULE ELF_CreateDummyModule( LPCSTR libname, LPCSTR modname )
57 PIMAGE_DOS_HEADER dh;
58 PIMAGE_NT_HEADERS nth;
59 PIMAGE_SECTION_HEADER sh;
60 HMODULE hmod;
62 hmod = (HMODULE)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
63 sizeof(IMAGE_DOS_HEADER) +
64 sizeof(IMAGE_NT_HEADERS) +
65 sizeof(IMAGE_SECTION_HEADER) + STUBSIZE );
66 dh = (PIMAGE_DOS_HEADER)hmod;
67 dh->e_magic = IMAGE_DOS_SIGNATURE;
68 dh->e_lfanew = sizeof(IMAGE_DOS_HEADER);
69 nth = PE_HEADER(hmod);
70 nth->Signature = IMAGE_NT_SIGNATURE;
71 nth->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
72 nth->FileHeader.NumberOfSections = 1;
73 nth->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
74 nth->FileHeader.Characteristics =
75 IMAGE_FILE_RELOCS_STRIPPED|IMAGE_FILE_LINE_NUMS_STRIPPED|
76 IMAGE_FILE_LOCAL_SYMS_STRIPPED|IMAGE_FILE_32BIT_MACHINE|
77 IMAGE_FILE_DLL|IMAGE_FILE_DEBUG_STRIPPED;
78 nth->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
79 nth->OptionalHeader.SizeOfCode = 0;
80 nth->OptionalHeader.SizeOfInitializedData = 0;
81 nth->OptionalHeader.SizeOfUninitializedData = 0;
82 nth->OptionalHeader.AddressOfEntryPoint = 0;
83 nth->OptionalHeader.BaseOfCode = 0;
84 nth->OptionalHeader.MajorOperatingSystemVersion = 4;
85 nth->OptionalHeader.MajorImageVersion = 4;
86 nth->OptionalHeader.SizeOfImage = 0;
87 nth->OptionalHeader.SizeOfHeaders = 0;
88 nth->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_NATIVE;
89 nth->OptionalHeader.DllCharacteristics = 0;
90 nth->OptionalHeader.NumberOfRvaAndSizes = 0;
92 /* allocate one code section that crosses the whole process range
93 * (we could find out from internal tables ... hmm )
95 sh=(PIMAGE_SECTION_HEADER)(nth+1);
96 strcpy(sh->Name,".text");
97 sh->Misc.VirtualSize = STUBSIZE;
98 sh->VirtualAddress = STUBOFFSET; /* so snoop can use it ... */
99 sh->SizeOfRawData = STUBSIZE;
100 sh->PointerToRawData = 0;
101 sh->Characteristics = IMAGE_SCN_CNT_CODE|IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_EXECUTE|IMAGE_SCN_MEM_READ;
102 return hmod;
105 WINE_MODREF *ELF_LoadLibraryExA( LPCSTR libname, DWORD flags)
107 WINE_MODREF *wm;
108 HMODULE hmod;
109 char *modname,*s,*t,*x;
110 LPVOID *dlhandle;
111 char error[256];
113 t = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
114 strlen(libname) + strlen("lib.so") + 1 );
115 *t = '\0';
116 /* copy path to tempvar ... */
117 s=strrchr(libname,'/');
118 if (!s)
119 s=strrchr(libname,'\\');
120 if (s) {
121 s++; /* skip / or \ */
122 /* copy everything up to s-1 */
123 memcpy(t,libname,s-libname);
124 t[s-libname]= '\0';
125 } else
126 s = (LPSTR)libname;
127 modname = s;
128 /* append "lib" foo ".so" */
129 strcat(t,"lib");
130 x = t+strlen(t);
131 strcat(t,s);
132 s = strchr(x,'.');
133 if (s) {
134 while (s) {
135 if (!FILE_strcasecmp(s,".dll")) {
136 strcpy(s+1,UNIX_DLL_ENDING);
137 break;
139 s=strchr(s+1,'.');
141 } else {
142 strcat(x,"."UNIX_DLL_ENDING);
145 /* grab just the last piece of the path/filename
146 which should be the name of the library we are
147 looking to load. increment by 1 to skip the DOS slash */
148 s = strrchr(t,'\\');
149 s++;
151 /* ... and open the library pointed by s, while t points
152 points to the ENTIRE DOS filename of the library
153 t is returned by HeapAlloc() above and so is also used
154 with HeapFree() below */
155 dlhandle = wine_dlopen(s,RTLD_NOW,error,sizeof(error));
156 if (!dlhandle) {
157 WARN("failed to load %s: %s\n", s, error);
158 HeapFree( GetProcessHeap(), 0, t );
159 SetLastError( ERROR_FILE_NOT_FOUND );
160 return NULL;
163 hmod = ELF_CreateDummyModule( t, modname );
165 SNOOP_RegisterDLL(hmod,libname,STUBSIZE/sizeof(ELF_STDCALL_STUB));
167 wm = PE_CreateModule( hmod, libname, 0, 0, FALSE );
168 wm->find_export = ELF_FindExportedFunction;
169 wm->dlhandle = dlhandle;
170 return wm;
173 static FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName, BOOL snoop )
175 LPVOID fun;
176 int i,nrofargs = 0;
177 ELF_STDCALL_STUB *stub, *first_stub;
178 char error[256];
180 if (!HIWORD(funcName)) {
181 ERR("Can't import from UNIX dynamic libs by ordinal, sorry.\n");
182 return (FARPROC)0;
184 fun = wine_dlsym(wm->dlhandle,funcName,error,sizeof(error));
185 if (!fun)
187 /* we sometimes have an excess '_' at the beginning of the name */
188 if (funcName[0]=='_')
190 funcName++ ;
191 fun = wine_dlsym(wm->dlhandle,funcName,error,sizeof(error));
194 if (!fun) {
195 /* Function@nrofargs usually marks a stdcall function
196 * with nrofargs bytes that are popped at the end
198 if (strchr(funcName,'@')) {
199 LPSTR t,fn = HEAP_strdupA( GetProcessHeap(), 0, funcName );
201 t = strchr(fn,'@');
202 *t = '\0';
203 nrofargs = 0;
204 sscanf(t+1,"%d",&nrofargs);
205 fun = wine_dlsym(wm->dlhandle,fn,error,sizeof(error));
206 HeapFree( GetProcessHeap(), 0, fn );
209 /* We sometimes have Win32 dlls implemented using stdcall but UNIX
210 * dlls using cdecl. If we find out the number of args the function
211 * uses, we remove them from the stack using two small stubs.
213 stub = first_stub = (ELF_STDCALL_STUB *)((char *)wm->module + STUBOFFSET);
214 for (i=0;i<STUBSIZE/sizeof(ELF_STDCALL_STUB);i++) {
215 if (!stub->origfun)
216 break;
217 if (stub->origfun == (DWORD)fun)
218 break;
219 stub++;
221 if (i==STUBSIZE/sizeof(ELF_STDCALL_STUB)) {
222 ERR("please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
223 assert(i<STUBSIZE/sizeof(ELF_STDCALL_STUB));
225 if (!stub->origfun)
226 stub->origfun=(DWORD)fun; /* just a marker */
228 if (fun && nrofargs) { /* we don't need it for 0 args */
229 /* Selfmodifying entry/return stub for stdcall -> cdecl
230 * conversion.
231 * - Pop returnaddress directly into our return code
232 * popl <into code below>
233 * - Replace it by pointer to start of our returncode
234 * push $newret
235 * - And call the original function
236 * jmp $orgfun
237 * - Remove the arguments no longer needed
238 * newret: add esp, <nrofargs>
239 * - Push the original returnvalue on the stack
240 * pushl <poppedvalue>
241 * - And return to it.
242 * ret
245 /* FIXME: The function stub is not reentrant. */
247 ((LPBYTE)&(stub->popl))[0] = 0x8f;
248 ((LPBYTE)&(stub->popl))[1] = 0x05;
249 stub->addr_popped = (DWORD)&(stub->oldret);
250 stub->pushl1 = 0x68;
251 stub->newret = (DWORD)&(stub->addesp);
252 stub->pushl2 = 0x68;
253 stub->origfun = (DWORD)fun;
254 stub->ret1 = 0xc3;
255 ((LPBYTE)&(stub->addesp))[0]=0x83;
256 ((LPBYTE)&(stub->addesp))[1]=0xc4;
257 stub->nrofargs = nrofargs;
258 stub->pushl3 = 0x68;
259 /* filled out by entrycode */
260 stub->oldret = 0xdeadbeef;
261 stub->ret2 = 0xc3;
262 fun=(FARPROC)stub;
264 if (!fun) {
265 FIXME("function %s not found: %s\n",funcName,error);
266 return fun;
268 fun = SNOOP_GetProcAddress(wm->module,funcName,stub-first_stub,fun);
269 return (FARPROC)fun;
272 #else /* HAVE_DL_API */
274 WINE_MODREF *ELF_LoadLibraryExA( LPCSTR libname, DWORD flags)
276 return NULL;
279 #endif /* HAVE_DL_API */