Added support for the TEB on Mac OS X.
[wine] / relay32 / builtin32.c
1 /*
2  * Win32 builtin functions
3  *
4  * Copyright 1997 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_MMAN_H
30 #include <sys/mman.h>
31 #endif
32
33 #include "windef.h"
34 #include "wine/winbase16.h"
35 #include "wine/library.h"
36 #include "module.h"
37 #include "file.h"
38 #include "ntdll_misc.h"
39 #include "wine/server.h"
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(module);
43 WINE_DECLARE_DEBUG_CHANNEL(relay);
44
45 static HMODULE main_module;
46 static NTSTATUS last_status; /* use to gather all errors in callback */
47
48
49 /***********************************************************************
50  *           load_library
51  *
52  * Load a library in memory; callback function for wine_dll_register
53  */
54 static void load_library( void *base, const char *filename )
55 {
56     UNICODE_STRING      wstr;
57     HMODULE module = (HMODULE)base, ret;
58     IMAGE_NT_HEADERS *nt;
59     WINE_MODREF *wm;
60     char *fullname;
61     DWORD len;
62
63     if (!base)
64     {
65         ERR("could not map image for %s\n", filename ? filename : "main exe" );
66         return;
67     }
68     if (!(nt = RtlImageNtHeader( module )))
69     {
70         ERR( "bad module for %s\n", filename ? filename : "main exe" );
71         last_status = STATUS_INVALID_IMAGE_FORMAT;
72         return;
73     }
74
75     if (!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
76     {
77         /* if we already have an executable, ignore this one */
78         if (!main_module) main_module = module;
79         return; /* don't create the modref here, will be done later on */
80     }
81
82     RtlCreateUnicodeStringFromAsciiz(&wstr, filename);
83     if (LdrGetDllHandle(0, 0, &wstr, &ret) == STATUS_SUCCESS)
84         MESSAGE( "Warning: loading builtin %s, but native version already present. "
85                  "Expect trouble.\n", filename );
86     RtlFreeUnicodeString( &wstr );
87
88     len = GetSystemDirectoryA( NULL, 0 );
89     if (!(fullname = RtlAllocateHeap( ntdll_get_process_heap(), 0, len + strlen(filename) + 1 )))
90     {
91         ERR( "can't load %s\n", filename );
92         last_status = STATUS_NO_MEMORY;
93         return;
94     }
95     GetSystemDirectoryA( fullname, len );
96     strcat( fullname, "\\" );
97     strcat( fullname, filename );
98
99     /* Create 32-bit MODREF */
100     if (!(wm = PE_CreateModule( module, fullname, 0, 0, TRUE )))
101     {
102         ERR( "can't load %s\n", filename );
103         RtlFreeHeap( ntdll_get_process_heap(), 0, fullname );
104         last_status = STATUS_NO_MEMORY;
105         return;
106     }
107     TRACE( "loaded %s %p %p\n", fullname, wm, module );
108     RtlFreeHeap( ntdll_get_process_heap(), 0, fullname );
109
110     /* setup relay debugging entry points */
111     if (TRACE_ON(relay)) RELAY_SetupDLL( (void *)module );
112 }
113
114
115 /***********************************************************************
116  *           BUILTIN32_LoadLibraryExA
117  *
118  * Partly copied from the original PE_ version.
119  *
120  */
121 NTSTATUS BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags, WINE_MODREF** pwm)
122 {
123     char error[256], dllname[20], *p;
124     int file_exists;
125     LPCSTR name;
126     void *handle;
127
128     /* Fix the name in case we have a full path and extension */
129     name = path;
130     if ((p = strrchr( name, '\\' ))) name = p + 1;
131     if ((p = strrchr( name, '/' ))) name = p + 1;
132
133     if (strlen(name) >= sizeof(dllname)-4) return STATUS_NO_SUCH_FILE;
134
135     strcpy( dllname, name );
136     p = strrchr( dllname, '.' );
137     if (!p) strcat( dllname, ".dll" );
138     for (p = dllname; *p; p++) *p = FILE_tolower(*p);
139
140     last_status = STATUS_SUCCESS;
141     /* load_library will modify last_status. Note also that load_library can be
142      * called several times, if the .so file we're loading has dependencies.
143      * last_status will gather all the errors we may get while loading all these
144      * libraries
145      */
146     if (!(handle = wine_dll_load( dllname, error, sizeof(error), &file_exists )))
147     {
148         if (!file_exists)
149         {
150             /* The file does not exist -> WARN() */
151             WARN("cannot open .so lib for builtin %s: %s\n", name, error);
152             return STATUS_NO_SUCH_FILE;
153         }
154         /* ERR() for all other errors (missing functions, ...) */
155         ERR("failed to load .so lib for builtin %s: %s\n", name, error );
156         return STATUS_PROCEDURE_NOT_FOUND;
157     }
158     if (last_status != STATUS_SUCCESS) return last_status;
159
160     if (!((*pwm) = MODULE_FindModule( path ))) *pwm = MODULE_FindModule( dllname );
161     if (!*pwm)
162     {
163         ERR( "loaded .so but dll %s still not found - 16-bit dll or version conflict.\n", dllname );
164         /* wine_dll_unload( handle );*/
165         return STATUS_INVALID_IMAGE_FORMAT;
166     }
167     (*pwm)->dlhandle = handle;
168     return STATUS_SUCCESS;
169 }
170
171 /***********************************************************************
172  *           BUILTIN32_Init
173  *
174  * Initialize loading callbacks and return HMODULE of main exe.
175  * 'main' is the main exe in case it was already loaded from a PE file.
176  */
177 HMODULE BUILTIN32_LoadExeModule( HMODULE main )
178 {
179     main_module = main;
180     last_status = STATUS_SUCCESS;
181     wine_dll_set_callback( load_library );
182     if (!main_module)
183         MESSAGE( "No built-in EXE module loaded!  Did you create a .spec file?\n" );
184     if (last_status != STATUS_SUCCESS)
185         MESSAGE( "Error while processing initial modules\n");
186
187     return main_module;
188 }