4 * Copyright 1996 Alexandre Julliard
12 #include "wine/winbase16.h"
13 #include "wine/winestring.h"
14 #include "builtin16.h"
15 #include "builtin32.h"
20 #include "stackframe.h"
22 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(module
);
29 LPVOID res_start
; /* address of resource data */
31 DWORD res_size
; /* size of resource data */
35 /* Table of all built-in DLLs */
39 static const BUILTIN16_DESCRIPTOR
*builtin_dlls
[MAX_DLLS
];
43 /***********************************************************************
44 * BUILTIN_DoLoadModule16
46 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule.
48 static HMODULE16
BUILTIN_DoLoadModule16( const BUILTIN16_DESCRIPTOR
*descr
)
52 SEGTABLEENTRY
*pSegTable
;
54 const BUILTIN16_RESOURCE
*rsrc
= descr
->rsrc
;
58 hModule
= GLOBAL_CreateBlock( GMEM_MOVEABLE
, descr
->module_start
,
59 descr
->module_size
, 0,
60 FALSE
, FALSE
, FALSE
);
61 if (!hModule
) return 0;
62 FarSetOwner16( hModule
, hModule
);
64 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
70 hModule
= GLOBAL_Alloc( GMEM_MOVEABLE
,
71 descr
->module_size
+ rsrc
->res_size
,
72 0, FALSE
, FALSE
, FALSE
);
73 if (!hModule
) return 0;
74 FarSetOwner16( hModule
, hModule
);
76 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
77 res_off
= ((NE_MODULE
*)descr
->module_start
)->res_table
;
79 memcpy( (LPBYTE
)pModule
, descr
->module_start
, res_off
);
80 memcpy( (LPBYTE
)pModule
+ res_off
, rsrc
->res_start
, rsrc
->res_size
);
81 memcpy( (LPBYTE
)pModule
+ res_off
+ rsrc
->res_size
,
82 (LPBYTE
)descr
->module_start
+ res_off
, descr
->module_size
- res_off
);
84 /* Have to fix up various pModule-based near pointers. Ugh! */
85 pModule
->name_table
+= rsrc
->res_size
;
86 pModule
->modref_table
+= rsrc
->res_size
;
87 pModule
->import_table
+= rsrc
->res_size
;
88 pModule
->entry_table
+= rsrc
->res_size
;
90 for ( bundle
= (ET_BUNDLE
*)((LPBYTE
)pModule
+ pModule
->entry_table
);
92 bundle
= (ET_BUNDLE
*)((LPBYTE
)pModule
+ bundle
->next
) )
93 bundle
->next
+= rsrc
->res_size
;
95 /* NOTE: (Ab)use the hRsrcMap parameter for resource data pointer */
96 pModule
->hRsrcMap
= rsrc
->res_start
;
98 pModule
->self
= hModule
;
100 TRACE( "Built-in %s: hmodule=%04x\n", descr
->name
, hModule
);
102 /* Allocate the code segment */
104 pSegTable
= NE_SEG_TABLE( pModule
);
105 pSegTable
->hSeg
= GLOBAL_CreateBlock( GMEM_FIXED
, descr
->code_start
,
106 pSegTable
->minsize
, hModule
, TRUE
, TRUE
, FALSE
);
107 if (!pSegTable
->hSeg
) return 0;
110 /* Allocate the data segment */
112 minsize
= pSegTable
->minsize
? pSegTable
->minsize
: 0x10000;
113 minsize
+= pModule
->heap_size
;
114 if (minsize
> 0x10000) minsize
= 0x10000;
115 pSegTable
->hSeg
= GLOBAL_Alloc( GMEM_FIXED
, minsize
,
116 hModule
, FALSE
, FALSE
, FALSE
);
117 if (!pSegTable
->hSeg
) return 0;
118 if (pSegTable
->minsize
) memcpy( GlobalLock16( pSegTable
->hSeg
),
119 descr
->data_start
, pSegTable
->minsize
);
120 if (pModule
->heap_size
)
121 LocalInit16( GlobalHandleToSel16(pSegTable
->hSeg
),
122 pSegTable
->minsize
, minsize
);
125 NE_InitResourceHandler(hModule
);
127 NE_RegisterModule( pModule
);
132 /***********************************************************************
135 * Load a built-in module.
137 HMODULE16
BUILTIN_LoadModule( LPCSTR name
)
139 char dllname
[20], *p
;
143 /* Fix the name in case we have a full path and extension */
145 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
146 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
148 if (strlen(name
) >= sizeof(dllname
)-4) return (HMODULE16
)2;
150 strcpy( dllname
, name
);
151 p
= strrchr( dllname
, '.' );
152 if (!p
) strcat( dllname
, ".dll" );
154 for (i
= 0; i
< nb_dlls
; i
++)
156 const BUILTIN16_DESCRIPTOR
*descr
= builtin_dlls
[i
];
157 NE_MODULE
*pModule
= (NE_MODULE
*)descr
->module_start
;
158 OFSTRUCT
*pOfs
= (OFSTRUCT
*)((LPBYTE
)pModule
+ pModule
->fileinfo
);
159 if (!strcasecmp( pOfs
->szPathName
, dllname
))
160 return BUILTIN_DoLoadModule16( descr
);
163 if ((handle
= BUILTIN32_dlopen( dllname
)))
165 for (i
= 0; i
< nb_dlls
; i
++)
167 const BUILTIN16_DESCRIPTOR
*descr
= builtin_dlls
[i
];
168 NE_MODULE
*pModule
= (NE_MODULE
*)descr
->module_start
;
169 OFSTRUCT
*pOfs
= (OFSTRUCT
*)((LPBYTE
)pModule
+ pModule
->fileinfo
);
170 if (!strcasecmp( pOfs
->szPathName
, dllname
))
171 return BUILTIN_DoLoadModule16( descr
);
173 ERR( "loaded .so but dll %s still not found\n", dllname
);
174 BUILTIN32_dlclose( handle
);
181 /***********************************************************************
182 * BUILTIN_GetEntryPoint16
184 * Return the ordinal, name, and type info corresponding to a CS:IP address.
185 * This is used only by relay debugging.
187 LPCSTR
BUILTIN_GetEntryPoint16( STACK16FRAME
*frame
, LPSTR name
, WORD
*pOrd
)
195 if (!(pModule
= NE_GetPtr( FarGetOwner16( GlobalHandle16( frame
->module_cs
) ))))
200 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ pModule
->entry_table
);
203 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
204 for (i
= bundle
->first
+ 1; i
<= bundle
->last
; i
++)
206 if ((entry
->offs
< frame
->entry_ip
)
207 && (entry
->segnum
== 1) /* code segment ? */
208 && (entry
->offs
>= max_offset
))
210 max_offset
= entry
->offs
;
215 } while ( (bundle
->next
)
216 && (bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+bundle
->next
)));
218 /* Search for the name in the resident names table */
219 /* (built-in modules have no non-resident table) */
221 p
= (BYTE
*)pModule
+ pModule
->name_table
;
224 p
+= *p
+ 1 + sizeof(WORD
);
225 if (*(WORD
*)(p
+ *p
+ 1) == *pOrd
) break;
228 sprintf( name
, "%.*s.%d: %.*s",
229 *((BYTE
*)pModule
+ pModule
->name_table
),
230 (char *)pModule
+ pModule
->name_table
+ 1,
231 *pOrd
, *p
, (char *)(p
+ 1) );
233 /* Retrieve type info string */
234 return *(LPCSTR
*)((LPBYTE
)PTR_SEG_OFF_TO_LIN( frame
->module_cs
, frame
->callfrom_ip
) + 4);
238 /***********************************************************************
239 * BUILTIN_RegisterDLL
241 * Register a built-in DLL descriptor.
243 void BUILTIN_RegisterDLL( const BUILTIN16_DESCRIPTOR
*descr
)
245 assert( nb_dlls
< MAX_DLLS
);
246 builtin_dlls
[nb_dlls
++] = descr
;