blob: f302f06a9184ec32caf3c951ec2ee4e41cd93618 [file] [log] [blame]
Vincent Béron9a624912002-05-31 23:06:46 +00001/*
Marcus Meissner8220bc91998-10-11 11:10:27 +00002 * UNIX dynamic loader
Vincent Béron9a624912002-05-31 23:06:46 +00003 *
Marcus Meissner8220bc91998-10-11 11:10:27 +00004 * Currently only supports stuff using the dl* API.
5 *
6 * Copyright 1998 Marcus Meissner
7 *
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00008 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
Marcus Meissner8220bc91998-10-11 11:10:27 +000022 * FIXME: Small reentrancy problem.
Vincent Béron9a624912002-05-31 23:06:46 +000023 * IDEA(s): could be used to split up shell32,comctl32...
Marcus Meissner8220bc91998-10-11 11:10:27 +000024 */
25
François Gouget14259412001-11-06 20:57:11 +000026#include "config.h"
Vincent Béron9a624912002-05-31 23:06:46 +000027#include "wine/port.h"
Marcus Meissner8220bc91998-10-11 11:10:27 +000028
29#include <assert.h>
30#include <stdio.h>
31#include <string.h>
32#include <sys/types.h>
33
Marcus Meissner8220bc91998-10-11 11:10:27 +000034#include "snoop.h"
Aric Stewarte4d09322000-12-03 03:14:29 +000035#include "file.h"
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000036#include "wine/debug.h"
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +000037#include "winerror.h"
Marcus Meissner8220bc91998-10-11 11:10:27 +000038
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000039WINE_DEFAULT_DEBUG_CHANNEL(win32);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000040
Alexandre Julliard081ee942000-08-07 04:12:41 +000041typedef struct {
42 WORD popl WINE_PACKED; /* 0x8f 0x05 */
43 DWORD addr_popped WINE_PACKED;/* ... */
44 BYTE pushl1 WINE_PACKED; /* 0x68 */
45 DWORD newret WINE_PACKED; /* ... */
46 BYTE pushl2 WINE_PACKED; /* 0x68 */
47 DWORD origfun WINE_PACKED; /* original function */
48 BYTE ret1 WINE_PACKED; /* 0xc3 */
49 WORD addesp WINE_PACKED; /* 0x83 0xc4 */
50 BYTE nrofargs WINE_PACKED; /* nr of arguments to add esp, */
51 BYTE pushl3 WINE_PACKED; /* 0x68 */
52 DWORD oldret WINE_PACKED; /* Filled out from popl above */
53 BYTE ret2 WINE_PACKED; /* 0xc3 */
54} ELF_STDCALL_STUB;
Ulrich Weigand9ffd4032000-02-03 01:33:48 +000055
56#define UNIX_DLL_ENDING "so"
57
58#define STUBSIZE 4095
Alexandre Julliard081ee942000-08-07 04:12:41 +000059#define STUBOFFSET (sizeof(IMAGE_DOS_HEADER) + \
60 sizeof(IMAGE_NT_HEADERS) + \
61 sizeof(IMAGE_SECTION_HEADER))
Ulrich Weigand9ffd4032000-02-03 01:33:48 +000062
Alexandre Julliard891d23e2002-07-24 19:04:41 +000063static FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName, int hint, BOOL snoop );
Ulrich Weigand9ffd4032000-02-03 01:33:48 +000064
Alexandre Julliard081ee942000-08-07 04:12:41 +000065static HMODULE ELF_CreateDummyModule( LPCSTR libname, LPCSTR modname )
Ulrich Weigand7df1fbb1998-11-01 18:01:53 +000066{
Uwe Bonnes73d6c131998-10-16 09:30:33 +000067 PIMAGE_DOS_HEADER dh;
68 PIMAGE_NT_HEADERS nth;
69 PIMAGE_SECTION_HEADER sh;
Alexandre Julliarda3960291999-02-26 11:11:13 +000070 HMODULE hmod;
Marcus Meissner8220bc91998-10-11 11:10:27 +000071
Vincent Béron9a624912002-05-31 23:06:46 +000072 hmod = (HMODULE)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
73 sizeof(IMAGE_DOS_HEADER) +
Ulrich Weigand1d90d691999-02-24 14:27:07 +000074 sizeof(IMAGE_NT_HEADERS) +
Alexandre Julliard081ee942000-08-07 04:12:41 +000075 sizeof(IMAGE_SECTION_HEADER) + STUBSIZE );
Uwe Bonnes73d6c131998-10-16 09:30:33 +000076 dh = (PIMAGE_DOS_HEADER)hmod;
Marcus Meissner8220bc91998-10-11 11:10:27 +000077 dh->e_magic = IMAGE_DOS_SIGNATURE;
78 dh->e_lfanew = sizeof(IMAGE_DOS_HEADER);
Alexandre Julliarda5dea212002-08-09 19:57:38 +000079 nth = (IMAGE_NT_HEADERS *)(dh + 1);
Vincent Béron9a624912002-05-31 23:06:46 +000080 nth->Signature = IMAGE_NT_SIGNATURE;
Marcus Meissner8220bc91998-10-11 11:10:27 +000081 nth->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
82 nth->FileHeader.NumberOfSections = 1;
83 nth->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
Vincent Béron9a624912002-05-31 23:06:46 +000084 nth->FileHeader.Characteristics =
Marcus Meissner8220bc91998-10-11 11:10:27 +000085 IMAGE_FILE_RELOCS_STRIPPED|IMAGE_FILE_LINE_NUMS_STRIPPED|
86 IMAGE_FILE_LOCAL_SYMS_STRIPPED|IMAGE_FILE_32BIT_MACHINE|
87 IMAGE_FILE_DLL|IMAGE_FILE_DEBUG_STRIPPED;
88 nth->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
89 nth->OptionalHeader.SizeOfCode = 0;
90 nth->OptionalHeader.SizeOfInitializedData = 0;
91 nth->OptionalHeader.SizeOfUninitializedData = 0;
92 nth->OptionalHeader.AddressOfEntryPoint = 0;
93 nth->OptionalHeader.BaseOfCode = 0;
94 nth->OptionalHeader.MajorOperatingSystemVersion = 4;
95 nth->OptionalHeader.MajorImageVersion = 4;
96 nth->OptionalHeader.SizeOfImage = 0;
97 nth->OptionalHeader.SizeOfHeaders = 0;
98 nth->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_NATIVE;
99 nth->OptionalHeader.DllCharacteristics = 0;
100 nth->OptionalHeader.NumberOfRvaAndSizes = 0;
101
102 /* allocate one code section that crosses the whole process range
103 * (we could find out from internal tables ... hmm )
104 */
Uwe Bonnes73d6c131998-10-16 09:30:33 +0000105 sh=(PIMAGE_SECTION_HEADER)(nth+1);
Marcus Meissner8220bc91998-10-11 11:10:27 +0000106 strcpy(sh->Name,".text");
Alexandre Julliard081ee942000-08-07 04:12:41 +0000107 sh->Misc.VirtualSize = STUBSIZE;
108 sh->VirtualAddress = STUBOFFSET; /* so snoop can use it ... */
109 sh->SizeOfRawData = STUBSIZE;
Marcus Meissner8220bc91998-10-11 11:10:27 +0000110 sh->PointerToRawData = 0;
111 sh->Characteristics = IMAGE_SCN_CNT_CODE|IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_EXECUTE|IMAGE_SCN_MEM_READ;
Alexandre Julliard081ee942000-08-07 04:12:41 +0000112 return hmod;
Ulrich Weigand7df1fbb1998-11-01 18:01:53 +0000113}
114
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000115WINE_MODREF *ELF_LoadLibraryExA( LPCSTR libname, DWORD flags)
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000116{
Ulrich Weigand7df1fbb1998-11-01 18:01:53 +0000117 WINE_MODREF *wm;
Alexandre Julliard081ee942000-08-07 04:12:41 +0000118 HMODULE hmod;
Ulrich Weigand7df1fbb1998-11-01 18:01:53 +0000119 char *modname,*s,*t,*x;
120 LPVOID *dlhandle;
James Abbatielloe6758872000-12-13 21:32:55 +0000121 char error[256];
Ulrich Weigand7df1fbb1998-11-01 18:01:53 +0000122
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000123 t = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
124 strlen(libname) + strlen("lib.so") + 1 );
Ulrich Weigand7df1fbb1998-11-01 18:01:53 +0000125 *t = '\0';
126 /* copy path to tempvar ... */
127 s=strrchr(libname,'/');
128 if (!s)
129 s=strrchr(libname,'\\');
130 if (s) {
Marcus Meissner623c0d61999-07-03 16:01:42 +0000131 s++; /* skip / or \ */
132 /* copy everything up to s-1 */
133 memcpy(t,libname,s-libname);
134 t[s-libname]= '\0';
Ulrich Weigand7df1fbb1998-11-01 18:01:53 +0000135 } else
136 s = (LPSTR)libname;
137 modname = s;
138 /* append "lib" foo ".so" */
139 strcat(t,"lib");
140 x = t+strlen(t);
141 strcat(t,s);
142 s = strchr(x,'.');
Marcus Meissner9c6735e1999-04-01 10:09:46 +0000143 if (s) {
144 while (s) {
Aric Stewarte4d09322000-12-03 03:14:29 +0000145 if (!FILE_strcasecmp(s,".dll")) {
Marcus Meissner9c6735e1999-04-01 10:09:46 +0000146 strcpy(s+1,UNIX_DLL_ENDING);
147 break;
Ulrich Weigand7df1fbb1998-11-01 18:01:53 +0000148 }
149 s=strchr(s+1,'.');
Marcus Meissner9c6735e1999-04-01 10:09:46 +0000150 }
151 } else {
152 strcat(x,"."UNIX_DLL_ENDING);
Ulrich Weigand7df1fbb1998-11-01 18:01:53 +0000153 }
154
Andreas Mohr786c9a62000-11-06 05:26:27 +0000155 /* grab just the last piece of the path/filename
156 which should be the name of the library we are
157 looking to load. increment by 1 to skip the DOS slash */
158 s = strrchr(t,'\\');
159 s++;
Ulrich Weigand7df1fbb1998-11-01 18:01:53 +0000160
Andreas Mohr786c9a62000-11-06 05:26:27 +0000161 /* ... and open the library pointed by s, while t points
162 points to the ENTIRE DOS filename of the library
163 t is returned by HeapAlloc() above and so is also used
164 with HeapFree() below */
James Abbatielloe6758872000-12-13 21:32:55 +0000165 dlhandle = wine_dlopen(s,RTLD_NOW,error,sizeof(error));
Ulrich Weigand7df1fbb1998-11-01 18:01:53 +0000166 if (!dlhandle) {
James Abbatielloe6758872000-12-13 21:32:55 +0000167 WARN("failed to load %s: %s\n", s, error);
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000168 HeapFree( GetProcessHeap(), 0, t );
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000169 SetLastError( ERROR_FILE_NOT_FOUND );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000170 return NULL;
Ulrich Weigand7df1fbb1998-11-01 18:01:53 +0000171 }
172
Alexandre Julliard081ee942000-08-07 04:12:41 +0000173 hmod = ELF_CreateDummyModule( t, modname );
Ulrich Weigand7df1fbb1998-11-01 18:01:53 +0000174
Andreas Mohr9abd5532001-05-24 18:43:16 +0000175 SNOOP_RegisterDLL(hmod,libname,0,STUBSIZE/sizeof(ELF_STDCALL_STUB));
Alexandre Julliard081ee942000-08-07 04:12:41 +0000176
Alexandre Julliard8081e5a2001-01-05 04:08:07 +0000177 wm = PE_CreateModule( hmod, libname, 0, 0, FALSE );
Alexandre Julliard081ee942000-08-07 04:12:41 +0000178 wm->find_export = ELF_FindExportedFunction;
179 wm->dlhandle = dlhandle;
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000180 return wm;
Marcus Meissner8220bc91998-10-11 11:10:27 +0000181}
182
Alexandre Julliard891d23e2002-07-24 19:04:41 +0000183static FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName, int hint, BOOL snoop )
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000184{
Marcus Meissner8220bc91998-10-11 11:10:27 +0000185 LPVOID fun;
186 int i,nrofargs = 0;
Alexandre Julliard081ee942000-08-07 04:12:41 +0000187 ELF_STDCALL_STUB *stub, *first_stub;
James Abbatielloe6758872000-12-13 21:32:55 +0000188 char error[256];
Marcus Meissner8220bc91998-10-11 11:10:27 +0000189
Marcus Meissner8220bc91998-10-11 11:10:27 +0000190 if (!HIWORD(funcName)) {
Alexandre Julliarda099a551999-06-12 15:45:58 +0000191 ERR("Can't import from UNIX dynamic libs by ordinal, sorry.\n");
Alexandre Julliarda3960291999-02-26 11:11:13 +0000192 return (FARPROC)0;
Marcus Meissner8220bc91998-10-11 11:10:27 +0000193 }
James Abbatielloe6758872000-12-13 21:32:55 +0000194 fun = wine_dlsym(wm->dlhandle,funcName,error,sizeof(error));
Alexandre Julliard886604c2000-12-05 21:17:59 +0000195 if (!fun)
196 {
Alexandre Julliard886604c2000-12-05 21:17:59 +0000197 /* we sometimes have an excess '_' at the beginning of the name */
198 if (funcName[0]=='_')
199 {
Marcus Meissner8220bc91998-10-11 11:10:27 +0000200 funcName++ ;
James Abbatielloe6758872000-12-13 21:32:55 +0000201 fun = wine_dlsym(wm->dlhandle,funcName,error,sizeof(error));
Alexandre Julliard886604c2000-12-05 21:17:59 +0000202 }
203 }
Marcus Meissner8220bc91998-10-11 11:10:27 +0000204 if (!fun) {
Vincent Béron9a624912002-05-31 23:06:46 +0000205 /* Function@nrofargs usually marks a stdcall function
Marcus Meissner8220bc91998-10-11 11:10:27 +0000206 * with nrofargs bytes that are popped at the end
207 */
Alexandre Julliard5f728ca2001-07-24 21:45:22 +0000208 LPCSTR t;
209 if ((t = strchr(funcName,'@')))
210 {
211 LPSTR fn = HeapAlloc( GetProcessHeap(), 0, t - funcName + 1 );
212 memcpy( fn, funcName, t - funcName );
213 fn[t - funcName] = 0;
214 nrofargs = 0;
215 sscanf(t+1,"%d",&nrofargs);
216 fun = wine_dlsym(wm->dlhandle,fn,error,sizeof(error));
217 HeapFree( GetProcessHeap(), 0, fn );
218 }
Marcus Meissner8220bc91998-10-11 11:10:27 +0000219 }
Vincent Béron9a624912002-05-31 23:06:46 +0000220 /* We sometimes have Win32 dlls implemented using stdcall but UNIX
Marcus Meissner8220bc91998-10-11 11:10:27 +0000221 * dlls using cdecl. If we find out the number of args the function
222 * uses, we remove them from the stack using two small stubs.
223 */
Alexandre Julliard081ee942000-08-07 04:12:41 +0000224 stub = first_stub = (ELF_STDCALL_STUB *)((char *)wm->module + STUBOFFSET);
Marcus Meissner8220bc91998-10-11 11:10:27 +0000225 for (i=0;i<STUBSIZE/sizeof(ELF_STDCALL_STUB);i++) {
226 if (!stub->origfun)
227 break;
228 if (stub->origfun == (DWORD)fun)
229 break;
230 stub++;
231 }
232 if (i==STUBSIZE/sizeof(ELF_STDCALL_STUB)) {
Alexandre Julliarda099a551999-06-12 15:45:58 +0000233 ERR("please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
Marcus Meissner8220bc91998-10-11 11:10:27 +0000234 assert(i<STUBSIZE/sizeof(ELF_STDCALL_STUB));
235 }
236 if (!stub->origfun)
237 stub->origfun=(DWORD)fun; /* just a marker */
238
239 if (fun && nrofargs) { /* we don't need it for 0 args */
Vincent Béron9a624912002-05-31 23:06:46 +0000240 /* Selfmodifying entry/return stub for stdcall -> cdecl
Marcus Meissner8220bc91998-10-11 11:10:27 +0000241 * conversion.
242 * - Pop returnaddress directly into our return code
243 * popl <into code below>
244 * - Replace it by pointer to start of our returncode
245 * push $newret
246 * - And call the original function
247 * jmp $orgfun
248 * - Remove the arguments no longer needed
249 * newret: add esp, <nrofargs>
250 * - Push the original returnvalue on the stack
251 * pushl <poppedvalue>
252 * - And return to it.
253 * ret
254 */
255
256 /* FIXME: The function stub is not reentrant. */
257
258 ((LPBYTE)&(stub->popl))[0] = 0x8f;
259 ((LPBYTE)&(stub->popl))[1] = 0x05;
260 stub->addr_popped = (DWORD)&(stub->oldret);
261 stub->pushl1 = 0x68;
262 stub->newret = (DWORD)&(stub->addesp);
263 stub->pushl2 = 0x68;
264 stub->origfun = (DWORD)fun;
265 stub->ret1 = 0xc3;
266 ((LPBYTE)&(stub->addesp))[0]=0x83;
267 ((LPBYTE)&(stub->addesp))[1]=0xc4;
268 stub->nrofargs = nrofargs;
269 stub->pushl3 = 0x68;
270 /* filled out by entrycode */
271 stub->oldret = 0xdeadbeef;
272 stub->ret2 = 0xc3;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000273 fun=(FARPROC)stub;
Marcus Meissner8220bc91998-10-11 11:10:27 +0000274 }
275 if (!fun) {
James Abbatielloe6758872000-12-13 21:32:55 +0000276 FIXME("function %s not found: %s\n",funcName,error);
Marcus Meissner8220bc91998-10-11 11:10:27 +0000277 return fun;
278 }
Alexandre Julliard081ee942000-08-07 04:12:41 +0000279 fun = SNOOP_GetProcAddress(wm->module,funcName,stub-first_stub,fun);
Alexandre Julliarda3960291999-02-26 11:11:13 +0000280 return (FARPROC)fun;
Marcus Meissner8220bc91998-10-11 11:10:27 +0000281}