4 * Copyright 1996 Alexandre Julliard
12 #include "wine/winbase16.h"
13 #include "builtin16.h"
18 #include "stackframe.h"
19 #include "selectors.h"
21 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(module
);
26 /* Table of all built-in DLLs */
30 static const BUILTIN16_DESCRIPTOR
*builtin_dlls
[MAX_DLLS
];
34 /* patch all the flat cs references of the code segment if necessary */
35 inline static void patch_code_segment( void *code_segment
)
38 CALLFROM16
*call
= code_segment
;
39 if (call
->flatcs
== __get_cs()) return; /* nothing to patch */
40 while (call
->pushl
== 0x68)
42 call
->flatcs
= __get_cs();
49 /***********************************************************************
50 * BUILTIN_DoLoadModule16
52 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule.
54 static HMODULE16
BUILTIN_DoLoadModule16( const BUILTIN16_DESCRIPTOR
*descr
)
58 SEGTABLEENTRY
*pSegTable
;
61 hModule
= GLOBAL_CreateBlock( GMEM_MOVEABLE
, descr
->module_start
,
62 descr
->module_size
, 0, WINE_LDT_FLAGS_DATA
);
63 if (!hModule
) return 0;
64 FarSetOwner16( hModule
, hModule
);
66 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
67 pModule
->self
= hModule
;
68 /* NOTE: (Ab)use the hRsrcMap parameter for resource data pointer */
69 pModule
->hRsrcMap
= (void *)descr
->rsrc
;
71 TRACE( "Built-in %s: hmodule=%04x\n", descr
->name
, hModule
);
73 /* Allocate the code segment */
75 pSegTable
= NE_SEG_TABLE( pModule
);
76 pSegTable
->hSeg
= GLOBAL_CreateBlock( GMEM_FIXED
, descr
->code_start
,
77 pSegTable
->minsize
, hModule
,
78 WINE_LDT_FLAGS_CODE
|WINE_LDT_FLAGS_32BIT
);
79 if (!pSegTable
->hSeg
) return 0;
80 patch_code_segment( descr
->code_start
);
83 /* Allocate the data segment */
85 minsize
= pSegTable
->minsize
? pSegTable
->minsize
: 0x10000;
86 minsize
+= pModule
->heap_size
;
87 if (minsize
> 0x10000) minsize
= 0x10000;
88 pSegTable
->hSeg
= GLOBAL_Alloc( GMEM_FIXED
, minsize
, hModule
, WINE_LDT_FLAGS_DATA
);
89 if (!pSegTable
->hSeg
) return 0;
90 if (pSegTable
->minsize
) memcpy( GlobalLock16( pSegTable
->hSeg
),
91 descr
->data_start
, pSegTable
->minsize
);
92 if (pModule
->heap_size
)
93 LocalInit16( GlobalHandleToSel16(pSegTable
->hSeg
),
94 pSegTable
->minsize
, minsize
);
96 if (descr
->rsrc
) NE_InitResourceHandler(hModule
);
98 NE_RegisterModule( pModule
);
103 /***********************************************************************
106 * Load a built-in module.
108 HMODULE16
BUILTIN_LoadModule( LPCSTR name
)
110 char dllname
[20], *p
;
114 /* Fix the name in case we have a full path and extension */
116 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
117 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
119 if (strlen(name
) >= sizeof(dllname
)-4) return (HMODULE16
)2;
121 strcpy( dllname
, name
);
122 p
= strrchr( dllname
, '.' );
123 if (!p
) strcat( dllname
, ".dll" );
125 for (i
= 0; i
< nb_dlls
; i
++)
127 const BUILTIN16_DESCRIPTOR
*descr
= builtin_dlls
[i
];
128 NE_MODULE
*pModule
= (NE_MODULE
*)descr
->module_start
;
129 OFSTRUCT
*pOfs
= (OFSTRUCT
*)((LPBYTE
)pModule
+ pModule
->fileinfo
);
130 if (!strcasecmp( pOfs
->szPathName
, dllname
))
131 return BUILTIN_DoLoadModule16( descr
);
134 if ((handle
= BUILTIN32_dlopen( dllname
)))
136 for (i
= 0; i
< nb_dlls
; i
++)
138 const BUILTIN16_DESCRIPTOR
*descr
= builtin_dlls
[i
];
139 NE_MODULE
*pModule
= (NE_MODULE
*)descr
->module_start
;
140 OFSTRUCT
*pOfs
= (OFSTRUCT
*)((LPBYTE
)pModule
+ pModule
->fileinfo
);
141 if (!strcasecmp( pOfs
->szPathName
, dllname
))
142 return BUILTIN_DoLoadModule16( descr
);
144 ERR( "loaded .so but dll %s still not found\n", dllname
);
145 BUILTIN32_dlclose( handle
);
152 /***********************************************************************
153 * BUILTIN_GetEntryPoint16
155 * Return the ordinal, name, and type info corresponding to a CS:IP address.
156 * This is used only by relay debugging.
158 LPCSTR
BUILTIN_GetEntryPoint16( STACK16FRAME
*frame
, LPSTR name
, WORD
*pOrd
)
166 if (!(pModule
= NE_GetPtr( FarGetOwner16( GlobalHandle16( frame
->module_cs
) ))))
171 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ pModule
->entry_table
);
174 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
175 for (i
= bundle
->first
+ 1; i
<= bundle
->last
; i
++)
177 if ((entry
->offs
< frame
->entry_ip
)
178 && (entry
->segnum
== 1) /* code segment ? */
179 && (entry
->offs
>= max_offset
))
181 max_offset
= entry
->offs
;
186 } while ( (bundle
->next
)
187 && (bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+bundle
->next
)));
189 /* Search for the name in the resident names table */
190 /* (built-in modules have no non-resident table) */
192 p
= (BYTE
*)pModule
+ pModule
->name_table
;
195 p
+= *p
+ 1 + sizeof(WORD
);
196 if (*(WORD
*)(p
+ *p
+ 1) == *pOrd
) break;
199 sprintf( name
, "%.*s.%d: %.*s",
200 *((BYTE
*)pModule
+ pModule
->name_table
),
201 (char *)pModule
+ pModule
->name_table
+ 1,
202 *pOrd
, *p
, (char *)(p
+ 1) );
204 /* Retrieve type info string */
205 return *(LPCSTR
*)((LPBYTE
)PTR_SEG_OFF_TO_LIN( frame
->module_cs
, frame
->callfrom_ip
) + 4);
209 /***********************************************************************
210 * __wine_register_dll_16
212 * Register a built-in DLL descriptor.
214 void __wine_register_dll_16( const BUILTIN16_DESCRIPTOR
*descr
)
216 assert( nb_dlls
< MAX_DLLS
);
217 builtin_dlls
[nb_dlls
++] = descr
;