If there is enough space in the buffer and the type is REG_SZ and the
[wine.git] / loader / elf.c
blobeb49890299fd2e1c6c3ffeddf12543c2c99006f3
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 "module.h"
25 #include "pe_image.h"
26 #include "debugtools.h"
27 #include "winerror.h"
28 #include "elfdll.h"
30 DEFAULT_DEBUG_CHANNEL(win32)
32 WINE_MODREF *ELF_CreateDummyModule( LPCSTR libname, LPCSTR modname )
34 PIMAGE_DOS_HEADER dh;
35 PIMAGE_NT_HEADERS nth;
36 PIMAGE_SECTION_HEADER sh;
37 WINE_MODREF *wm;
38 HMODULE hmod;
40 wm=(WINE_MODREF*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wm) );
41 wm->type = MODULE32_ELF;
43 /* FIXME: hmm, order? */
44 wm->next = PROCESS_Current()->modref_list;
45 PROCESS_Current()->modref_list = wm;
47 wm->modname = HEAP_strdupA( GetProcessHeap(), 0, modname );
48 wm->filename = HEAP_strdupA( GetProcessHeap(), 0, libname );
49 wm->short_modname = wm->modname;
50 wm->short_filename = wm->filename;
52 hmod = (HMODULE)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
53 sizeof(IMAGE_DOS_HEADER) +
54 sizeof(IMAGE_NT_HEADERS) +
55 sizeof(IMAGE_SECTION_HEADER) + 100 );
56 dh = (PIMAGE_DOS_HEADER)hmod;
57 dh->e_magic = IMAGE_DOS_SIGNATURE;
58 dh->e_lfanew = sizeof(IMAGE_DOS_HEADER);
59 nth = PE_HEADER(hmod);
60 nth->Signature = IMAGE_NT_SIGNATURE;
61 nth->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
62 nth->FileHeader.NumberOfSections = 1;
63 nth->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
64 nth->FileHeader.Characteristics =
65 IMAGE_FILE_RELOCS_STRIPPED|IMAGE_FILE_LINE_NUMS_STRIPPED|
66 IMAGE_FILE_LOCAL_SYMS_STRIPPED|IMAGE_FILE_32BIT_MACHINE|
67 IMAGE_FILE_DLL|IMAGE_FILE_DEBUG_STRIPPED;
68 nth->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
69 nth->OptionalHeader.SizeOfCode = 0;
70 nth->OptionalHeader.SizeOfInitializedData = 0;
71 nth->OptionalHeader.SizeOfUninitializedData = 0;
72 nth->OptionalHeader.AddressOfEntryPoint = 0;
73 nth->OptionalHeader.BaseOfCode = 0;
74 nth->OptionalHeader.MajorOperatingSystemVersion = 4;
75 nth->OptionalHeader.MajorImageVersion = 4;
76 nth->OptionalHeader.SizeOfImage = 0;
77 nth->OptionalHeader.SizeOfHeaders = 0;
78 nth->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_NATIVE;
79 nth->OptionalHeader.DllCharacteristics = 0;
80 nth->OptionalHeader.NumberOfRvaAndSizes = 0;
82 /* allocate one code section that crosses the whole process range
83 * (we could find out from internal tables ... hmm )
85 sh=(PIMAGE_SECTION_HEADER)(nth+1);
86 strcpy(sh->Name,".text");
87 sh->Misc.VirtualSize = 0x7fffffff;
88 sh->VirtualAddress = 0x42; /* so snoop can use it ... */
89 sh->SizeOfRawData = 0x7fffffff;
90 sh->PointerToRawData = 0;
91 sh->Characteristics = IMAGE_SCN_CNT_CODE|IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_EXECUTE|IMAGE_SCN_MEM_READ;
92 wm->module = hmod;
93 return wm;
97 #if defined(HAVE_DL_API)
99 #define UNIX_DLL_ENDING "so"
101 #define STUBSIZE 4095
103 #include <dlfcn.h>
105 WINE_MODREF *ELF_LoadLibraryExA( LPCSTR libname, DWORD flags, DWORD *err)
107 WINE_MODREF *wm;
108 char *modname,*s,*t,*x;
109 LPVOID *dlhandle;
111 t = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
112 strlen(libname) + strlen("lib.so") + 1 );
113 *t = '\0';
114 /* copy path to tempvar ... */
115 s=strrchr(libname,'/');
116 if (!s)
117 s=strrchr(libname,'\\');
118 if (s) {
119 s++; /* skip / or \ */
120 /* copy everything up to s-1 */
121 memcpy(t,libname,s-libname);
122 t[s-libname]= '\0';
123 } else
124 s = (LPSTR)libname;
125 modname = s;
126 /* append "lib" foo ".so" */
127 strcat(t,"lib");
128 x = t+strlen(t);
129 strcat(t,s);
130 s = strchr(x,'.');
131 if (s) {
132 while (s) {
133 if (!strcasecmp(s,".dll")) {
134 strcpy(s+1,UNIX_DLL_ENDING);
135 break;
137 s=strchr(s+1,'.');
139 } else {
140 strcat(x,"."UNIX_DLL_ENDING);
143 /* FIXME: make UNIX filename from DOS fn? */
145 /* ... and open it */
146 dlhandle = ELFDLL_dlopen(t,RTLD_NOW);
147 if (!dlhandle) {
148 HeapFree( GetProcessHeap(), 0, t );
149 *err = ERROR_FILE_NOT_FOUND;
150 return NULL;
153 wm = ELF_CreateDummyModule( t, modname );
154 wm->binfmt.elf.dlhandle = dlhandle;
156 SNOOP_RegisterDLL(wm->module,libname,STUBSIZE/sizeof(ELF_STDCALL_STUB));
157 *err = 0;
158 return wm;
161 FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName)
163 LPVOID fun;
164 int i,nrofargs = 0;
165 ELF_STDCALL_STUB *stub;
167 assert(wm->type == MODULE32_ELF);
168 if (!HIWORD(funcName)) {
169 ERR("Can't import from UNIX dynamic libs by ordinal, sorry.\n");
170 return (FARPROC)0;
172 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
173 /* we sometimes have an excess '_' at the beginning of the name */
174 if (!fun && (funcName[0]=='_')) {
175 funcName++ ;
176 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
178 if (!fun) {
179 /* Function@nrofargs usually marks a stdcall function
180 * with nrofargs bytes that are popped at the end
182 if (strchr(funcName,'@')) {
183 LPSTR t,fn = HEAP_strdupA( GetProcessHeap(), 0, funcName );
185 t = strchr(fn,'@');
186 *t = '\0';
187 nrofargs = 0;
188 sscanf(t+1,"%d",&nrofargs);
189 fun = dlsym(wm->binfmt.elf.dlhandle,fn);
190 HeapFree( GetProcessHeap(), 0, fn );
193 /* We sometimes have Win32 dlls implemented using stdcall but UNIX
194 * dlls using cdecl. If we find out the number of args the function
195 * uses, we remove them from the stack using two small stubs.
197 if (!wm->binfmt.elf.stubs) {
198 /* one page should suffice */
199 wm->binfmt.elf.stubs = VirtualAlloc(NULL,STUBSIZE,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
200 memset(wm->binfmt.elf.stubs,0,STUBSIZE);
202 stub = wm->binfmt.elf.stubs;
203 for (i=0;i<STUBSIZE/sizeof(ELF_STDCALL_STUB);i++) {
204 if (!stub->origfun)
205 break;
206 if (stub->origfun == (DWORD)fun)
207 break;
208 stub++;
210 if (i==STUBSIZE/sizeof(ELF_STDCALL_STUB)) {
211 ERR("please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
212 assert(i<STUBSIZE/sizeof(ELF_STDCALL_STUB));
214 if (!stub->origfun)
215 stub->origfun=(DWORD)fun; /* just a marker */
217 if (fun && nrofargs) { /* we don't need it for 0 args */
218 /* Selfmodifying entry/return stub for stdcall -> cdecl
219 * conversion.
220 * - Pop returnaddress directly into our return code
221 * popl <into code below>
222 * - Replace it by pointer to start of our returncode
223 * push $newret
224 * - And call the original function
225 * jmp $orgfun
226 * - Remove the arguments no longer needed
227 * newret: add esp, <nrofargs>
228 * - Push the original returnvalue on the stack
229 * pushl <poppedvalue>
230 * - And return to it.
231 * ret
234 /* FIXME: The function stub is not reentrant. */
236 ((LPBYTE)&(stub->popl))[0] = 0x8f;
237 ((LPBYTE)&(stub->popl))[1] = 0x05;
238 stub->addr_popped = (DWORD)&(stub->oldret);
239 stub->pushl1 = 0x68;
240 stub->newret = (DWORD)&(stub->addesp);
241 stub->pushl2 = 0x68;
242 stub->origfun = (DWORD)fun;
243 stub->ret1 = 0xc3;
244 ((LPBYTE)&(stub->addesp))[0]=0x83;
245 ((LPBYTE)&(stub->addesp))[1]=0xc4;
246 stub->nrofargs = nrofargs;
247 stub->pushl3 = 0x68;
248 /* filled out by entrycode */
249 stub->oldret = 0xdeadbeef;
250 stub->ret2 = 0xc3;
251 fun=(FARPROC)stub;
253 if (!fun) {
254 FIXME("function %s not found: %s\n",funcName,dlerror());
255 return fun;
257 fun = SNOOP_GetProcAddress(wm->module,funcName,stub-wm->binfmt.elf.stubs,fun);
258 return (FARPROC)fun;
262 /***************************************************************************
263 * ELF_UnloadLibrary
265 * Unload the elf library and free the modref
267 void ELF_UnloadLibrary(WINE_MODREF *wm)
269 /* FIXME: do something here */
272 #else
274 WINE_MODREF *ELF_LoadLibraryExA( LPCSTR libname, DWORD flags, DWORD *err)
276 return NULL;
279 void ELF_UnloadLibrary(WINE_MODREF *wm)
283 FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName)
285 return (FARPROC)0;
288 #endif