4 * Copyright 1996 Alexandre Julliard
12 #include "wine/winbase16.h"
13 #include "wine/winestring.h"
14 #include "builtin16.h"
19 #include "stackframe.h"
20 #include "selectors.h"
22 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(module
);
27 /* Table of all built-in DLLs */
31 static const BUILTIN16_DESCRIPTOR
*builtin_dlls
[MAX_DLLS
];
35 /* patch all the flat cs references of the code segment if necessary */
36 inline static void patch_code_segment( void *code_segment
)
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();
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
)
59 SEGTABLEENTRY
*pSegTable
;
62 hModule
= GLOBAL_CreateBlock( GMEM_MOVEABLE
, descr
->module_start
,
63 descr
->module_size
, 0,
64 FALSE
, FALSE
, FALSE
);
65 if (!hModule
) return 0;
66 FarSetOwner16( hModule
, hModule
);
68 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
69 pModule
->self
= hModule
;
70 /* NOTE: (Ab)use the hRsrcMap parameter for resource data pointer */
71 pModule
->hRsrcMap
= (void *)descr
->rsrc
;
73 TRACE( "Built-in %s: hmodule=%04x\n", descr
->name
, hModule
);
75 /* Allocate the code segment */
77 pSegTable
= NE_SEG_TABLE( pModule
);
78 pSegTable
->hSeg
= GLOBAL_CreateBlock( GMEM_FIXED
, descr
->code_start
,
79 pSegTable
->minsize
, hModule
, TRUE
, TRUE
, FALSE
);
80 if (!pSegTable
->hSeg
) return 0;
81 patch_code_segment( descr
->code_start
);
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
,
90 hModule
, FALSE
, FALSE
, FALSE
);
91 if (!pSegTable
->hSeg
) return 0;
92 if (pSegTable
->minsize
) memcpy( GlobalLock16( pSegTable
->hSeg
),
93 descr
->data_start
, pSegTable
->minsize
);
94 if (pModule
->heap_size
)
95 LocalInit16( GlobalHandleToSel16(pSegTable
->hSeg
),
96 pSegTable
->minsize
, minsize
);
98 if (descr
->rsrc
) NE_InitResourceHandler(hModule
);
100 NE_RegisterModule( pModule
);
105 /***********************************************************************
108 * Load a built-in module.
110 HMODULE16
BUILTIN_LoadModule( LPCSTR name
)
112 char dllname
[20], *p
;
116 /* Fix the name in case we have a full path and extension */
118 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
119 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
121 if (strlen(name
) >= sizeof(dllname
)-4) return (HMODULE16
)2;
123 strcpy( dllname
, name
);
124 p
= strrchr( dllname
, '.' );
125 if (!p
) strcat( dllname
, ".dll" );
127 for (i
= 0; i
< nb_dlls
; i
++)
129 const BUILTIN16_DESCRIPTOR
*descr
= builtin_dlls
[i
];
130 NE_MODULE
*pModule
= (NE_MODULE
*)descr
->module_start
;
131 OFSTRUCT
*pOfs
= (OFSTRUCT
*)((LPBYTE
)pModule
+ pModule
->fileinfo
);
132 if (!strcasecmp( pOfs
->szPathName
, dllname
))
133 return BUILTIN_DoLoadModule16( descr
);
136 if ((handle
= BUILTIN32_dlopen( dllname
)))
138 for (i
= 0; i
< nb_dlls
; i
++)
140 const BUILTIN16_DESCRIPTOR
*descr
= builtin_dlls
[i
];
141 NE_MODULE
*pModule
= (NE_MODULE
*)descr
->module_start
;
142 OFSTRUCT
*pOfs
= (OFSTRUCT
*)((LPBYTE
)pModule
+ pModule
->fileinfo
);
143 if (!strcasecmp( pOfs
->szPathName
, dllname
))
144 return BUILTIN_DoLoadModule16( descr
);
146 ERR( "loaded .so but dll %s still not found\n", dllname
);
147 BUILTIN32_dlclose( handle
);
154 /***********************************************************************
155 * BUILTIN_GetEntryPoint16
157 * Return the ordinal, name, and type info corresponding to a CS:IP address.
158 * This is used only by relay debugging.
160 LPCSTR
BUILTIN_GetEntryPoint16( STACK16FRAME
*frame
, LPSTR name
, WORD
*pOrd
)
168 if (!(pModule
= NE_GetPtr( FarGetOwner16( GlobalHandle16( frame
->module_cs
) ))))
173 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ pModule
->entry_table
);
176 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
177 for (i
= bundle
->first
+ 1; i
<= bundle
->last
; i
++)
179 if ((entry
->offs
< frame
->entry_ip
)
180 && (entry
->segnum
== 1) /* code segment ? */
181 && (entry
->offs
>= max_offset
))
183 max_offset
= entry
->offs
;
188 } while ( (bundle
->next
)
189 && (bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+bundle
->next
)));
191 /* Search for the name in the resident names table */
192 /* (built-in modules have no non-resident table) */
194 p
= (BYTE
*)pModule
+ pModule
->name_table
;
197 p
+= *p
+ 1 + sizeof(WORD
);
198 if (*(WORD
*)(p
+ *p
+ 1) == *pOrd
) break;
201 sprintf( name
, "%.*s.%d: %.*s",
202 *((BYTE
*)pModule
+ pModule
->name_table
),
203 (char *)pModule
+ pModule
->name_table
+ 1,
204 *pOrd
, *p
, (char *)(p
+ 1) );
206 /* Retrieve type info string */
207 return *(LPCSTR
*)((LPBYTE
)PTR_SEG_OFF_TO_LIN( frame
->module_cs
, frame
->callfrom_ip
) + 4);
211 /***********************************************************************
212 * BUILTIN_RegisterDLL
214 * Register a built-in DLL descriptor.
216 void BUILTIN_RegisterDLL( const BUILTIN16_DESCRIPTOR
*descr
)
218 assert( nb_dlls
< MAX_DLLS
);
219 builtin_dlls
[nb_dlls
++] = descr
;