Added pow() forward.
[wine.git] / if1632 / builtin.c
blobe125bccb1926020f0393d77363bfa653bbab8c13
1 /*
2 * Built-in modules
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <ctype.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include "winbase.h"
12 #include "wine/winbase16.h"
13 #include "wine/winestring.h"
14 #include "builtin16.h"
15 #include "global.h"
16 #include "heap.h"
17 #include "module.h"
18 #include "miscemu.h"
19 #include "stackframe.h"
20 #include "selectors.h"
21 #include "task.h"
22 #include "debugtools.h"
23 #include "toolhelp.h"
25 DEFAULT_DEBUG_CHANNEL(module);
27 /* Table of all built-in DLLs */
29 #define MAX_DLLS 50
31 static const BUILTIN16_DESCRIPTOR *builtin_dlls[MAX_DLLS];
32 static int nb_dlls;
35 /* patch all the flat cs references of the code segment if necessary */
36 inline static void patch_code_segment( void *code_segment )
38 #ifdef __i386__
39 CALLFROM16 *call = code_segment;
40 if (call->flatcs == __get_cs()) return; /* nothing to patch */
41 while (call->pushl == 0x68)
43 call->flatcs = __get_cs();
44 call++;
46 #endif
50 /***********************************************************************
51 * BUILTIN_DoLoadModule16
53 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule.
55 static HMODULE16 BUILTIN_DoLoadModule16( const BUILTIN16_DESCRIPTOR *descr )
57 NE_MODULE *pModule;
58 int minsize;
59 SEGTABLEENTRY *pSegTable;
60 HMODULE16 hModule;
62 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
63 descr->module_size, 0, WINE_LDT_FLAGS_DATA );
64 if (!hModule) return 0;
65 FarSetOwner16( hModule, hModule );
67 pModule = (NE_MODULE *)GlobalLock16( hModule );
68 pModule->self = hModule;
69 /* NOTE: (Ab)use the hRsrcMap parameter for resource data pointer */
70 pModule->hRsrcMap = (void *)descr->rsrc;
72 TRACE( "Built-in %s: hmodule=%04x\n", descr->name, hModule );
74 /* Allocate the code segment */
76 pSegTable = NE_SEG_TABLE( pModule );
77 pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
78 pSegTable->minsize, hModule,
79 WINE_LDT_FLAGS_CODE|WINE_LDT_FLAGS_32BIT );
80 if (!pSegTable->hSeg) return 0;
81 patch_code_segment( descr->code_start );
82 pSegTable++;
84 /* Allocate the data segment */
86 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
87 minsize += pModule->heap_size;
88 if (minsize > 0x10000) minsize = 0x10000;
89 pSegTable->hSeg = GLOBAL_Alloc( GMEM_FIXED, minsize, hModule, WINE_LDT_FLAGS_DATA );
90 if (!pSegTable->hSeg) return 0;
91 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->hSeg ),
92 descr->data_start, pSegTable->minsize);
93 if (pModule->heap_size)
94 LocalInit16( GlobalHandleToSel16(pSegTable->hSeg),
95 pSegTable->minsize, minsize );
97 if (descr->rsrc) NE_InitResourceHandler(hModule);
99 NE_RegisterModule( pModule );
100 return hModule;
104 /***********************************************************************
105 * BUILTIN_LoadModule
107 * Load a built-in module.
109 HMODULE16 BUILTIN_LoadModule( LPCSTR name )
111 char dllname[20], *p;
112 void *handle;
113 int i;
115 /* Fix the name in case we have a full path and extension */
117 if ((p = strrchr( name, '\\' ))) name = p + 1;
118 if ((p = strrchr( name, '/' ))) name = p + 1;
120 if (strlen(name) >= sizeof(dllname)-4) return (HMODULE16)2;
122 strcpy( dllname, name );
123 p = strrchr( dllname, '.' );
124 if (!p) strcat( dllname, ".dll" );
126 for (i = 0; i < nb_dlls; i++)
128 const BUILTIN16_DESCRIPTOR *descr = builtin_dlls[i];
129 NE_MODULE *pModule = (NE_MODULE *)descr->module_start;
130 OFSTRUCT *pOfs = (OFSTRUCT *)((LPBYTE)pModule + pModule->fileinfo);
131 if (!strcasecmp( pOfs->szPathName, dllname ))
132 return BUILTIN_DoLoadModule16( descr );
135 if ((handle = BUILTIN32_dlopen( dllname )))
137 for (i = 0; i < nb_dlls; i++)
139 const BUILTIN16_DESCRIPTOR *descr = builtin_dlls[i];
140 NE_MODULE *pModule = (NE_MODULE *)descr->module_start;
141 OFSTRUCT *pOfs = (OFSTRUCT *)((LPBYTE)pModule + pModule->fileinfo);
142 if (!strcasecmp( pOfs->szPathName, dllname ))
143 return BUILTIN_DoLoadModule16( descr );
145 ERR( "loaded .so but dll %s still not found\n", dllname );
146 BUILTIN32_dlclose( handle );
149 return (HMODULE16)2;
153 /***********************************************************************
154 * BUILTIN_GetEntryPoint16
156 * Return the ordinal, name, and type info corresponding to a CS:IP address.
157 * This is used only by relay debugging.
159 LPCSTR BUILTIN_GetEntryPoint16( STACK16FRAME *frame, LPSTR name, WORD *pOrd )
161 WORD i, max_offset;
162 register BYTE *p;
163 NE_MODULE *pModule;
164 ET_BUNDLE *bundle;
165 ET_ENTRY *entry;
167 if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16( frame->module_cs ) ))))
168 return NULL;
170 max_offset = 0;
171 *pOrd = 0;
172 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
175 entry = (ET_ENTRY *)((BYTE *)bundle+6);
176 for (i = bundle->first + 1; i <= bundle->last; i++)
178 if ((entry->offs < frame->entry_ip)
179 && (entry->segnum == 1) /* code segment ? */
180 && (entry->offs >= max_offset))
182 max_offset = entry->offs;
183 *pOrd = i;
185 entry++;
187 } while ( (bundle->next)
188 && (bundle = (ET_BUNDLE *)((BYTE *)pModule+bundle->next)));
190 /* Search for the name in the resident names table */
191 /* (built-in modules have no non-resident table) */
193 p = (BYTE *)pModule + pModule->name_table;
194 while (*p)
196 p += *p + 1 + sizeof(WORD);
197 if (*(WORD *)(p + *p + 1) == *pOrd) break;
200 sprintf( name, "%.*s.%d: %.*s",
201 *((BYTE *)pModule + pModule->name_table),
202 (char *)pModule + pModule->name_table + 1,
203 *pOrd, *p, (char *)(p + 1) );
205 /* Retrieve type info string */
206 return *(LPCSTR *)((LPBYTE)PTR_SEG_OFF_TO_LIN( frame->module_cs, frame->callfrom_ip ) + 4);
210 /***********************************************************************
211 * __wine_register_dll_16
213 * Register a built-in DLL descriptor.
215 void __wine_register_dll_16( const BUILTIN16_DESCRIPTOR *descr )
217 assert( nb_dlls < MAX_DLLS );
218 builtin_dlls[nb_dlls++] = descr;