4 * Copyright 1996 Alexandre Julliard
11 #include "builtin32.h"
18 #include "stackframe.h"
23 /* Built-in modules descriptors */
24 /* Don't change these structures! (see tools/build.c) */
28 const char *name
; /* DLL name */
29 void *module_start
; /* 32-bit address of the module data */
30 int module_size
; /* Size of the module data */
31 const BYTE
*code_start
; /* 32-bit address of DLL code */
32 const BYTE
*data_start
; /* 32-bit address of DLL data */
37 const WIN16_DESCRIPTOR
*descr
; /* DLL descriptor */
38 int flags
; /* flags (see below) */
42 #define DLL_FLAG_NOT_USED 0x01 /* Use original Windows DLL if possible */
43 #define DLL_FLAG_ALWAYS_USED 0x02 /* Always use built-in DLL */
47 extern const WIN16_DESCRIPTOR COMMDLG_Descriptor
;
48 extern const WIN16_DESCRIPTOR COMPOBJ_Descriptor
;
49 extern const WIN16_DESCRIPTOR DDEML_Descriptor
;
50 extern const WIN16_DESCRIPTOR GDI_Descriptor
;
51 extern const WIN16_DESCRIPTOR KERNEL_Descriptor
;
52 extern const WIN16_DESCRIPTOR KEYBOARD_Descriptor
;
53 extern const WIN16_DESCRIPTOR LZEXPAND_Descriptor
;
54 extern const WIN16_DESCRIPTOR MMSYSTEM_Descriptor
;
55 extern const WIN16_DESCRIPTOR MOUSE_Descriptor
;
56 extern const WIN16_DESCRIPTOR OLE2CONV_Descriptor
;
57 extern const WIN16_DESCRIPTOR OLE2DISP_Descriptor
;
58 extern const WIN16_DESCRIPTOR OLE2NLS_Descriptor
;
59 extern const WIN16_DESCRIPTOR OLE2PROX_Descriptor
;
60 extern const WIN16_DESCRIPTOR OLE2THK_Descriptor
;
61 extern const WIN16_DESCRIPTOR OLE2_Descriptor
;
62 extern const WIN16_DESCRIPTOR OLECLI_Descriptor
;
63 extern const WIN16_DESCRIPTOR OLESVR_Descriptor
;
64 extern const WIN16_DESCRIPTOR SHELL_Descriptor
;
65 extern const WIN16_DESCRIPTOR SOUND_Descriptor
;
66 extern const WIN16_DESCRIPTOR STORAGE_Descriptor
;
67 extern const WIN16_DESCRIPTOR STRESS_Descriptor
;
68 extern const WIN16_DESCRIPTOR SYSTEM_Descriptor
;
69 extern const WIN16_DESCRIPTOR TOOLHELP_Descriptor
;
70 extern const WIN16_DESCRIPTOR USER_Descriptor
;
71 extern const WIN16_DESCRIPTOR VER_Descriptor
;
72 extern const WIN16_DESCRIPTOR W32SYS_Descriptor
;
73 extern const WIN16_DESCRIPTOR WIN32S16_Descriptor
;
74 extern const WIN16_DESCRIPTOR WIN87EM_Descriptor
;
75 extern const WIN16_DESCRIPTOR WINASPI_Descriptor
;
76 extern const WIN16_DESCRIPTOR WINDEBUG_Descriptor
;
77 extern const WIN16_DESCRIPTOR WING_Descriptor
;
78 extern const WIN16_DESCRIPTOR WINSOCK_Descriptor
;
79 extern const WIN16_DESCRIPTOR WPROCS_Descriptor
;
81 /* Table of all built-in DLLs */
83 static BUILTIN16_DLL BuiltinDLLs
[] =
85 { &KERNEL_Descriptor
, DLL_FLAG_ALWAYS_USED
},
86 { &USER_Descriptor
, DLL_FLAG_ALWAYS_USED
},
87 { &GDI_Descriptor
, DLL_FLAG_ALWAYS_USED
},
88 { &SYSTEM_Descriptor
, DLL_FLAG_ALWAYS_USED
},
89 { &WPROCS_Descriptor
, DLL_FLAG_ALWAYS_USED
},
90 { &WINDEBUG_Descriptor
, DLL_FLAG_ALWAYS_USED
},
91 { &COMMDLG_Descriptor
, DLL_FLAG_NOT_USED
},
92 { &COMPOBJ_Descriptor
, DLL_FLAG_NOT_USED
},
93 { &DDEML_Descriptor
, DLL_FLAG_NOT_USED
},
94 { &KEYBOARD_Descriptor
, 0 },
95 { &LZEXPAND_Descriptor
, 0 },
96 { &MMSYSTEM_Descriptor
, 0 },
97 { &MOUSE_Descriptor
, 0 },
98 { &OLE2CONV_Descriptor
, DLL_FLAG_NOT_USED
},
99 { &OLE2DISP_Descriptor
, DLL_FLAG_NOT_USED
},
100 { &OLE2NLS_Descriptor
, DLL_FLAG_NOT_USED
},
101 { &OLE2PROX_Descriptor
, DLL_FLAG_NOT_USED
},
102 { &OLE2THK_Descriptor
, DLL_FLAG_NOT_USED
},
103 { &OLE2_Descriptor
, DLL_FLAG_NOT_USED
},
104 { &OLECLI_Descriptor
, DLL_FLAG_NOT_USED
},
105 { &OLESVR_Descriptor
, DLL_FLAG_NOT_USED
},
106 { &SHELL_Descriptor
, 0 },
107 { &SOUND_Descriptor
, 0 },
108 { &STORAGE_Descriptor
, DLL_FLAG_NOT_USED
},
109 { &STRESS_Descriptor
, 0 },
110 { &TOOLHELP_Descriptor
, 0 },
111 { &VER_Descriptor
, 0 },
112 { &W32SYS_Descriptor
, 0 },
113 { &WIN32S16_Descriptor
, 0 },
114 { &WIN87EM_Descriptor
, DLL_FLAG_NOT_USED
},
115 { &WINASPI_Descriptor
, 0 },
116 { &WING_Descriptor
, 0 },
117 { &WINSOCK_Descriptor
, 0 },
122 /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
123 #define FIRST_INTERRUPT_ORDINAL 100
126 /***********************************************************************
127 * BUILTIN_DoLoadModule16
129 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
132 static HMODULE16
BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR
*descr
)
136 SEGTABLEENTRY
*pSegTable
;
138 HMODULE16 hModule
= GLOBAL_CreateBlock( GMEM_MOVEABLE
, descr
->module_start
,
139 descr
->module_size
, 0,
140 FALSE
, FALSE
, FALSE
, NULL
);
141 if (!hModule
) return 0;
142 FarSetOwner( hModule
, hModule
);
144 dprintf_info(module
, "Built-in %s: hmodule=%04x\n",
145 descr
->name
, hModule
);
146 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
147 pModule
->self
= hModule
;
149 /* Allocate the code segment */
151 pSegTable
= NE_SEG_TABLE( pModule
);
152 pSegTable
->selector
= GLOBAL_CreateBlock( GMEM_FIXED
, descr
->code_start
,
153 pSegTable
->minsize
, hModule
,
154 TRUE
, TRUE
, FALSE
, NULL
);
155 if (!pSegTable
->selector
) return 0;
158 /* Allocate the data segment */
160 minsize
= pSegTable
->minsize
? pSegTable
->minsize
: 0x10000;
161 minsize
+= pModule
->heap_size
;
162 if (minsize
> 0x10000) minsize
= 0x10000;
163 pSegTable
->selector
= GLOBAL_Alloc( GMEM_FIXED
, minsize
,
164 hModule
, FALSE
, FALSE
, FALSE
);
165 if (!pSegTable
->selector
) return 0;
166 if (pSegTable
->minsize
) memcpy( GlobalLock16( pSegTable
->selector
),
167 descr
->data_start
, pSegTable
->minsize
);
168 if (pModule
->heap_size
)
169 LocalInit( pSegTable
->selector
, pSegTable
->minsize
, minsize
);
171 MODULE_RegisterModule( pModule
);
176 /***********************************************************************
179 * Load all built-in modules marked as 'always used'.
181 BOOL32
BUILTIN_Init(void)
188 fnBUILTIN_LoadModule
= BUILTIN_LoadModule
;
190 for (dll
= BuiltinDLLs
; dll
->descr
; dll
++)
192 if (dll
->flags
& DLL_FLAG_ALWAYS_USED
)
193 if (!BUILTIN_DoLoadModule16( dll
->descr
)) return FALSE
;
196 /* Set the USER and GDI heap selectors */
198 pModule
= MODULE_GetPtr( GetModuleHandle16( "USER" ));
199 USER_HeapSel
= (NE_SEG_TABLE( pModule
) + pModule
->dgroup
- 1)->selector
;
200 pModule
= MODULE_GetPtr( GetModuleHandle16( "GDI" ));
201 GDI_HeapSel
= (NE_SEG_TABLE( pModule
) + pModule
->dgroup
- 1)->selector
;
203 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
205 hModule
= GetModuleHandle16( "KERNEL" );
206 MODULE_SetEntryPoint( hModule
, 178, GetWinFlags() );
208 /* Initialize the real-mode selector entry points */
210 DOSMEM_InitExports( hModule
);
212 /* Set interrupt vectors from entry points in WPROCS.DLL */
214 hModule
= GetModuleHandle16( "WPROCS" );
215 for (vector
= 0; vector
< 256; vector
++)
217 FARPROC16 proc
= MODULE_GetEntryPoint( hModule
,
218 FIRST_INTERRUPT_ORDINAL
+vector
);
220 INT_SetHandler( vector
, proc
);
227 /***********************************************************************
230 * Load a built-in module. If the 'force' parameter is FALSE, we only
231 * load the module if it has not been disabled via the -dll option.
233 HMODULE32
BUILTIN_LoadModule( LPCSTR name
, BOOL32 force
)
235 BUILTIN16_DLL
*table
;
236 char dllname
[16], *p
;
238 /* Fix the name in case we have a full path and extension */
240 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
241 lstrcpyn32A( dllname
, name
, sizeof(dllname
) );
242 if ((p
= strrchr( dllname
, '.' ))) *p
= '\0';
244 for (table
= BuiltinDLLs
; table
->descr
; table
++)
245 if (!lstrcmpi32A( table
->descr
->name
, dllname
)) break;
246 if (!table
->descr
) return BUILTIN32_LoadModule( name
, force
,
248 if ((table
->flags
& DLL_FLAG_NOT_USED
) && !force
) return 0;
250 return BUILTIN_DoLoadModule16( table
->descr
);
254 /***********************************************************************
255 * BUILTIN_GetEntryPoint16
257 * Return the ordinal and name corresponding to a CS:IP address.
258 * This is used only by relay debugging.
260 LPCSTR
BUILTIN_GetEntryPoint16( WORD cs
, WORD ip
, WORD
*pOrd
)
262 static char buffer
[80];
263 WORD ordinal
, i
, max_offset
;
267 if (!(pModule
= MODULE_GetPtr( FarGetOwner( GlobalHandle16(cs
) ))))
270 /* Search for the ordinal */
272 p
= (BYTE
*)pModule
+ pModule
->entry_table
;
284 case 1: /* code segment */
290 if ((*(WORD
*)p
<= ip
) && (*(WORD
*)p
>= max_offset
))
292 max_offset
= *(WORD
*)p
;
299 case 0xff: /* moveable (should not happen in built-in modules) */
300 fprintf( stderr
, "Built-in module has moveable entry\n" );
304 default: /* other segment */
311 /* Search for the name in the resident names table */
312 /* (built-in modules have no non-resident table) */
314 p
= (BYTE
*)pModule
+ pModule
->name_table
;
317 p
+= *p
+ 1 + sizeof(WORD
);
318 if (*(WORD
*)(p
+ *p
+ 1) == *pOrd
) break;
321 sprintf( buffer
, "%.*s.%d: %.*s",
322 *((BYTE
*)pModule
+ pModule
->name_table
),
323 (char *)pModule
+ pModule
->name_table
+ 1,
324 *pOrd
, *p
, (char *)(p
+ 1) );
329 /**********************************************************************
330 * BUILTIN_DefaultIntHandler
332 * Default interrupt handler.
334 void BUILTIN_DefaultIntHandler( CONTEXT
*context
)
337 STACK16FRAME
*frame
= CURRENT_STACK16
;
338 BUILTIN_GetEntryPoint16( frame
->entry_cs
, frame
->entry_ip
, &ordinal
);
339 INT_BARF( context
, ordinal
- FIRST_INTERRUPT_ORDINAL
);
343 /***********************************************************************
344 * BUILTIN_ParseDLLOptions
346 * Set runtime DLL usage flags
348 BOOL32
BUILTIN_ParseDLLOptions( const char *str
)
355 while (*str
&& isspace(*str
)) str
++;
356 if (!*str
) return TRUE
;
357 if ((*str
!= '+') && (*str
!= '-')) return FALSE
;
359 if (!(p
= strchr( str
, ',' ))) p
= str
+ strlen(str
);
360 while ((p
> str
) && isspace(p
[-1])) p
--;
361 if (p
== str
) return FALSE
;
362 for (dll
= BuiltinDLLs
; dll
->descr
; dll
++)
364 if (!lstrncmpi32A( str
, dll
->descr
->name
, (int)(p
- str
) ))
366 if (dll
->descr
->name
[(int)(p
-str
)]) /* only partial match */
370 if (dll
->flags
& DLL_FLAG_ALWAYS_USED
) return FALSE
;
371 dll
->flags
|= DLL_FLAG_NOT_USED
;
373 else dll
->flags
&= ~DLL_FLAG_NOT_USED
;
378 if (!BUILTIN32_EnableDLL( str
, (int)(p
- str
), (str
[-1] == '+') ))
381 while (*str
&& (isspace(*str
) || (*str
== ','))) str
++;
387 /***********************************************************************
390 * Print the list of built-in DLLs that can be disabled.
392 void BUILTIN_PrintDLLs(void)
397 fprintf(stderr
,"Example: -dll -ole2 Do not use emulated OLE2.DLL\n");
398 fprintf(stderr
,"Available Win16 DLLs:\n");
399 for (i
= 0, dll
= BuiltinDLLs
; dll
->descr
; dll
++)
401 if (!(dll
->flags
& DLL_FLAG_ALWAYS_USED
))
402 fprintf( stderr
, "%-9s%c", dll
->descr
->name
,
403 ((++i
) % 8) ? ' ' : '\n' );
405 fprintf(stderr
,"\n");
406 BUILTIN32_PrintDLLs();