source: trunk/kLdr/kLdrModLX.c@ 25

Last change on this file since 25 was 25, checked in by bird, 17 years ago

A blind shot at FAT Mach-O images.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 86.8 KB
Line 
1/* $Id: kLdrModLX.c 25 2009-02-19 00:56:15Z bird $ */
2/** @file
3 * kLdr - The Module Interpreter for the Linear eXecutable (LX) Format.
4 */
5
6/*
7 * Copyright (c) 2006-2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
8 *
9 * This file is part of kStuff.
10 *
11 * kStuff is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * In addition to the permissions in the GNU Lesser General Public
17 * License, you are granted unlimited permission to link the compiled
18 * version of this file into combinations with other programs, and to
19 * distribute those combinations without any restriction coming from
20 * the use of this file.
21 *
22 * kStuff is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with kStuff; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30 * 02110-1301, USA
31 */
32
33/*******************************************************************************
34* Header Files *
35*******************************************************************************/
36#include <k/kLdr.h>
37#include "kLdrInternal.h"
38#include <k/kLdrFmts/lx.h>
39
40
41/*******************************************************************************
42* Defined Constants And Macros *
43*******************************************************************************/
44/** @def KLDRMODLX_STRICT
45 * Define KLDRMODLX_STRICT to enabled strict checks in KLDRMODLX. */
46#define KLDRMODLX_STRICT 1
47
48/** @def KLDRMODLX_ASSERT
49 * Assert that an expression is true when KLDR_STRICT is defined.
50 */
51#ifdef KLDRMODLX_STRICT
52# define KLDRMODLX_ASSERT(expr) kHlpAssert(expr)
53#else
54# define KLDRMODLX_ASSERT(expr) do {} while (0)
55#endif
56
57
58/*******************************************************************************
59* Structures and Typedefs *
60*******************************************************************************/
61/**
62 * Instance data for the LX module interpreter.
63 */
64typedef struct KLDRMODLX
65{
66 /** Pointer to the module. (Follows the section table.) */
67 PKLDRMOD pMod;
68 /** Pointer to the user mapping. */
69 const void *pvMapping;
70 /** The size of the mapped LX image. */
71 KSIZE cbMapped;
72 /** Reserved flags. */
73 KU32 f32Reserved;
74
75 /** The offset of the LX header. */
76 KLDRFOFF offHdr;
77 /** Copy of the LX header. */
78 struct e32_exe Hdr;
79
80 /** Pointer to the loader section.
81 * Allocated together with this strcture. */
82 const KU8 *pbLoaderSection;
83 /** Pointer to the last byte in the loader section. */
84 const KU8 *pbLoaderSectionLast;
85 /** Pointer to the object table in the loader section. */
86 const struct o32_obj *paObjs;
87 /** Pointer to the object page map table in the loader section. */
88 const struct o32_map *paPageMappings;
89 /** Pointer to the resource table in the loader section. */
90 const struct rsrc32 *paRsrcs;
91 /** Pointer to the resident name table in the loader section. */
92 const KU8 *pbResNameTab;
93 /** Pointer to the entry table in the loader section. */
94 const KU8 *pbEntryTab;
95
96 /** Pointer to the non-resident name table. */
97 KU8 *pbNonResNameTab;
98 /** Pointer to the last byte in the non-resident name table. */
99 const KU8 *pbNonResNameTabLast;
100
101 /** Pointer to the fixup section. */
102 KU8 *pbFixupSection;
103 /** Pointer to the last byte in the fixup section. */
104 const KU8 *pbFixupSectionLast;
105 /** Pointer to the fixup page table within pvFixupSection. */
106 const KU32 *paoffPageFixups;
107 /** Pointer to the fixup record table within pvFixupSection. */
108 const KU8 *pbFixupRecs;
109 /** Pointer to the import module name table within pvFixupSection. */
110 const KU8 *pbImportMods;
111 /** Pointer to the import module name table within pvFixupSection. */
112 const KU8 *pbImportProcs;
113} KLDRMODLX, *PKLDRMODLX;
114
115
116/*******************************************************************************
117* Internal Functions *
118*******************************************************************************/
119static int kldrModLXHasDbgInfo(PKLDRMOD pMod, const void *pvBits);
120static int kldrModLXRelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
121 PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
122static int kldrModLXDoCreate(PKRDR pRdr, KLDRFOFF offNewHdr, PKLDRMODLX *ppModLX);
123static const KU8 *kldrModLXDoNameTableLookupByOrdinal(const KU8 *pbNameTable, KI32 cbNameTable, KU32 iOrdinal);
124static int kldrModLXDoNameLookup(PKLDRMODLX pModLX, const char *pchSymbol, KU32 cchSymbol, KU32 *piSymbol);
125static const KU8 *kldrModLXDoNameTableLookupByName(const KU8 *pbNameTable, KI32 cbNameTable,
126 const char *pchSymbol, KSIZE cchSymbol);
127static int kldrModLXDoLoadBits(PKLDRMODLX pModLX, void *pvBits);
128static int kldrModLXDoIterDataUnpacking(KU8 *pbDst, const KU8 *pbSrc, int cbSrc);
129static int kldrModLXDoIterData2Unpacking(KU8 *pbDst, const KU8 *pbSrc, int cbSrc);
130static void kLdrModLXMemCopyW(KU8 *pbDst, const KU8 *pbSrc, int cb);
131static int kldrModLXDoProtect(PKLDRMODLX pModLX, void *pvBits, unsigned fUnprotectOrProtect);
132static int kldrModLXDoCallDLL(PKLDRMODLX pModLX, unsigned uOp, KUPTR uHandle);
133static int kldrModLXDoForwarderQuery(PKLDRMODLX pModLX, const struct e32_entry *pEntry,
134 PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32 *pfKind);
135static int kldrModLXDoLoadFixupSection(PKLDRMODLX pModLX);
136static KI32 kldrModLXDoCall(KUPTR uEntrypoint, KUPTR uHandle, KU32 uOp, void *pvReserved);
137static int kldrModLXDoReloc(KU8 *pbPage, int off, KLDRADDR PageAddress, const struct r32_rlc *prlc,
138 int iSelector, KLDRADDR uValue, KU32 fKind);
139
140
141/**
142 * Create a loader module instance interpreting the executable image found
143 * in the specified file provider instance.
144 *
145 * @returns 0 on success and *ppMod pointing to a module instance.
146 * On failure, a non-zero OS specific error code is returned.
147 * @param pOps Pointer to the registered method table.
148 * @param pRdr The file provider instance to use.
149 * @param fFlags Flags, MBZ.
150 * @param enmCpuArch The desired CPU architecture. KCPUARCH_UNKNOWN means
151 * anything goes, but with a preference for the current
152 * host architecture.
153 * @param offNewHdr The offset of the new header in MZ files. -1 if not found.
154 * @param ppMod Where to store the module instance pointer.
155 */
156static int kldrModLXCreate(PCKLDRMODOPS pOps, PKRDR pRdr, KU32 fFlags, KCPUARCH enmCpuArch, KLDRFOFF offNewHdr, PPKLDRMOD ppMod)
157{
158 PKLDRMODLX pModLX;
159 int rc;
160
161 /*
162 * Create the instance data and do a minimal header validation.
163 */
164 rc = kldrModLXDoCreate(pRdr, offNewHdr, &pModLX);
165 if (!rc)
166 {
167 /*
168 * Match up against the requested CPU architecture.
169 */
170 if ( enmCpuArch == KCPUARCH_UNKNOWN
171 || pModLX->pMod->enmArch == enmCpuArch)
172 {
173 pModLX->pMod->pOps = pOps;
174 pModLX->pMod->u32Magic = KLDRMOD_MAGIC;
175 *ppMod = pModLX->pMod;
176 return 0;
177 }
178 rc = KLDR_ERR_CPU_ARCH_MISMATCH;
179 }
180 kHlpFree(pModLX);
181 return rc;
182}
183
184
185/**
186 * Separate function for reading creating the LX module instance to
187 * simplify cleanup on failure.
188 */
189static int kldrModLXDoCreate(PKRDR pRdr, KLDRFOFF offNewHdr, PKLDRMODLX *ppModLX)
190{
191 struct e32_exe Hdr;
192 PKLDRMODLX pModLX;
193 PKLDRMOD pMod;
194 KSIZE cb;
195 KSIZE cchFilename;
196 KU32 off, offEnd;
197 KU32 i;
198 int rc;
199 int fCanOptimizeMapping;
200 KU32 NextRVA;
201 *ppModLX = NULL;
202
203 /*
204 * Read the signature and file header.
205 */
206 rc = kRdrRead(pRdr, &Hdr, sizeof(Hdr), offNewHdr > 0 ? offNewHdr : 0);
207 if (rc)
208 return rc;
209 if ( Hdr.e32_magic[0] != E32MAGIC1
210 || Hdr.e32_magic[1] != E32MAGIC2)
211 return KLDR_ERR_UNKNOWN_FORMAT;
212
213 /* We're not interested in anything but x86 images. */
214 if ( Hdr.e32_level != E32LEVEL
215 || Hdr.e32_border != E32LEBO
216 || Hdr.e32_worder != E32LEWO
217 || Hdr.e32_cpu < E32CPU286
218 || Hdr.e32_cpu > E32CPU486
219 || Hdr.e32_pagesize != OBJPAGELEN
220 )
221 return KLDR_ERR_LX_BAD_HEADER;
222
223 /* Some rough sanity checks. */
224 offEnd = kRdrSize(pRdr) >= (KLDRFOFF)~(KU32)16 ? ~(KU32)16 : (KU32)kRdrSize(pRdr);
225 if ( Hdr.e32_itermap > offEnd
226 || Hdr.e32_datapage > offEnd
227 || Hdr.e32_nrestab > offEnd
228 || Hdr.e32_nrestab + Hdr.e32_cbnrestab > offEnd
229 || Hdr.e32_ldrsize > offEnd - offNewHdr - sizeof(Hdr)
230 || Hdr.e32_fixupsize > offEnd - offNewHdr - sizeof(Hdr)
231 || Hdr.e32_fixupsize + Hdr.e32_ldrsize > offEnd - offNewHdr - sizeof(Hdr))
232 return KLDR_ERR_LX_BAD_HEADER;
233
234 /* Verify the loader section. */
235 offEnd = Hdr.e32_objtab + Hdr.e32_ldrsize;
236 if (Hdr.e32_objtab < sizeof(Hdr))
237 return KLDR_ERR_LX_BAD_LOADER_SECTION;
238 off = Hdr.e32_objtab + sizeof(struct o32_obj) * Hdr.e32_objcnt;
239 if (off > offEnd)
240 return KLDR_ERR_LX_BAD_LOADER_SECTION;
241 if ( Hdr.e32_objmap
242 && (Hdr.e32_objmap < off || Hdr.e32_objmap > offEnd))
243 return KLDR_ERR_LX_BAD_LOADER_SECTION;
244 if ( Hdr.e32_rsrccnt
245 && ( Hdr.e32_rsrctab < off
246 || Hdr.e32_rsrctab > offEnd
247 || Hdr.e32_rsrctab + sizeof(struct rsrc32) * Hdr.e32_rsrccnt > offEnd))
248 return KLDR_ERR_LX_BAD_LOADER_SECTION;
249 if ( Hdr.e32_restab
250 && (Hdr.e32_restab < off || Hdr.e32_restab > offEnd - 2))
251 return KLDR_ERR_LX_BAD_LOADER_SECTION;
252 if ( Hdr.e32_enttab
253 && (Hdr.e32_enttab < off || Hdr.e32_enttab >= offEnd))
254 return KLDR_ERR_LX_BAD_LOADER_SECTION;
255 if ( Hdr.e32_dircnt
256 && (Hdr.e32_dirtab < off || Hdr.e32_dirtab > offEnd - 2))
257 return KLDR_ERR_LX_BAD_LOADER_SECTION;
258
259 /* Verify the fixup section. */
260 off = offEnd;
261 offEnd = off + Hdr.e32_fixupsize;
262 if ( Hdr.e32_fpagetab
263 && (Hdr.e32_fpagetab < off || Hdr.e32_fpagetab > offEnd))
264 {
265 /*
266 * wlink mixes the fixup section and the loader section.
267 */
268 off = Hdr.e32_fpagetab;
269 offEnd = off + Hdr.e32_fixupsize;
270 Hdr.e32_ldrsize = off - Hdr.e32_objtab;
271 }
272 if ( Hdr.e32_frectab
273 && (Hdr.e32_frectab < off || Hdr.e32_frectab > offEnd))
274 return KLDR_ERR_LX_BAD_FIXUP_SECTION;
275 if ( Hdr.e32_impmod
276 && (Hdr.e32_impmod < off || Hdr.e32_impmod > offEnd || Hdr.e32_impmod + Hdr.e32_impmodcnt > offEnd))
277 return KLDR_ERR_LX_BAD_FIXUP_SECTION;
278 if ( Hdr.e32_impproc
279 && (Hdr.e32_impproc < off || Hdr.e32_impproc > offEnd))
280 return KLDR_ERR_LX_BAD_FIXUP_SECTION;
281
282 /*
283 * Calc the instance size, allocate and initialize it.
284 */
285 cchFilename = kHlpStrLen(kRdrName(pRdr));
286 cb = K_ALIGN_Z(sizeof(KLDRMODLX), 8)
287 + K_ALIGN_Z(K_OFFSETOF(KLDRMOD, aSegments[Hdr.e32_objcnt + 1]), 8)
288 + K_ALIGN_Z(cchFilename + 1, 8)
289 + Hdr.e32_ldrsize + 2; /* +2 for two extra zeros. */
290 pModLX = (PKLDRMODLX)kHlpAlloc(cb);
291 if (!pModLX)
292 return KERR_NO_MEMORY;
293 *ppModLX = pModLX;
294
295 /* KLDRMOD */
296 pMod = (PKLDRMOD)((KU8 *)pModLX + K_ALIGN_Z(sizeof(KLDRMODLX), 8));
297 pMod->pvData = pModLX;
298 pMod->pRdr = pRdr;
299 pMod->pOps = NULL; /* set upon success. */
300 pMod->cSegments = Hdr.e32_objcnt;
301 pMod->cchFilename = cchFilename;
302 pMod->pszFilename = (char *)K_ALIGN_P(&pMod->aSegments[pMod->cSegments], 8);
303 kHlpMemCopy((char *)pMod->pszFilename, kRdrName(pRdr), cchFilename + 1);
304 pMod->pszName = NULL; /* finalized further down */
305 pMod->cchName = 0;
306 switch (Hdr.e32_cpu)
307 {
308 case E32CPU286:
309 pMod->enmCpu = KCPU_I80286;
310 pMod->enmArch = KCPUARCH_X86_16;
311 break;
312 case E32CPU386:
313 pMod->enmCpu = KCPU_I386;
314 pMod->enmArch = KCPUARCH_X86_32;
315 break;
316 case E32CPU486:
317 pMod->enmCpu = KCPU_I486;
318 pMod->enmArch = KCPUARCH_X86_32;
319 break;
320 }
321 pMod->enmEndian = KLDRENDIAN_LITTLE;
322 pMod->enmFmt = KLDRFMT_LX;
323 switch (Hdr.e32_mflags & E32MODMASK)
324 {
325 case E32MODEXE:
326 pMod->enmType = !(Hdr.e32_mflags & E32NOINTFIX)
327 ? KLDRTYPE_EXECUTABLE_RELOCATABLE
328 : KLDRTYPE_EXECUTABLE_FIXED;
329 break;
330
331 case E32MODDLL:
332 case E32PROTDLL:
333 case E32MODPROTDLL:
334 pMod->enmType = !(Hdr.e32_mflags & E32SYSDLL)
335 ? KLDRTYPE_SHARED_LIBRARY_RELOCATABLE
336 : KLDRTYPE_SHARED_LIBRARY_FIXED;
337 break;
338
339 case E32MODPDEV:
340 case E32MODVDEV:
341 pMod->enmType = KLDRTYPE_SHARED_LIBRARY_RELOCATABLE;
342 break;
343 }
344 pMod->u32Magic = 0; /* set upon success. */
345
346 /* KLDRMODLX */
347 pModLX->pMod = pMod;
348 pModLX->pvMapping = 0;
349 pModLX->cbMapped = 0;
350 pModLX->f32Reserved = 0;
351
352 pModLX->offHdr = offNewHdr >= 0 ? offNewHdr : 0;
353 kHlpMemCopy(&pModLX->Hdr, &Hdr, sizeof(Hdr));
354
355 pModLX->pbLoaderSection = K_ALIGN_P(pMod->pszFilename + pMod->cchFilename + 1, 16);
356 pModLX->pbLoaderSectionLast = pModLX->pbLoaderSection + pModLX->Hdr.e32_ldrsize - 1;
357 pModLX->paObjs = NULL;
358 pModLX->paPageMappings = NULL;
359 pModLX->paRsrcs = NULL;
360 pModLX->pbResNameTab = NULL;
361 pModLX->pbEntryTab = NULL;
362
363 pModLX->pbNonResNameTab = NULL;
364 pModLX->pbNonResNameTabLast = NULL;
365
366 pModLX->pbFixupSection = NULL;
367 pModLX->pbFixupSectionLast = NULL;
368 pModLX->paoffPageFixups = NULL;
369 pModLX->pbFixupRecs = NULL;
370 pModLX->pbImportMods = NULL;
371 pModLX->pbImportProcs = NULL;
372
373 /*
374 * Read the loader data.
375 */
376 rc = kRdrRead(pRdr, (void *)pModLX->pbLoaderSection, pModLX->Hdr.e32_ldrsize, pModLX->Hdr.e32_objtab + pModLX->offHdr);
377 if (rc)
378 return rc;
379 ((KU8 *)pModLX->pbLoaderSectionLast)[1] = 0;
380 ((KU8 *)pModLX->pbLoaderSectionLast)[2] = 0;
381 if (pModLX->Hdr.e32_objcnt)
382 pModLX->paObjs = (const struct o32_obj *)pModLX->pbLoaderSection;
383 if (pModLX->Hdr.e32_objmap)
384 pModLX->paPageMappings = (const struct o32_map *)(pModLX->pbLoaderSection + pModLX->Hdr.e32_objmap - pModLX->Hdr.e32_objtab);
385 if (pModLX->Hdr.e32_rsrccnt)
386 pModLX->paRsrcs = (const struct rsrc32 *)(pModLX->pbLoaderSection + pModLX->Hdr.e32_rsrctab - pModLX->Hdr.e32_objtab);
387 if (pModLX->Hdr.e32_restab)
388 pModLX->pbResNameTab = pModLX->pbLoaderSection + pModLX->Hdr.e32_restab - pModLX->Hdr.e32_objtab;
389 if (pModLX->Hdr.e32_enttab)
390 pModLX->pbEntryTab = pModLX->pbLoaderSection + pModLX->Hdr.e32_enttab - pModLX->Hdr.e32_objtab;
391
392 /*
393 * Get the soname from the resident name table.
394 * Very convenient that it's the 0 ordinal, because then we get a
395 * free string terminator.
396 * (The table entry consists of a pascal string followed by a 16-bit ordinal.)
397 */
398 if (pModLX->pbResNameTab)
399 pMod->pszName = (const char *)kldrModLXDoNameTableLookupByOrdinal(pModLX->pbResNameTab,
400 pModLX->pbLoaderSectionLast - pModLX->pbResNameTab + 1,
401 0);
402 if (!pMod->pszName)
403 return KLDR_ERR_LX_NO_SONAME;
404 pMod->cchName = *(const KU8 *)pMod->pszName++;
405 if (pMod->cchName != kHlpStrLen(pMod->pszName))
406 return KLDR_ERR_LX_BAD_SONAME;
407
408 /*
409 * Quick validation of the object table.
410 */
411 cb = 0;
412 for (i = 0; i < pMod->cSegments; i++)
413 {
414 if (pModLX->paObjs[i].o32_base & (OBJPAGELEN - 1))
415 return KLDR_ERR_LX_BAD_OBJECT_TABLE;
416 if (pModLX->paObjs[i].o32_base + pModLX->paObjs[i].o32_size <= pModLX->paObjs[i].o32_base)
417 return KLDR_ERR_LX_BAD_OBJECT_TABLE;
418 if (pModLX->paObjs[i].o32_mapsize > (pModLX->paObjs[i].o32_size + (OBJPAGELEN - 1)))
419 return KLDR_ERR_LX_BAD_OBJECT_TABLE;
420 if ( pModLX->paObjs[i].o32_mapsize
421 && ( (KU8 *)&pModLX->paPageMappings[pModLX->paObjs[i].o32_pagemap] > pModLX->pbLoaderSectionLast
422 || (KU8 *)&pModLX->paPageMappings[pModLX->paObjs[i].o32_pagemap + pModLX->paObjs[i].o32_mapsize]
423 > pModLX->pbLoaderSectionLast))
424 return KLDR_ERR_LX_BAD_OBJECT_TABLE;
425 if (i > 0 && !(pModLX->paObjs[i].o32_flags & OBJRSRC))
426 {
427 if (pModLX->paObjs[i].o32_base <= pModLX->paObjs[i - 1].o32_base)
428 return KLDR_ERR_LX_BAD_OBJECT_TABLE;
429 if (pModLX->paObjs[i].o32_base < pModLX->paObjs[i - 1].o32_base + pModLX->paObjs[i - 1].o32_mapsize)
430 return KLDR_ERR_LX_BAD_OBJECT_TABLE;
431 }
432 }
433
434 /*
435 * Check if we can optimize the mapping by using a different
436 * object alignment. The linker typically uses 64KB alignment,
437 * we can easily get away with page alignment in most cases.
438 */
439 fCanOptimizeMapping = !(Hdr.e32_mflags & (E32NOINTFIX | E32SYSDLL));
440 NextRVA = 0;
441
442 /*
443 * Setup the KLDRMOD segment array.
444 */
445 for (i = 0; i < pMod->cSegments; i++)
446 {
447 /* unused */
448 pMod->aSegments[i].pvUser = NULL;
449 pMod->aSegments[i].MapAddress = 0;
450 pMod->aSegments[i].pchName = NULL;
451 pMod->aSegments[i].cchName = 0;
452 pMod->aSegments[i].offFile = -1;
453 pMod->aSegments[i].cbFile = -1;
454 pMod->aSegments[i].SelFlat = 0;
455 pMod->aSegments[i].Sel16bit = 0;
456
457 /* flags */
458 pMod->aSegments[i].fFlags = 0;
459 if (pModLX->paObjs[i].o32_flags & OBJBIGDEF)
460 pMod->aSegments[i].fFlags = KLDRSEG_FLAG_16BIT;
461 if (pModLX->paObjs[i].o32_flags & OBJALIAS16)
462 pMod->aSegments[i].fFlags = KLDRSEG_FLAG_OS2_ALIAS16;
463 if (pModLX->paObjs[i].o32_flags & OBJCONFORM)
464 pMod->aSegments[i].fFlags = KLDRSEG_FLAG_OS2_CONFORM;
465 if (pModLX->paObjs[i].o32_flags & OBJIOPL)
466 pMod->aSegments[i].fFlags = KLDRSEG_FLAG_OS2_IOPL;
467
468 /* size and addresses */
469 pMod->aSegments[i].Alignment = OBJPAGELEN;
470 pMod->aSegments[i].cb = pModLX->paObjs[i].o32_size;
471 pMod->aSegments[i].LinkAddress = pModLX->paObjs[i].o32_base;
472 pMod->aSegments[i].RVA = NextRVA;
473 if ( fCanOptimizeMapping
474 || i + 1 >= pMod->cSegments
475 || (pModLX->paObjs[i].o32_flags & OBJRSRC)
476 || (pModLX->paObjs[i + 1].o32_flags & OBJRSRC))
477 pMod->aSegments[i].cbMapped = K_ALIGN_Z(pModLX->paObjs[i].o32_size, OBJPAGELEN);
478 else
479 pMod->aSegments[i].cbMapped = pModLX->paObjs[i + 1].o32_base - pModLX->paObjs[i].o32_base;
480 NextRVA += pMod->aSegments[i].cbMapped;
481
482 /* protection */
483 switch ( pModLX->paObjs[i].o32_flags
484 & (OBJSHARED | OBJREAD | OBJWRITE | OBJEXEC))
485 {
486 case 0:
487 case OBJSHARED:
488 pMod->aSegments[i].enmProt = KPROT_NOACCESS;
489 break;
490 case OBJREAD:
491 case OBJREAD | OBJSHARED:
492 pMod->aSegments[i].enmProt = KPROT_READONLY;
493 break;
494 case OBJWRITE:
495 case OBJWRITE | OBJREAD:
496 pMod->aSegments[i].enmProt = KPROT_WRITECOPY;
497 break;
498 case OBJWRITE | OBJSHARED:
499 case OBJWRITE | OBJSHARED | OBJREAD:
500 pMod->aSegments[i].enmProt = KPROT_READWRITE;
501 break;
502 case OBJEXEC:
503 case OBJEXEC | OBJSHARED:
504 pMod->aSegments[i].enmProt = KPROT_EXECUTE;
505 break;
506 case OBJEXEC | OBJREAD:
507 case OBJEXEC | OBJREAD | OBJSHARED:
508 pMod->aSegments[i].enmProt = KPROT_EXECUTE_READ;
509 break;
510 case OBJEXEC | OBJWRITE:
511 case OBJEXEC | OBJWRITE | OBJREAD:
512 pMod->aSegments[i].enmProt = KPROT_EXECUTE_WRITECOPY;
513 break;
514 case OBJEXEC | OBJWRITE | OBJSHARED:
515 case OBJEXEC | OBJWRITE | OBJSHARED | OBJREAD:
516 pMod->aSegments[i].enmProt = KPROT_EXECUTE_READWRITE;
517 break;
518 }
519 if ((pModLX->paObjs[i].o32_flags & (OBJREAD | OBJWRITE | OBJEXEC | OBJRSRC)) == OBJRSRC)
520 pMod->aSegments[i].enmProt = KPROT_READONLY;
521 /*pMod->aSegments[i].f16bit = !(pModLX->paObjs[i].o32_flags & OBJBIGDEF)
522 pMod->aSegments[i].fIOPL = !(pModLX->paObjs[i].o32_flags & OBJIOPL)
523 pMod->aSegments[i].fConforming = !(pModLX->paObjs[i].o32_flags & OBJCONFORM) */
524 }
525
526 /* set the mapping size */
527 pModLX->cbMapped = NextRVA;
528
529 /*
530 * We're done.
531 */
532 *ppModLX = pModLX;
533 return 0;
534}
535
536
537/** @copydoc KLDRMODOPS::pfnDestroy */
538static int kldrModLXDestroy(PKLDRMOD pMod)
539{
540 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
541 int rc = 0;
542 KLDRMODLX_ASSERT(!pModLX->pvMapping);
543
544 if (pMod->pRdr)
545 {
546 rc = kRdrClose(pMod->pRdr);
547 pMod->pRdr = NULL;
548 }
549 if (pModLX->pbNonResNameTab)
550 {
551 kHlpFree(pModLX->pbNonResNameTab);
552 pModLX->pbNonResNameTab = NULL;
553 }
554 if (pModLX->pbFixupSection)
555 {
556 kHlpFree(pModLX->pbFixupSection);
557 pModLX->pbFixupSection = NULL;
558 }
559 pMod->u32Magic = 0;
560 pMod->pOps = NULL;
561 kHlpFree(pModLX);
562 return rc;
563}
564
565
566/**
567 * Resolved base address aliases.
568 *
569 * @param pModLX The interpreter module instance
570 * @param pBaseAddress The base address, IN & OUT.
571 */
572static void kldrModLXResolveBaseAddress(PKLDRMODLX pModLX, PKLDRADDR pBaseAddress)
573{
574 if (*pBaseAddress == KLDRMOD_BASEADDRESS_MAP)
575 *pBaseAddress = pModLX->pMod->aSegments[0].MapAddress;
576 else if (*pBaseAddress == KLDRMOD_BASEADDRESS_LINK)
577 *pBaseAddress = pModLX->pMod->aSegments[0].LinkAddress;
578}
579
580
581/** @copydoc kLdrModQuerySymbol */
582static int kldrModLXQuerySymbol(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 iSymbol,
583 const char *pchSymbol, KSIZE cchSymbol, const char *pszVersion,
584 PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32 *pfKind)
585{
586 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
587 KU32 iOrdinal;
588 int rc;
589 const struct b32_bundle *pBundle;
590
591
592 /*
593 * Give up at once if there is no entry table.
594 */
595 if (!pModLX->Hdr.e32_enttab)
596 return KLDR_ERR_SYMBOL_NOT_FOUND;
597
598 /*
599 * Translate the symbol name into an ordinal.
600 */
601 if (pchSymbol)
602 {
603 rc = kldrModLXDoNameLookup(pModLX, pchSymbol, cchSymbol, &iSymbol);
604 if (rc)
605 return rc;
606 }
607
608 /*
609 * Iterate the entry table.
610 * (The entry table is made up of bundles of similar exports.)
611 */
612 iOrdinal = 1;
613 pBundle = (const struct b32_bundle *)pModLX->pbEntryTab;
614 while (pBundle->b32_cnt && iOrdinal <= iSymbol)
615 {
616 static const KSIZE s_cbEntry[] = { 0, 3, 5, 5, 7 };
617
618 /*
619 * Check for a hit first.
620 */
621 iOrdinal += pBundle->b32_cnt;
622 if (iSymbol < iOrdinal)
623 {
624 KU32 offObject;
625 const struct e32_entry *pEntry = (const struct e32_entry *)((KUPTR)(pBundle + 1)
626 + (iSymbol - (iOrdinal - pBundle->b32_cnt))
627 * s_cbEntry[pBundle->b32_type]);
628
629 /*
630 * Calculate the return address.
631 */
632 kldrModLXResolveBaseAddress(pModLX, &BaseAddress);
633 switch (pBundle->b32_type)
634 {
635 /* empty bundles are place holders unused ordinal ranges. */
636 case EMPTY:
637 return KLDR_ERR_SYMBOL_NOT_FOUND;
638
639 /* e32_flags + a 16-bit offset. */
640 case ENTRY16:
641 offObject = pEntry->e32_variant.e32_offset.offset16;
642 if (pfKind)
643 *pfKind = KLDRSYMKIND_16BIT | KLDRSYMKIND_NO_TYPE;
644 break;
645
646 /* e32_flags + a 16-bit offset + a 16-bit callgate selector. */
647 case GATE16:
648 offObject = pEntry->e32_variant.e32_callgate.offset;
649 if (pfKind)
650 *pfKind = KLDRSYMKIND_16BIT | KLDRSYMKIND_CODE;
651 break;
652
653 /* e32_flags + a 32-bit offset. */
654 case ENTRY32:
655 offObject = pEntry->e32_variant.e32_offset.offset32;
656 if (pfKind)
657 *pfKind = KLDRSYMKIND_32BIT;
658 break;
659
660 /* e32_flags + 16-bit import module ordinal + a 32-bit procname or ordinal. */
661 case ENTRYFWD:
662 return kldrModLXDoForwarderQuery(pModLX, pEntry, pfnGetForwarder, pvUser, puValue, pfKind);
663
664 default:
665 /* anyone actually using TYPEINFO will end up here. */
666 KLDRMODLX_ASSERT(!"Bad bundle type");
667 return KLDR_ERR_LX_BAD_BUNDLE;
668 }
669
670 /*
671 * Validate the object number and calc the return address.
672 */
673 if ( pBundle->b32_obj <= 0
674 || pBundle->b32_obj > pMod->cSegments)
675 return KLDR_ERR_LX_BAD_BUNDLE;
676 if (puValue)
677 *puValue = BaseAddress
678 + offObject
679 + pMod->aSegments[pBundle->b32_obj - 1].RVA;
680 return 0;
681 }
682
683 /*
684 * Skip the bundle.
685 */
686 if (pBundle->b32_type > ENTRYFWD)
687 {
688 KLDRMODLX_ASSERT(!"Bad type"); /** @todo figure out TYPEINFO. */
689 return KLDR_ERR_LX_BAD_BUNDLE;
690 }
691 if (pBundle->b32_type == 0)
692 pBundle = (const struct b32_bundle *)((const KU8 *)pBundle + 2);
693 else
694 pBundle = (const struct b32_bundle *)((const KU8 *)(pBundle + 1) + s_cbEntry[pBundle->b32_type] * pBundle->b32_cnt);
695 }
696
697 return KLDR_ERR_SYMBOL_NOT_FOUND;
698}
699
700
701/**
702 * Do name lookup.
703 *
704 * @returns See kLdrModQuerySymbol.
705 * @param pModLX The module to lookup the symbol in.
706 * @param pchSymbol The symbol to lookup.
707 * @param cchSymbol The symbol name length.
708 * @param piSymbol Where to store the symbol ordinal.
709 */
710static int kldrModLXDoNameLookup(PKLDRMODLX pModLX, const char *pchSymbol, KU32 cchSymbol, KU32 *piSymbol)
711{
712
713 /*
714 * First do a hash table lookup.
715 */
716 /** @todo hash name table for speed. */
717
718 /*
719 * Search the name tables.
720 */
721 const KU8 *pbName = kldrModLXDoNameTableLookupByName(pModLX->pbResNameTab,
722 pModLX->pbLoaderSectionLast - pModLX->pbResNameTab + 1,
723 pchSymbol, cchSymbol);
724 if (!pbName)
725 {
726 if (!pModLX->pbNonResNameTab)
727 {
728 /* lazy load it */
729 /** @todo non-resident name table. */
730 }
731 if (pModLX->pbNonResNameTab)
732 pbName = kldrModLXDoNameTableLookupByName(pModLX->pbResNameTab,
733 pModLX->pbNonResNameTabLast - pModLX->pbResNameTab + 1,
734 pchSymbol, cchSymbol);
735 }
736 if (!pbName)
737 return KLDR_ERR_SYMBOL_NOT_FOUND;
738
739 *piSymbol = *(const KU16 *)(pbName + 1 + *pbName);
740 return 0;
741}
742
743
744#if 0
745/**
746 * Hash a symbol using the algorithm from sdbm.
747 *
748 * The following was is the documenation of the orignal sdbm functions:
749 *
750 * This algorithm was created for sdbm (a public-domain reimplementation of
751 * ndbm) database library. it was found to do well in scrambling bits,
752 * causing better distribution of the keys and fewer splits. it also happens
753 * to be a good general hashing function with good distribution. the actual
754 * function is hash(i) = hash(i - 1) * 65599 + str[i]; what is included below
755 * is the faster version used in gawk. [there is even a faster, duff-device
756 * version] the magic constant 65599 was picked out of thin air while
757 * experimenting with different constants, and turns out to be a prime.
758 * this is one of the algorithms used in berkeley db (see sleepycat) and
759 * elsewhere.
760 */
761static KU32 kldrModLXDoHash(const char *pchSymbol, KU8 cchSymbol)
762{
763 KU32 hash = 0;
764 int ch;
765
766 while ( cchSymbol-- > 0
767 && (ch = *(unsigned const char *)pchSymbol++))
768 hash = ch + (hash << 6) + (hash << 16) - hash;
769
770 return hash;
771}
772#endif
773
774
775/**
776 * Lookup a name table entry by name.
777 *
778 * @returns Pointer to the name table entry if found.
779 * @returns NULL if not found.
780 * @param pbNameTable Pointer to the name table that should be searched.
781 * @param cbNameTable The size of the name table.
782 * @param pchSymbol The name of the symbol we're looking for.
783 * @param cchSymbol The length of the symbol name.
784 */
785static const KU8 *kldrModLXDoNameTableLookupByName(const KU8 *pbNameTable, KI32 cbNameTable,
786 const char *pchSymbol, KSIZE cchSymbol)
787{
788 /*
789 * Determin the namelength up front so we can skip anything which doesn't matches the length.
790 */
791 KU8 cbSymbol8Bit = (KU8)cchSymbol;
792 if (cbSymbol8Bit != cchSymbol)
793 return NULL; /* too long. */
794
795 /*
796 * Walk the name table.
797 */
798 while (*pbNameTable != 0 && cbNameTable > 0)
799 {
800 const KU8 cbName = *pbNameTable;
801
802 cbNameTable -= cbName + 1 + 2;
803 if (cbNameTable < 0)
804 break;
805
806 if ( cbName == cbSymbol8Bit
807 && !kHlpMemComp(pbNameTable + 1, pchSymbol, cbName))
808 return pbNameTable;
809
810 /* next entry */
811 pbNameTable += cbName + 1 + 2;
812 }
813
814 return NULL;
815}
816
817
818/**
819 * Deal with a forwarder entry.
820 *
821 * @returns See kLdrModQuerySymbol.
822 * @param pModLX The PE module interpreter instance.
823 * @param pEntry The forwarder entry.
824 * @param pfnGetForwarder The callback for resolving forwarder symbols. (optional)
825 * @param pvUser The user argument for the callback.
826 * @param puValue Where to put the value. (optional)
827 * @param pfKind Where to put the symbol kind. (optional)
828 */
829static int kldrModLXDoForwarderQuery(PKLDRMODLX pModLX, const struct e32_entry *pEntry,
830 PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32 *pfKind)
831{
832 int rc;
833 KU32 iSymbol;
834 const char *pchSymbol;
835 KU8 cchSymbol;
836
837 if (!pfnGetForwarder)
838 return KLDR_ERR_FORWARDER_SYMBOL;
839
840 /*
841 * Validate the entry import module ordinal.
842 */
843 if ( !pEntry->e32_variant.e32_fwd.modord
844 || pEntry->e32_variant.e32_fwd.modord > pModLX->Hdr.e32_impmodcnt)
845 return KLDR_ERR_LX_BAD_FORWARDER;
846
847 /*
848 * Figure out the parameters.
849 */
850 if (pEntry->e32_flags & FWD_ORDINAL)
851 {
852 iSymbol = pEntry->e32_variant.e32_fwd.value;
853 pchSymbol = NULL; /* no symbol name. */
854 cchSymbol = 0;
855 }
856 else
857 {
858 const KU8 *pbName;
859
860 /* load the fixup section if necessary. */
861 if (!pModLX->pbImportProcs)
862 {
863 rc = kldrModLXDoLoadFixupSection(pModLX);
864 if (rc)
865 return rc;
866 }
867
868 /* Make name pointer. */
869 pbName = pModLX->pbImportProcs + pEntry->e32_variant.e32_fwd.value;
870 if ( pbName >= pModLX->pbFixupSectionLast
871 || pbName < pModLX->pbFixupSection
872 || !*pbName)
873 return KLDR_ERR_LX_BAD_FORWARDER;
874
875
876 /* check for '#' name. */
877 if (pbName[1] == '#')
878 {
879 KU8 cbLeft = *pbName;
880 const KU8 *pb = pbName + 1;
881 unsigned uBase;
882
883 /* base detection */
884 uBase = 10;
885 if ( cbLeft > 1
886 && pb[1] == '0'
887 && (pb[2] == 'x' || pb[2] == 'X'))
888 {
889 uBase = 16;
890 pb += 2;
891 cbLeft -= 2;
892 }
893
894 /* ascii to integer */
895 iSymbol = 0;
896 while (cbLeft-- > 0)
897 {
898 /* convert char to digit. */
899 unsigned uDigit = *pb++;
900 if (uDigit >= '0' && uDigit <= '9')
901 uDigit -= '0';
902 else if (uDigit >= 'a' && uDigit <= 'z')
903 uDigit -= 'a' + 10;
904 else if (uDigit >= 'A' && uDigit <= 'Z')
905 uDigit -= 'A' + 10;
906 else if (!uDigit)
907 break;
908 else
909 return KLDR_ERR_LX_BAD_FORWARDER;
910 if (uDigit >= uBase)
911 return KLDR_ERR_LX_BAD_FORWARDER;
912
913 /* insert the digit */
914 iSymbol *= uBase;
915 iSymbol += uDigit;
916 }
917 if (!iSymbol)
918 return KLDR_ERR_LX_BAD_FORWARDER;
919
920 pchSymbol = NULL; /* no symbol name. */
921 cchSymbol = 0;
922 }
923 else
924 {
925 pchSymbol = (char *)pbName + 1;
926 cchSymbol = *pbName;
927 iSymbol = NIL_KLDRMOD_SYM_ORDINAL;
928 }
929 }
930
931 /*
932 * Resolve the forwarder.
933 */
934 rc = pfnGetForwarder(pModLX->pMod, pEntry->e32_variant.e32_fwd.modord - 1, iSymbol, pchSymbol, cchSymbol, NULL, puValue, pfKind, pvUser);
935 if (!rc && pfKind)
936 *pfKind |= KLDRSYMKIND_FORWARDER;
937 return rc;
938}
939
940
941/**
942 * Loads the fixup section from the executable image.
943 *
944 * The fixup section isn't loaded until it's accessed. It's also freed by kLdrModDone().
945 *
946 * @returns 0 on success, non-zero kLdr or native status code on failure.
947 * @param pModLX The PE module interpreter instance.
948 */
949static int kldrModLXDoLoadFixupSection(PKLDRMODLX pModLX)
950{
951 int rc;
952 KU32 off;
953 void *pv;
954
955 pv = kHlpAlloc(pModLX->Hdr.e32_fixupsize);
956 if (!pv)
957 return KERR_NO_MEMORY;
958
959 off = pModLX->Hdr.e32_objtab + pModLX->Hdr.e32_ldrsize;
960 rc = kRdrRead(pModLX->pMod->pRdr, pv, pModLX->Hdr.e32_fixupsize,
961 off + pModLX->offHdr);
962 if (!rc)
963 {
964 pModLX->pbFixupSection = pv;
965 pModLX->pbFixupSectionLast = pModLX->pbFixupSection + pModLX->Hdr.e32_fixupsize;
966 KLDRMODLX_ASSERT(!pModLX->paoffPageFixups);
967 if (pModLX->Hdr.e32_fpagetab)
968 pModLX->paoffPageFixups = (const KU32 *)(pModLX->pbFixupSection + pModLX->Hdr.e32_fpagetab - off);
969 KLDRMODLX_ASSERT(!pModLX->pbFixupRecs);
970 if (pModLX->Hdr.e32_frectab)
971 pModLX->pbFixupRecs = pModLX->pbFixupSection + pModLX->Hdr.e32_frectab - off;
972 KLDRMODLX_ASSERT(!pModLX->pbImportMods);
973 if (pModLX->Hdr.e32_impmod)
974 pModLX->pbImportMods = pModLX->pbFixupSection + pModLX->Hdr.e32_impmod - off;
975 KLDRMODLX_ASSERT(!pModLX->pbImportProcs);
976 if (pModLX->Hdr.e32_impproc)
977 pModLX->pbImportProcs = pModLX->pbFixupSection + pModLX->Hdr.e32_impproc - off;
978 }
979 else
980 kHlpFree(pv);
981 return rc;
982}
983
984
985/** @copydoc kLdrModEnumSymbols */
986static int kldrModLXEnumSymbols(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress,
987 KU32 fFlags, PFNKLDRMODENUMSYMS pfnCallback, void *pvUser)
988{
989 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
990 const struct b32_bundle *pBundle;
991 KU32 iOrdinal;
992 int rc = 0;
993
994 kldrModLXResolveBaseAddress(pModLX, &BaseAddress);
995
996 /*
997 * Enumerate the entry table.
998 * (The entry table is made up of bundles of similar exports.)
999 */
1000 iOrdinal = 1;
1001 pBundle = (const struct b32_bundle *)pModLX->pbEntryTab;
1002 while (pBundle->b32_cnt && iOrdinal)
1003 {
1004 static const KSIZE s_cbEntry[] = { 0, 3, 5, 5, 7 };
1005
1006 /*
1007 * Enum the entries in the bundle.
1008 */
1009 if (pBundle->b32_type != EMPTY)
1010 {
1011 const struct e32_entry *pEntry;
1012 KSIZE cbEntry;
1013 KLDRADDR BundleRVA;
1014 unsigned cLeft;
1015
1016
1017 /* Validate the bundle. */
1018 switch (pBundle->b32_type)
1019 {
1020 case ENTRY16:
1021 case GATE16:
1022 case ENTRY32:
1023 if ( pBundle->b32_obj <= 0
1024 || pBundle->b32_obj > pMod->cSegments)
1025 return KLDR_ERR_LX_BAD_BUNDLE;
1026 BundleRVA = pMod->aSegments[pBundle->b32_obj - 1].RVA;
1027 break;
1028
1029 case ENTRYFWD:
1030 BundleRVA = 0;
1031 break;
1032
1033 default:
1034 /* anyone actually using TYPEINFO will end up here. */
1035 KLDRMODLX_ASSERT(!"Bad bundle type");
1036 return KLDR_ERR_LX_BAD_BUNDLE;
1037 }
1038
1039 /* iterate the bundle entries. */
1040 cbEntry = s_cbEntry[pBundle->b32_type];
1041 pEntry = (const struct e32_entry *)(pBundle + 1);
1042 cLeft = pBundle->b32_cnt;
1043 while (cLeft-- > 0)
1044 {
1045 KLDRADDR uValue;
1046 KU32 fKind;
1047 int fFoundName;
1048 const KU8 *pbName;
1049
1050 /*
1051 * Calc the symbol value and kind.
1052 */
1053 switch (pBundle->b32_type)
1054 {
1055 /* e32_flags + a 16-bit offset. */
1056 case ENTRY16:
1057 uValue = BaseAddress + BundleRVA + pEntry->e32_variant.e32_offset.offset16;
1058 fKind = KLDRSYMKIND_16BIT | KLDRSYMKIND_NO_TYPE;
1059 break;
1060
1061 /* e32_flags + a 16-bit offset + a 16-bit callgate selector. */
1062 case GATE16:
1063 uValue = BaseAddress + BundleRVA + pEntry->e32_variant.e32_callgate.offset;
1064 fKind = KLDRSYMKIND_16BIT | KLDRSYMKIND_CODE;
1065 break;
1066
1067 /* e32_flags + a 32-bit offset. */
1068 case ENTRY32:
1069 uValue = BaseAddress + BundleRVA + pEntry->e32_variant.e32_offset.offset32;
1070 fKind = KLDRSYMKIND_32BIT;
1071 break;
1072
1073 /* e32_flags + 16-bit import module ordinal + a 32-bit procname or ordinal. */
1074 case ENTRYFWD:
1075 uValue = 0; /** @todo implement enumeration of forwarders properly. */
1076 fKind = KLDRSYMKIND_FORWARDER;
1077 break;
1078 }
1079
1080 /*
1081 * Any symbol names?
1082 */
1083 fFoundName = 0;
1084
1085 /* resident name table. */
1086 pbName = pModLX->pbResNameTab;
1087 if (pbName)
1088 {
1089 do
1090 {
1091 pbName = kldrModLXDoNameTableLookupByOrdinal(pbName, pModLX->pbLoaderSectionLast - pbName + 1, iOrdinal);
1092 if (!pbName)
1093 break;
1094 fFoundName = 1;
1095 rc = pfnCallback(pMod, iOrdinal, (const char *)pbName + 1, *pbName, NULL, uValue, fKind, pvUser);
1096 if (rc)
1097 return rc;
1098
1099 /* skip to the next entry */
1100 pbName += 1 + *pbName + 2;
1101 } while (pbName < pModLX->pbLoaderSectionLast);
1102 }
1103
1104 /* resident name table. */
1105 pbName = pModLX->pbNonResNameTab;
1106 /** @todo lazy load the non-resident name table. */
1107 if (pbName)
1108 {
1109 do
1110 {
1111 pbName = kldrModLXDoNameTableLookupByOrdinal(pbName, pModLX->pbNonResNameTabLast - pbName + 1, iOrdinal);
1112 if (!pbName)
1113 break;
1114 fFoundName = 1;
1115 rc = pfnCallback(pMod, iOrdinal, (const char *)pbName + 1, *pbName, NULL, uValue, fKind, pvUser);
1116 if (rc)
1117 return rc;
1118
1119 /* skip to the next entry */
1120 pbName += 1 + *pbName + 2;
1121 } while (pbName < pModLX->pbLoaderSectionLast);
1122 }
1123
1124 /*
1125 * If no names, call once with the ordinal only.
1126 */
1127 if (!fFoundName)
1128 {
1129 rc = pfnCallback(pMod, iOrdinal, NULL, 0, NULL, uValue, fKind, pvUser);
1130 if (rc)
1131 return rc;
1132 }
1133
1134 /* next */
1135 iOrdinal++;
1136 pEntry = (const struct e32_entry *)((KUPTR)pEntry + cbEntry);
1137 }
1138 }
1139
1140 /*
1141 * The next bundle.
1142 */
1143 if (pBundle->b32_type > ENTRYFWD)
1144 {
1145 KLDRMODLX_ASSERT(!"Bad type"); /** @todo figure out TYPEINFO. */
1146 return KLDR_ERR_LX_BAD_BUNDLE;
1147 }
1148 if (pBundle->b32_type == 0)
1149 pBundle = (const struct b32_bundle *)((const KU8 *)pBundle + 2);
1150 else
1151 pBundle = (const struct b32_bundle *)((const KU8 *)(pBundle + 1) + s_cbEntry[pBundle->b32_type] * pBundle->b32_cnt);
1152 }
1153
1154 return 0;
1155}
1156
1157
1158/**
1159 * Lookup a name table entry by ordinal.
1160 *
1161 * @returns Pointer to the name table entry if found.
1162 * @returns NULL if not found.
1163 * @param pbNameTable Pointer to the name table that should be searched.
1164 * @param cbNameTable The size of the name table.
1165 * @param iOrdinal The ordinal to search for.
1166 */
1167static const KU8 *kldrModLXDoNameTableLookupByOrdinal(const KU8 *pbNameTable, KI32 cbNameTable, KU32 iOrdinal)
1168{
1169 while (*pbNameTable != 0 && cbNameTable > 0)
1170 {
1171 const KU8 cbName = *pbNameTable;
1172 KU32 iName;
1173
1174 cbNameTable -= cbName + 1 + 2;
1175 if (cbNameTable < 0)
1176 break;
1177
1178 iName = *(pbNameTable + cbName + 1)
1179 | ((unsigned)*(pbNameTable + cbName + 2) << 8);
1180 if (iName == iOrdinal)
1181 return pbNameTable;
1182
1183 /* next entry */
1184 pbNameTable += cbName + 1 + 2;
1185 }
1186
1187 return NULL;
1188}
1189
1190
1191/** @copydoc kLdrModGetImport */
1192static int kldrModLXGetImport(PKLDRMOD pMod, const void *pvBits, KU32 iImport, char *pszName, KSIZE cchName)
1193{
1194 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
1195 const KU8 *pb;
1196 int rc;
1197
1198 /*
1199 * Validate
1200 */
1201 if (iImport >= pModLX->Hdr.e32_impmodcnt)
1202 return KLDR_ERR_IMPORT_ORDINAL_OUT_OF_BOUNDS;
1203
1204 /*
1205 * Lazy loading the fixup section.
1206 */
1207 if (!pModLX->pbImportMods)
1208 {
1209 rc = kldrModLXDoLoadFixupSection(pModLX);
1210 if (rc)
1211 return rc;
1212 }
1213
1214 /*
1215 * Iterate the module import table until we reach the requested import ordinal.
1216 */
1217 pb = pModLX->pbImportMods;
1218 while (iImport-- > 0)
1219 pb += *pb + 1;
1220
1221 /*
1222 * Copy out the result.
1223 */
1224 if (*pb < cchName)
1225 {
1226 kHlpMemCopy(pszName, pb + 1, *pb);
1227 pszName[*pb] = '\0';
1228 rc = 0;
1229 }
1230 else
1231 {
1232 kHlpMemCopy(pszName, pb + 1, cchName);
1233 if (cchName)
1234 pszName[cchName - 1] = '\0';
1235 rc = KERR_BUFFER_OVERFLOW;
1236 }
1237
1238 return rc;
1239}
1240
1241
1242/** @copydoc kLdrModNumberOfImports */
1243static KI32 kldrModLXNumberOfImports(PKLDRMOD pMod, const void *pvBits)
1244{
1245 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
1246 return pModLX->Hdr.e32_impmodcnt;
1247}
1248
1249
1250/** @copydoc kLdrModGetStackInfo */
1251static int kldrModLXGetStackInfo(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo)
1252{
1253 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
1254 const KU32 i = pModLX->Hdr.e32_stackobj;
1255
1256 if ( i
1257 && i <= pMod->cSegments
1258 && pModLX->Hdr.e32_esp <= pMod->aSegments[i - 1].LinkAddress + pMod->aSegments[i - 1].cb
1259 && pModLX->Hdr.e32_stacksize
1260 && pModLX->Hdr.e32_esp - pModLX->Hdr.e32_stacksize >= pMod->aSegments[i - 1].LinkAddress)
1261 {
1262
1263 kldrModLXResolveBaseAddress(pModLX, &BaseAddress);
1264 pStackInfo->LinkAddress = pModLX->Hdr.e32_esp - pModLX->Hdr.e32_stacksize;
1265 pStackInfo->Address = BaseAddress
1266 + pMod->aSegments[i - 1].RVA
1267 + pModLX->Hdr.e32_esp - pModLX->Hdr.e32_stacksize - pMod->aSegments[i - 1].LinkAddress;
1268 }
1269 else
1270 {
1271 pStackInfo->Address = NIL_KLDRADDR;
1272 pStackInfo->LinkAddress = NIL_KLDRADDR;
1273 }
1274 pStackInfo->cbStack = pModLX->Hdr.e32_stacksize;
1275 pStackInfo->cbStackThread = 0;
1276
1277 return 0;
1278}
1279
1280
1281/** @copydoc kLdrModQueryMainEntrypoint */
1282static int kldrModLXQueryMainEntrypoint(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRADDR pMainEPAddress)
1283{
1284 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
1285
1286 /*
1287 * Convert the address from the header.
1288 */
1289 kldrModLXResolveBaseAddress(pModLX, &BaseAddress);
1290 *pMainEPAddress = pModLX->Hdr.e32_startobj
1291 && pModLX->Hdr.e32_startobj <= pMod->cSegments
1292 && pModLX->Hdr.e32_eip < pMod->aSegments[pModLX->Hdr.e32_startobj - 1].cb
1293 ? BaseAddress + pMod->aSegments[pModLX->Hdr.e32_startobj - 1].RVA + pModLX->Hdr.e32_eip
1294 : NIL_KLDRADDR;
1295 return 0;
1296}
1297
1298
1299/** @copydoc kLdrModEnumDbgInfo */
1300static int kldrModLXEnumDbgInfo(PKLDRMOD pMod, const void *pvBits, PFNKLDRENUMDBG pfnCallback, void *pvUser)
1301{
1302 /*PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;*/
1303
1304 /*
1305 * Quit immediately if no debug info.
1306 */
1307 if (kldrModLXHasDbgInfo(pMod, pvBits))
1308 return 0;
1309#if 0
1310 /*
1311 * Read the debug info and look for familiar magics and structures.
1312 */
1313 /** @todo */
1314#endif
1315
1316 return 0;
1317}
1318
1319
1320/** @copydoc kLdrModHasDbgInfo */
1321static int kldrModLXHasDbgInfo(PKLDRMOD pMod, const void *pvBits)
1322{
1323 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
1324
1325 /*
1326 * Don't curretnly bother with linkers which doesn't advertise it in the header.
1327 */
1328 if ( !pModLX->Hdr.e32_debuginfo
1329 || !pModLX->Hdr.e32_debuglen)
1330 return KLDR_ERR_NO_DEBUG_INFO;
1331 return 0;
1332}
1333
1334
1335/** @copydoc kLdrModMap */
1336static int kldrModLXMap(PKLDRMOD pMod)
1337{
1338 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
1339 unsigned fFixed;
1340 void *pvBase;
1341 int rc;
1342
1343 /*
1344 * Already mapped?
1345 */
1346 if (pModLX->pvMapping)
1347 return KLDR_ERR_ALREADY_MAPPED;
1348
1349 /*
1350 * Allocate memory for it.
1351 */
1352 /* fixed image? */
1353 fFixed = pMod->enmType == KLDRTYPE_EXECUTABLE_FIXED
1354 || pMod->enmType == KLDRTYPE_SHARED_LIBRARY_FIXED;
1355 if (!fFixed)
1356 pvBase = NULL;
1357 else
1358 {
1359 pvBase = (void *)(KUPTR)pMod->aSegments[0].LinkAddress;
1360 if ((KUPTR)pvBase != pMod->aSegments[0].LinkAddress)
1361 return KLDR_ERR_ADDRESS_OVERFLOW;
1362 }
1363 rc = kHlpPageAlloc(&pvBase, pModLX->cbMapped, KPROT_EXECUTE_READWRITE, fFixed);
1364 if (rc)
1365 return rc;
1366
1367 /*
1368 * Load the bits, apply page protection, and update the segment table.
1369 */
1370 rc = kldrModLXDoLoadBits(pModLX, pvBase);
1371 if (!rc)
1372 rc = kldrModLXDoProtect(pModLX, pvBase, 0 /* protect */);
1373 if (!rc)
1374 {
1375 KU32 i;
1376 for (i = 0; i < pMod->cSegments; i++)
1377 {
1378 if (pMod->aSegments[i].RVA != NIL_KLDRADDR)
1379 pMod->aSegments[i].MapAddress = (KUPTR)pvBase + (KUPTR)pMod->aSegments[i].RVA;
1380 }
1381 pModLX->pvMapping = pvBase;
1382 }
1383 else
1384 kHlpPageFree(pvBase, pModLX->cbMapped);
1385 return rc;
1386}
1387
1388
1389/**
1390 * Loads the LX pages into the specified memory mapping.
1391 *
1392 * @returns 0 on success.
1393 * @returns non-zero kLdr or OS status code on failure.
1394 *
1395 * @param pModLX The LX module interpreter instance.
1396 * @param pvBits Where to load the bits.
1397 */
1398static int kldrModLXDoLoadBits(PKLDRMODLX pModLX, void *pvBits)
1399{
1400 const PKRDR pRdr = pModLX->pMod->pRdr;
1401 KU8 *pbTmpPage = NULL;
1402 int rc = 0;
1403 KU32 i;
1404
1405 /*
1406 * Iterate the segments.
1407 */
1408 for (i = 0; i < pModLX->Hdr.e32_objcnt; i++)
1409 {
1410 const struct o32_obj * const pObj = &pModLX->paObjs[i];
1411 const KU32 cPages = pModLX->pMod->aSegments[i].cbMapped / OBJPAGELEN;
1412 KU32 iPage;
1413 KU8 *pbPage = (KU8 *)pvBits + (KUPTR)pModLX->pMod->aSegments[i].RVA;
1414
1415 /*
1416 * Iterate the page map pages.
1417 */
1418 for (iPage = 0; !rc && iPage < pObj->o32_mapsize; iPage++, pbPage += OBJPAGELEN)
1419 {
1420 const struct o32_map *pMap = &pModLX->paPageMappings[iPage + pObj->o32_pagemap - 1];
1421 switch (pMap->o32_pageflags)
1422 {
1423 case VALID:
1424 if (pMap->o32_pagesize == OBJPAGELEN)
1425 rc = kRdrRead(pRdr, pbPage, OBJPAGELEN,
1426 pModLX->Hdr.e32_datapage + (pMap->o32_pagedataoffset << pModLX->Hdr.e32_pageshift));
1427 else if (pMap->o32_pagesize < OBJPAGELEN)
1428 {
1429 rc = kRdrRead(pRdr, pbPage, pMap->o32_pagesize,
1430 pModLX->Hdr.e32_datapage + (pMap->o32_pagedataoffset << pModLX->Hdr.e32_pageshift));
1431 kHlpMemSet(pbPage + pMap->o32_pagesize, 0, OBJPAGELEN - pMap->o32_pagesize);
1432 }
1433 else
1434 rc = KLDR_ERR_LX_BAD_PAGE_MAP;
1435 break;
1436
1437 case ITERDATA:
1438 case ITERDATA2:
1439 /* make sure we've got a temp page .*/
1440 if (!pbTmpPage)
1441 {
1442 pbTmpPage = kHlpAlloc(OBJPAGELEN + 256);
1443 if (!pbTmpPage)
1444 break;
1445 }
1446 /* validate the size. */
1447 if (pMap->o32_pagesize > OBJPAGELEN + 252)
1448 {
1449 rc = KLDR_ERR_LX_BAD_PAGE_MAP;
1450 break;
1451 }
1452
1453 /* read it and ensure 4 extra zero bytes. */
1454 rc = kRdrRead(pRdr, pbTmpPage, pMap->o32_pagesize,
1455 pModLX->Hdr.e32_datapage + (pMap->o32_pagedataoffset << pModLX->Hdr.e32_pageshift));
1456 if (rc)
1457 break;
1458 kHlpMemSet(pbTmpPage + pMap->o32_pagesize, 0, 4);
1459
1460 /* unpack it into the image page. */
1461 if (pMap->o32_pageflags == ITERDATA2)
1462 rc = kldrModLXDoIterData2Unpacking(pbPage, pbTmpPage, pMap->o32_pagesize);
1463 else
1464 rc = kldrModLXDoIterDataUnpacking(pbPage, pbTmpPage, pMap->o32_pagesize);
1465 break;
1466
1467 case INVALID: /* we're probably not dealing correctly with INVALID pages... */
1468 case ZEROED:
1469 kHlpMemSet(pbPage, 0, OBJPAGELEN);
1470 break;
1471
1472 case RANGE:
1473 KLDRMODLX_ASSERT(!"RANGE");
1474 default:
1475 rc = KLDR_ERR_LX_BAD_PAGE_MAP;
1476 break;
1477 }
1478 }
1479 if (rc)
1480 break;
1481
1482 /*
1483 * Zero the remaining pages.
1484 */
1485 if (iPage < cPages)
1486 kHlpMemSet(pbPage, 0, (cPages - iPage) * OBJPAGELEN);
1487 }
1488
1489 if (pbTmpPage)
1490 kHlpFree(pbTmpPage);
1491 return rc;
1492}
1493
1494
1495/**
1496 * Unpacks iterdata (aka EXEPACK).
1497 *
1498 * @returns 0 on success, non-zero kLdr status code on failure.
1499 * @param pbDst Where to put the uncompressed data. (Assumes OBJPAGELEN size.)
1500 * @param pbSrc The compressed source data.
1501 * @param cbSrc The file size of the compressed data. The source buffer
1502 * contains 4 additional zero bytes.
1503 */
1504static int kldrModLXDoIterDataUnpacking(KU8 *pbDst, const KU8 *pbSrc, int cbSrc)
1505{
1506 const struct LX_Iter *pIter = (const struct LX_Iter *)pbSrc;
1507 int cbDst = OBJPAGELEN;
1508
1509 /* Validate size of data. */
1510 if (cbSrc >= OBJPAGELEN - 2)
1511 return KLDR_ERR_LX_BAD_ITERDATA;
1512
1513 /*
1514 * Expand the page.
1515 */
1516 while (cbSrc > 0 && pIter->LX_nIter)
1517 {
1518 if (pIter->LX_nBytes == 1)
1519 {
1520 /*
1521 * Special case - one databyte.
1522 */
1523 cbDst -= pIter->LX_nIter;
1524 if (cbDst < 0)
1525 return KLDR_ERR_LX_BAD_ITERDATA;
1526
1527 cbSrc -= 4 + 1;
1528 if (cbSrc < -4)
1529 return KLDR_ERR_LX_BAD_ITERDATA;
1530
1531 kHlpMemSet(pbDst, pIter->LX_Iterdata, pIter->LX_nIter);
1532 pbDst += pIter->LX_nIter;
1533 pIter++;
1534 }
1535 else
1536 {
1537 /*
1538 * General.
1539 */
1540 int i;
1541
1542 cbDst -= pIter->LX_nIter * pIter->LX_nBytes;
1543 if (cbDst < 0)
1544 return KLDR_ERR_LX_BAD_ITERDATA;
1545
1546 cbSrc -= 4 + pIter->LX_nBytes;
1547 if (cbSrc < -4)
1548 return KLDR_ERR_LX_BAD_ITERDATA;
1549
1550 for (i = pIter->LX_nIter; i > 0; i--, pbDst += pIter->LX_nBytes)
1551 kHlpMemCopy(pbDst, &pIter->LX_Iterdata, pIter->LX_nBytes);
1552 pIter = (struct LX_Iter *)((char*)pIter + 4 + pIter->LX_nBytes);
1553 }
1554 }
1555
1556 /*
1557 * Zero remainder of the page.
1558 */
1559 if (cbDst > 0)
1560 kHlpMemSet(pbDst, 0, cbDst);
1561
1562 return 0;
1563}
1564
1565
1566/**
1567 * Unpacks iterdata (aka EXEPACK).
1568 *
1569 * @returns 0 on success, non-zero kLdr status code on failure.
1570 * @param pbDst Where to put the uncompressed data. (Assumes OBJPAGELEN size.)
1571 * @param pbSrc The compressed source data.
1572 * @param cbSrc The file size of the compressed data. The source buffer
1573 * contains 4 additional zero bytes.
1574 */
1575static int kldrModLXDoIterData2Unpacking(KU8 *pbDst, const KU8 *pbSrc, int cbSrc)
1576{
1577 int cbDst = OBJPAGELEN;
1578
1579 while (cbSrc > 0)
1580 {
1581 /*
1582 * Bit 0 and 1 is the encoding type.
1583 */
1584 switch (*pbSrc & 0x03)
1585 {
1586 /*
1587 *
1588 * 0 1 2 3 4 5 6 7
1589 * type | |
1590 * ----------------
1591 * cb <cb bytes of data>
1592 *
1593 * Bits 2-7 is, if not zero, the length of an uncompressed run
1594 * starting at the following byte.
1595 *
1596 * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
1597 * type | | | | | |
1598 * ---------------- ---------------------- -----------------------
1599 * zero cb char to multiply
1600 *
1601 * If the bits are zero, the following two bytes describes a 1 byte interation
1602 * run. First byte is count, second is the byte to copy. A count of zero is
1603 * means end of data, and we simply stops. In that case the rest of the data
1604 * should be zero.
1605 */
1606 case 0:
1607 {
1608 if (*pbSrc)
1609 {
1610 const int cb = *pbSrc >> 2;
1611 cbDst -= cb;
1612 if (cbDst < 0)
1613 return KLDR_ERR_LX_BAD_ITERDATA2;
1614 cbSrc -= cb + 1;
1615 if (cbSrc < 0)
1616 return KLDR_ERR_LX_BAD_ITERDATA2;
1617 kHlpMemCopy(pbDst, ++pbSrc, cb);
1618 pbDst += cb;
1619 pbSrc += cb;
1620 }
1621 else if (cbSrc < 2)
1622 return KLDR_ERR_LX_BAD_ITERDATA2;
1623 else
1624 {
1625 const int cb = pbSrc[1];
1626 if (!cb)
1627 goto l_endloop;
1628 cbDst -= cb;
1629 if (cbDst < 0)
1630 return KLDR_ERR_LX_BAD_ITERDATA2;
1631 cbSrc -= 3;
1632 if (cbSrc < 0)
1633 return KLDR_ERR_LX_BAD_ITERDATA2;
1634 kHlpMemSet(pbDst, pbSrc[2], cb);
1635 pbDst += cb;
1636 pbSrc += 3;
1637 }
1638 break;
1639 }
1640
1641
1642 /*
1643 * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1644 * type | | | | | |
1645 * ---- ------- -------------------------
1646 * cb1 cb2 - 3 offset <cb1 bytes of data>
1647 *
1648 * Two bytes layed out as described above, followed by cb1 bytes of data to be copied.
1649 * The cb2(+3) and offset describes an amount of data to be copied from the expanded
1650 * data relative to the current position. The data copied as you would expect it to be.
1651 */
1652 case 1:
1653 {
1654 cbSrc -= 2;
1655 if (cbSrc < 0)
1656 return KLDR_ERR_LX_BAD_ITERDATA2;
1657 else
1658 {
1659 const unsigned off = ((unsigned)pbSrc[1] << 1) | (*pbSrc >> 7);
1660 const int cb1 = (*pbSrc >> 2) & 3;
1661 const int cb2 = ((*pbSrc >> 4) & 7) + 3;
1662
1663 pbSrc += 2;
1664 cbSrc -= cb1;
1665 if (cbSrc < 0)
1666 return KLDR_ERR_LX_BAD_ITERDATA2;
1667 cbDst -= cb1;
1668 if (cbDst < 0)
1669 return KLDR_ERR_LX_BAD_ITERDATA2;
1670 kHlpMemCopy(pbDst, pbSrc, cb1);
1671 pbDst += cb1;
1672 pbSrc += cb1;
1673
1674 if (off > OBJPAGELEN - cbDst)
1675 return KLDR_ERR_LX_BAD_ITERDATA2;
1676 cbDst -= cb2;
1677 if (cbDst < 0)
1678 return KLDR_ERR_LX_BAD_ITERDATA2;
1679 kHlpMemMove(pbDst, pbDst - off, cb2);
1680 pbDst += cb2;
1681 }
1682 break;
1683 }
1684
1685
1686 /*
1687 * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1688 * type | | | |
1689 * ---- ----------------------------------
1690 * cb-3 offset
1691 *
1692 * Two bytes layed out as described above.
1693 * The cb(+3) and offset describes an amount of data to be copied from the expanded
1694 * data relative to the current position.
1695 *
1696 * If offset == 1 the data is not copied as expected, but in the memcpyw manner.
1697 */
1698 case 2:
1699 {
1700 cbSrc -= 2;
1701 if (cbSrc < 0)
1702 return KLDR_ERR_LX_BAD_ITERDATA2;
1703 else
1704 {
1705 const unsigned off = ((unsigned)pbSrc[1] << 4) | (*pbSrc >> 4);
1706 const int cb = ((*pbSrc >> 2) & 3) + 3;
1707
1708 pbSrc += 2;
1709 if (off > OBJPAGELEN - cbDst)
1710 return KLDR_ERR_LX_BAD_ITERDATA2;
1711 cbDst -= cb;
1712 if (cbDst < 0)
1713 return KLDR_ERR_LX_BAD_ITERDATA2;
1714 kLdrModLXMemCopyW(pbDst, pbDst - off, cb);
1715 pbDst += cb;
1716 }
1717 break;
1718 }
1719
1720
1721 /*
1722 * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
1723 * type | | | | | |
1724 * ---------- ---------------- ----------------------------------
1725 * cb1 cb2 offset <cb1 bytes of data>
1726 *
1727 * Three bytes layed out as described above, followed by cb1 bytes of data to be copied.
1728 * The cb2 and offset describes an amount of data to be copied from the expanded
1729 * data relative to the current position.
1730 *
1731 * If offset == 1 the data is not copied as expected, but in the memcpyw manner.
1732 */
1733 case 3:
1734 {
1735 cbSrc -= 3;
1736 if (cbSrc < 0)
1737 return KLDR_ERR_LX_BAD_ITERDATA2;
1738 else
1739 {
1740 const int cb1 = (*pbSrc >> 2) & 0xf;
1741 const int cb2 = ((pbSrc[1] & 0xf) << 2) | (*pbSrc >> 6);
1742 const unsigned off = ((unsigned)pbSrc[2] << 4) | (pbSrc[1] >> 4);
1743
1744 pbSrc += 3;
1745 cbSrc -= cb1;
1746 if (cbSrc < 0)
1747 return KLDR_ERR_LX_BAD_ITERDATA2;
1748 cbDst -= cb1;
1749 if (cbDst < 0)
1750 return KLDR_ERR_LX_BAD_ITERDATA2;
1751 kHlpMemCopy(pbDst, pbSrc, cb1);
1752 pbDst += cb1;
1753 pbSrc += cb1;
1754
1755 if (off > OBJPAGELEN - cbDst)
1756 return KLDR_ERR_LX_BAD_ITERDATA2;
1757 cbDst -= cb2;
1758 if (cbDst < 0)
1759 return KLDR_ERR_LX_BAD_ITERDATA2;
1760 kLdrModLXMemCopyW(pbDst, pbDst - off, cb2);
1761 pbDst += cb2;
1762 }
1763 break;
1764 }
1765 } /* type switch. */
1766 } /* unpack loop */
1767
1768l_endloop:
1769
1770
1771 /*
1772 * Zero remainder of the page.
1773 */
1774 if (cbDst > 0)
1775 kHlpMemSet(pbDst, 0, cbDst);
1776
1777 return 0;
1778}
1779
1780
1781/**
1782 * Special memcpy employed by the iterdata2 algorithm.
1783 *
1784 * Emulate a 16-bit memcpy (copying 16-bit at a time) and the effects this
1785 * has if src is very close to the destination.
1786 *
1787 * @param pbDst Destination pointer.
1788 * @param pbSrc Source pointer. Will always be <= pbDst.
1789 * @param cb Amount of data to be copied.
1790 * @remark This assumes that unaligned word and dword access is fine.
1791 */
1792static void kLdrModLXMemCopyW(KU8 *pbDst, const KU8 *pbSrc, int cb)
1793{
1794 switch (pbDst - pbSrc)
1795 {
1796 case 0:
1797 case 1:
1798 case 2:
1799 case 3:
1800 /* 16-bit copy (unaligned) */
1801 if (cb & 1)
1802 *pbDst++ = *pbSrc++;
1803 for (cb >>= 1; cb > 0; cb--, pbDst += 2, pbSrc += 2)
1804 *(KU16 *)pbDst = *(const KU16 *)pbSrc;
1805 break;
1806
1807 default:
1808 /* 32-bit copy (unaligned) */
1809 if (cb & 1)
1810 *pbDst++ = *pbSrc++;
1811 if (cb & 2)
1812 {
1813 *(KU16 *)pbDst = *(const KU16 *)pbSrc;
1814 pbDst += 2;
1815 pbSrc += 2;
1816 }
1817 for (cb >>= 2; cb > 0; cb--, pbDst += 4, pbSrc += 4)
1818 *(KU32 *)pbDst = *(const KU32 *)pbSrc;
1819 break;
1820 }
1821}
1822
1823
1824/**
1825 * Unprotects or protects the specified image mapping.
1826 *
1827 * @returns 0 on success.
1828 * @returns non-zero kLdr or OS status code on failure.
1829 *
1830 * @param pModLX The LX module interpreter instance.
1831 * @param pvBits The mapping to protect.
1832 * @param UnprotectOrProtect If 1 unprotect (i.e. make all writable), otherwise
1833 * protect according to the object table.
1834 */
1835static int kldrModLXDoProtect(PKLDRMODLX pModLX, void *pvBits, unsigned fUnprotectOrProtect)
1836{
1837 KU32 i;
1838 PKLDRMOD pMod = pModLX->pMod;
1839
1840 /*
1841 * Change object protection.
1842 */
1843 for (i = 0; i < pMod->cSegments; i++)
1844 {
1845 int rc;
1846 void *pv;
1847 KPROT enmProt;
1848
1849 /* calc new protection. */
1850 enmProt = pMod->aSegments[i].enmProt;
1851 if (fUnprotectOrProtect)
1852 {
1853 switch (enmProt)
1854 {
1855 case KPROT_NOACCESS:
1856 case KPROT_READONLY:
1857 case KPROT_READWRITE:
1858 case KPROT_WRITECOPY:
1859 enmProt = KPROT_READWRITE;
1860 break;
1861 case KPROT_EXECUTE:
1862 case KPROT_EXECUTE_READ:
1863 case KPROT_EXECUTE_READWRITE:
1864 case KPROT_EXECUTE_WRITECOPY:
1865 enmProt = KPROT_EXECUTE_READWRITE;
1866 break;
1867 default:
1868 KLDRMODLX_ASSERT(!"bad enmProt");
1869 return -1;
1870 }
1871 }
1872 else
1873 {
1874 /* copy on write -> normal write. */
1875 if (enmProt == KPROT_EXECUTE_WRITECOPY)
1876 enmProt = KPROT_EXECUTE_READWRITE;
1877 else if (enmProt == KPROT_WRITECOPY)
1878 enmProt = KPROT_READWRITE;
1879 }
1880
1881
1882 /* calc the address and set page protection. */
1883 pv = (KU8 *)pvBits + pMod->aSegments[i].RVA;
1884
1885 rc = kHlpPageProtect(pv, pMod->aSegments[i].cbMapped, enmProt);
1886 if (rc)
1887 break;
1888
1889 /** @todo the gap page should be marked NOACCESS! */
1890 }
1891
1892 return 0;
1893}
1894
1895
1896/** @copydoc kLdrModUnmap */
1897static int kldrModLXUnmap(PKLDRMOD pMod)
1898{
1899 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
1900 KU32 i;
1901 int rc;
1902
1903 /*
1904 * Mapped?
1905 */
1906 if (!pModLX->pvMapping)
1907 return KLDR_ERR_NOT_MAPPED;
1908
1909 /*
1910 * Free the mapping and update the segments.
1911 */
1912 rc = kHlpPageFree((void *)pModLX->pvMapping, pModLX->cbMapped);
1913 KLDRMODLX_ASSERT(!rc);
1914 pModLX->pvMapping = NULL;
1915
1916 for (i = 0; i < pMod->cSegments; i++)
1917 pMod->aSegments[i].MapAddress = 0;
1918
1919 return rc;
1920}
1921
1922
1923/** @copydoc kLdrModAllocTLS */
1924static int kldrModLXAllocTLS(PKLDRMOD pMod)
1925{
1926 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
1927
1928 /* no tls, just do the error checking. */
1929 if (!pModLX->pvMapping)
1930 return KLDR_ERR_NOT_MAPPED;
1931 return 0;
1932}
1933
1934
1935/** @copydoc kLdrModFreeTLS */
1936static void kldrModLXFreeTLS(PKLDRMOD pMod)
1937{
1938 /* no tls. */
1939}
1940
1941
1942/** @copydoc kLdrModReload */
1943static int kldrModLXReload(PKLDRMOD pMod)
1944{
1945 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
1946 int rc, rc2;
1947
1948 /*
1949 * Mapped?
1950 */
1951 if (!pModLX->pvMapping)
1952 return KLDR_ERR_NOT_MAPPED;
1953
1954 /*
1955 * Before doing anything we'll have to make all pages writable.
1956 */
1957 rc = kldrModLXDoProtect(pModLX, (void *)pModLX->pvMapping, 1 /* unprotect */);
1958 if (rc)
1959 return rc;
1960
1961 /*
1962 * Load the bits again.
1963 */
1964 rc = kldrModLXDoLoadBits(pModLX, (void *)pModLX->pvMapping);
1965
1966 /*
1967 * Restore protection.
1968 */
1969 rc2 = kldrModLXDoProtect(pModLX, (void *)pModLX->pvMapping, 0 /* protect */);
1970 if (!rc && rc2)
1971 rc = rc2;
1972 return rc;
1973}
1974
1975
1976/** @copydoc kLdrModFixupMapping */
1977static int kldrModLXFixupMapping(PKLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
1978{
1979 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
1980 int rc, rc2;
1981
1982 /*
1983 * Mapped?
1984 */
1985 if (!pModLX->pvMapping)
1986 return KLDR_ERR_NOT_MAPPED;
1987
1988 /*
1989 * Before doing anything we'll have to make all pages writable.
1990 */
1991 rc = kldrModLXDoProtect(pModLX, (void *)pModLX->pvMapping, 1 /* unprotect */);
1992 if (rc)
1993 return rc;
1994
1995 /*
1996 * Apply fixups and resolve imports.
1997 */
1998 rc = kldrModLXRelocateBits(pMod, (void *)pModLX->pvMapping, (KUPTR)pModLX->pvMapping,
1999 pMod->aSegments[0].LinkAddress, pfnGetImport, pvUser);
2000
2001 /*
2002 * Restore protection.
2003 */
2004 rc2 = kldrModLXDoProtect(pModLX, (void *)pModLX->pvMapping, 0 /* protect */);
2005 if (!rc && rc2)
2006 rc = rc2;
2007 return rc;
2008}
2009
2010
2011/** @copydoc kLdrModCallInit */
2012static int kldrModLXCallInit(PKLDRMOD pMod, KUPTR uHandle)
2013{
2014 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
2015 int rc;
2016
2017 /*
2018 * Mapped?
2019 */
2020 if (!pModLX->pvMapping)
2021 return KLDR_ERR_NOT_MAPPED;
2022
2023 /*
2024 * Do TLS callbacks first and then call the init/term function if it's a DLL.
2025 */
2026 if ((pModLX->Hdr.e32_mflags & E32MODMASK) == E32MODDLL)
2027 rc = kldrModLXDoCallDLL(pModLX, 0 /* attach */, uHandle);
2028 else
2029 rc = 0;
2030 return rc;
2031}
2032
2033
2034/**
2035 * Call the DLL entrypoint.
2036 *
2037 * @returns 0 on success.
2038 * @returns KLDR_ERR_MODULE_INIT_FAILED or KLDR_ERR_THREAD_ATTACH_FAILED on failure.
2039 * @param pModLX The LX module interpreter instance.
2040 * @param uOp The operation (DLL_*).
2041 * @param uHandle The module handle to present.
2042 */
2043static int kldrModLXDoCallDLL(PKLDRMODLX pModLX, unsigned uOp, KUPTR uHandle)
2044{
2045 int rc;
2046
2047 /*
2048 * If no entrypoint there isn't anything to be done.
2049 */
2050 if ( !pModLX->Hdr.e32_startobj
2051 || pModLX->Hdr.e32_startobj > pModLX->Hdr.e32_objcnt)
2052 return 0;
2053
2054 /*
2055 * Invoke the entrypoint and convert the boolean result to a kLdr status code.
2056 */
2057 rc = kldrModLXDoCall((KUPTR)pModLX->pvMapping
2058 + (KUPTR)pModLX->pMod->aSegments[pModLX->Hdr.e32_startobj - 1].RVA
2059 + pModLX->Hdr.e32_eip,
2060 uHandle, uOp, NULL);
2061 if (rc)
2062 rc = 0;
2063 else if (uOp == 0 /* attach */)
2064 rc = KLDR_ERR_MODULE_INIT_FAILED;
2065 else /* detach: ignore failures */
2066 rc = 0;
2067 return rc;
2068}
2069
2070
2071/**
2072 * Do a 3 parameter callback.
2073 *
2074 * @returns 32-bit callback return.
2075 * @param uEntrypoint The address of the function to be called.
2076 * @param uHandle The first argument, the module handle.
2077 * @param uOp The second argumnet, the reason we're calling.
2078 * @param pvReserved The third argument, reserved argument. (figure this one out)
2079 */
2080static KI32 kldrModLXDoCall(KUPTR uEntrypoint, KUPTR uHandle, KU32 uOp, void *pvReserved)
2081{
2082#if defined(__X86__) || defined(__i386__) || defined(_M_IX86)
2083 KI32 rc;
2084/** @todo try/except */
2085
2086 /*
2087 * Paranoia.
2088 */
2089# ifdef __GNUC__
2090 __asm__ __volatile__(
2091 "pushl %2\n\t"
2092 "pushl %1\n\t"
2093 "pushl %0\n\t"
2094 "lea 12(%%esp), %2\n\t"
2095 "call *%3\n\t"
2096 "movl %2, %%esp\n\t"
2097 : "=a" (rc)
2098 : "d" (uOp),
2099 "S" (0),
2100 "c" (uEntrypoint),
2101 "0" (uHandle));
2102# elif defined(_MSC_VER)
2103 __asm {
2104 mov eax, [uHandle]
2105 mov edx, [uOp]
2106 mov ecx, 0
2107 mov ebx, [uEntrypoint]
2108 push edi
2109 mov edi, esp
2110 push ecx
2111 push edx
2112 push eax
2113 call ebx
2114 mov esp, edi
2115 pop edi
2116 mov [rc], eax
2117 }
2118# else
2119# error "port me!"
2120# endif
2121 return rc;
2122
2123#else
2124 return KCPU_ERR_ARCH_CPU_NOT_COMPATIBLE;
2125#endif
2126}
2127
2128
2129/** @copydoc kLdrModCallTerm */
2130static int kldrModLXCallTerm(PKLDRMOD pMod, KUPTR uHandle)
2131{
2132 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
2133
2134 /*
2135 * Mapped?
2136 */
2137 if (!pModLX->pvMapping)
2138 return KLDR_ERR_NOT_MAPPED;
2139
2140 /*
2141 * Do the call.
2142 */
2143 if ((pModLX->Hdr.e32_mflags & E32MODMASK) == E32MODDLL)
2144 kldrModLXDoCallDLL(pModLX, 1 /* detach */, uHandle);
2145
2146 return 0;
2147}
2148
2149
2150/** @copydoc kLdrModCallThread */
2151static int kldrModLXCallThread(PKLDRMOD pMod, KUPTR uHandle, unsigned fAttachingOrDetaching)
2152{
2153 /* no thread attach/detach callout. */
2154 return 0;
2155}
2156
2157
2158/** @copydoc kLdrModSize */
2159static KLDRADDR kldrModLXSize(PKLDRMOD pMod)
2160{
2161 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
2162 return pModLX->cbMapped;
2163}
2164
2165
2166/** @copydoc kLdrModGetBits */
2167static int kldrModLXGetBits(PKLDRMOD pMod, void *pvBits, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
2168{
2169 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
2170 int rc;
2171
2172 /*
2173 * Load the image bits.
2174 */
2175 rc = kldrModLXDoLoadBits(pModLX, pvBits);
2176 if (rc)
2177 return rc;
2178
2179 /*
2180 * Perform relocations.
2181 */
2182 return kldrModLXRelocateBits(pMod, pvBits, BaseAddress, pMod->aSegments[0].LinkAddress, pfnGetImport, pvUser);
2183
2184}
2185
2186
2187/** @copydoc kLdrModRelocateBits */
2188static int kldrModLXRelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
2189 PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
2190{
2191 PKLDRMODLX pModLX = (PKLDRMODLX)pMod->pvData;
2192 KU32 iSeg;
2193 int rc;
2194
2195 /*
2196 * Do we need to to *anything*?
2197 */
2198 if ( NewBaseAddress == OldBaseAddress
2199 && NewBaseAddress == pModLX->paObjs[0].o32_base
2200 && !pModLX->Hdr.e32_impmodcnt)
2201 return 0;
2202
2203 /*
2204 * Load the fixup section.
2205 */
2206 if (!pModLX->pbFixupSection)
2207 {
2208 rc = kldrModLXDoLoadFixupSection(pModLX);
2209 if (rc)
2210 return rc;
2211 }
2212
2213 /*
2214 * Iterate the segments.
2215 */
2216 for (iSeg = 0; iSeg < pModLX->Hdr.e32_objcnt; iSeg++)
2217 {
2218 const struct o32_obj * const pObj = &pModLX->paObjs[iSeg];
2219 KLDRADDR PageAddress = NewBaseAddress + pModLX->pMod->aSegments[iSeg].RVA;
2220 KU32 iPage;
2221 KU8 *pbPage = (KU8 *)pvBits + (KUPTR)pModLX->pMod->aSegments[iSeg].RVA;
2222
2223 /*
2224 * Iterate the page map pages.
2225 */
2226 for (iPage = 0, rc = 0; !rc && iPage < pObj->o32_mapsize; iPage++, pbPage += OBJPAGELEN, PageAddress += OBJPAGELEN)
2227 {
2228 const KU8 * const pbFixupRecEnd = pModLX->pbFixupRecs + pModLX->paoffPageFixups[iPage + pObj->o32_pagemap];
2229 const KU8 *pb = pModLX->pbFixupRecs + pModLX->paoffPageFixups[iPage + pObj->o32_pagemap - 1];
2230 KLDRADDR uValue;
2231 int iSelector;
2232 KU32 fKind;
2233
2234 /* sanity */
2235 if (pbFixupRecEnd < pb)
2236 return KLDR_ERR_BAD_FIXUP;
2237 if (pbFixupRecEnd - 1 > pModLX->pbFixupSectionLast)
2238 return KLDR_ERR_BAD_FIXUP;
2239 if (pb < pModLX->pbFixupSection)
2240 return KLDR_ERR_BAD_FIXUP;
2241
2242 /*
2243 * Iterate the fixup record.
2244 */
2245 while (pb < pbFixupRecEnd)
2246 {
2247 union _rel
2248 {
2249 const KU8 * pb;
2250 const struct r32_rlc *prlc;
2251 } u;
2252
2253 u.pb = pb;
2254 pb += 3 + (u.prlc->nr_stype & NRCHAIN ? 0 : 1); /* place pch at the 4th member. */
2255
2256 /*
2257 * Figure out the target.
2258 */
2259 switch (u.prlc->nr_flags & NRRTYP)
2260 {
2261 /*
2262 * Internal fixup.
2263 */
2264 case NRRINT:
2265 {
2266 KU16 iTrgObject;
2267 KU32 offTrgObject;
2268
2269 /* the object */
2270 if (u.prlc->nr_flags & NR16OBJMOD)
2271 {
2272 iTrgObject = *(const KU16 *)pb;
2273 pb += 2;
2274 }
2275 else
2276 iTrgObject = *pb++;
2277 iTrgObject--;
2278 if (iTrgObject >= pModLX->Hdr.e32_objcnt)
2279 return KLDR_ERR_BAD_FIXUP;
2280
2281 /* the target */
2282 if ((u.prlc->nr_stype & NRSRCMASK) != NRSSEG)
2283 {
2284 if (u.prlc->nr_flags & NR32BITOFF)
2285 {
2286 offTrgObject = *(const KU32 *)pb;
2287 pb += 4;
2288 }
2289 else
2290 {
2291 offTrgObject = *(const KU16 *)pb;
2292 pb += 2;
2293 }
2294
2295 /* calculate the symbol info. */
2296 uValue = offTrgObject + NewBaseAddress + pMod->aSegments[iTrgObject].RVA;
2297 }
2298 else
2299 uValue = NewBaseAddress + pMod->aSegments[iTrgObject].RVA;
2300 if ( (u.prlc->nr_stype & NRALIAS)
2301 || (pMod->aSegments[iTrgObject].fFlags & KLDRSEG_FLAG_16BIT))
2302 iSelector = pMod->aSegments[iTrgObject].Sel16bit;
2303 else
2304 iSelector = pMod->aSegments[iTrgObject].SelFlat;
2305 fKind = 0;
2306 break;
2307 }
2308
2309 /*
2310 * Import by symbol ordinal.
2311 */
2312 case NRRORD:
2313 {
2314 KU16 iModule;
2315 KU32 iSymbol;
2316
2317 /* the module ordinal */
2318 if (u.prlc->nr_flags & NR16OBJMOD)
2319 {
2320 iModule = *(const KU16 *)pb;
2321 pb += 2;
2322 }
2323 else
2324 iModule = *pb++;
2325 iModule--;
2326 if (iModule >= pModLX->Hdr.e32_impmodcnt)
2327 return KLDR_ERR_BAD_FIXUP;
2328#if 1
2329 if (u.prlc->nr_flags & NRICHAIN)
2330 return KLDR_ERR_BAD_FIXUP;
2331#endif
2332
2333 /* . */
2334 if (u.prlc->nr_flags & NR32BITOFF)
2335 {
2336 iSymbol = *(const KU32 *)pb;
2337 pb += 4;
2338 }
2339 else if (!(u.prlc->nr_flags & NR8BITORD))
2340 {
2341 iSymbol = *(const KU16 *)pb;
2342 pb += 2;
2343 }
2344 else
2345 iSymbol = *pb++;
2346
2347 /* resolve it. */
2348 rc = pfnGetImport(pMod, iModule, iSymbol, NULL, 0, NULL, &uValue, &fKind, pvUser);
2349 if (rc)
2350 return rc;
2351 iSelector = -1;
2352 break;
2353 }
2354
2355 /*
2356 * Import by symbol name.
2357 */
2358 case NRRNAM:
2359 {
2360 KU32 iModule;
2361 KU16 offSymbol;
2362 const KU8 *pbSymbol;
2363
2364 /* the module ordinal */
2365 if (u.prlc->nr_flags & NR16OBJMOD)
2366 {
2367 iModule = *(const KU16 *)pb;
2368 pb += 2;
2369 }
2370 else
2371 iModule = *pb++;
2372 iModule--;
2373 if (iModule >= pModLX->Hdr.e32_impmodcnt)
2374 return KLDR_ERR_BAD_FIXUP;
2375#if 1
2376 if (u.prlc->nr_flags & NRICHAIN)
2377 return KLDR_ERR_BAD_FIXUP;
2378#endif
2379
2380 /* . */
2381 if (u.prlc->nr_flags & NR32BITOFF)
2382 {
2383 offSymbol = *(const KU32 *)pb;
2384 pb += 4;
2385 }
2386 else if (!(u.prlc->nr_flags & NR8BITORD))
2387 {
2388 offSymbol = *(const KU16 *)pb;
2389 pb += 2;
2390 }
2391 else
2392 offSymbol = *pb++;
2393 pbSymbol = pModLX->pbImportProcs + offSymbol;
2394 if ( pbSymbol < pModLX->pbImportProcs
2395 || pbSymbol > pModLX->pbFixupSectionLast)
2396 return KLDR_ERR_BAD_FIXUP;
2397
2398 /* resolve it. */
2399 rc = pfnGetImport(pMod, iModule, NIL_KLDRMOD_SYM_ORDINAL, (const char *)pbSymbol + 1, *pbSymbol, NULL,
2400 &uValue, &fKind, pvUser);
2401 if (rc)
2402 return rc;
2403 iSelector = -1;
2404 break;
2405 }
2406
2407 case NRRENT:
2408 KLDRMODLX_ASSERT(!"NRRENT");
2409 default:
2410 break;
2411 }
2412
2413 /* addend */
2414 if (u.prlc->nr_flags & NRADD)
2415 {
2416 if (u.prlc->nr_flags & NR32BITADD)
2417 {
2418 uValue += *(const KU32 *)pb;
2419 pb += 4;
2420 }
2421 else
2422 {
2423 uValue += *(const KU16 *)pb;
2424 pb += 2;
2425 }
2426 }
2427
2428
2429 /*
2430 * Deal with the 'source' (i.e. the place that should be modified - very logical).
2431 */
2432 if (!(u.prlc->nr_stype & NRCHAIN))
2433 {
2434 int off = u.prlc->r32_soff;
2435
2436 /* common / simple */
2437 if ( (u.prlc->nr_stype & NRSRCMASK) == NROFF32
2438 && off >= 0
2439 && off <= OBJPAGELEN - 4)
2440 *(KU32 *)&pbPage[off] = uValue;
2441 else if ( (u.prlc->nr_stype & NRSRCMASK) == NRSOFF32
2442 && off >= 0
2443 && off <= OBJPAGELEN - 4)
2444 *(KU32 *)&pbPage[off] = uValue - (PageAddress + off + 4);
2445 else
2446 {
2447 /* generic */
2448 rc = kldrModLXDoReloc(pbPage, off, PageAddress, u.prlc, iSelector, uValue, fKind);
2449 if (rc)
2450 return rc;
2451 }
2452 }
2453 else if (!(u.prlc->nr_flags & NRICHAIN))
2454 {
2455 const KI16 *poffSrc = (const KI16 *)pb;
2456 KU8 c = u.pb[2];
2457
2458 /* common / simple */
2459 if ((u.prlc->nr_stype & NRSRCMASK) == NROFF32)
2460 {
2461 while (c-- > 0)
2462 {
2463 int off = *poffSrc++;
2464 if (off >= 0 && off <= OBJPAGELEN - 4)
2465 *(KU32 *)&pbPage[off] = uValue;
2466 else
2467 {
2468 rc = kldrModLXDoReloc(pbPage, off, PageAddress, u.prlc, iSelector, uValue, fKind);
2469 if (rc)
2470 return rc;
2471 }
2472 }
2473 }
2474 else if ((u.prlc->nr_stype & NRSRCMASK) == NRSOFF32)
2475 {
2476 while (c-- > 0)
2477 {
2478 int off = *poffSrc++;
2479 if (off >= 0 && off <= OBJPAGELEN - 4)
2480 *(KU32 *)&pbPage[off] = uValue - (PageAddress + off + 4);
2481 else
2482 {
2483 rc = kldrModLXDoReloc(pbPage, off, PageAddress, u.prlc, iSelector, uValue, fKind);
2484 if (rc)
2485 return rc;
2486 }
2487 }
2488 }
2489 else
2490 {
2491 while (c-- > 0)
2492 {
2493 rc = kldrModLXDoReloc(pbPage, *poffSrc++, PageAddress, u.prlc, iSelector, uValue, fKind);
2494 if (rc)
2495 return rc;
2496 }
2497 }
2498 pb = (const KU8 *)poffSrc;
2499 }
2500 else
2501 {
2502 /* This is a pain because it will require virgin pages on a relocation. */
2503 KLDRMODLX_ASSERT(!"NRICHAIN");
2504 return KLDR_ERR_LX_NRICHAIN_NOT_SUPPORTED;
2505 }
2506 }
2507 }
2508 }
2509
2510 return 0;
2511}
2512
2513
2514/**
2515 * Applies the relocation to one 'source' in a page.
2516 *
2517 * This takes care of the more esotic case while the common cases
2518 * are dealt with seperately.
2519 *
2520 * @returns 0 on success, non-zero kLdr status code on failure.
2521 * @param pbPage The page in which to apply the fixup.
2522 * @param off Page relative offset of where to apply the offset.
2523 * @param uValue The target value.
2524 * @param fKind The target kind.
2525 */
2526static int kldrModLXDoReloc(KU8 *pbPage, int off, KLDRADDR PageAddress, const struct r32_rlc *prlc,
2527 int iSelector, KLDRADDR uValue, KU32 fKind)
2528{
2529#pragma pack(1) /* just to be sure */
2530 union
2531 {
2532 KU8 ab[6];
2533 KU32 off32;
2534 KU16 off16;
2535 KU8 off8;
2536 struct
2537 {
2538 KU16 off;
2539 KU16 Sel;
2540 } Far16;
2541 struct
2542 {
2543 KU32 off;
2544 KU16 Sel;
2545 } Far32;
2546 } uData;
2547#pragma pack()
2548 const KU8 *pbSrc;
2549 KU8 *pbDst;
2550 KU8 cb;
2551
2552 /*
2553 * Compose the fixup data.
2554 */
2555 switch (prlc->nr_stype & NRSRCMASK)
2556 {
2557 case NRSBYT:
2558 uData.off8 = (KU8)uValue;
2559 cb = 1;
2560 break;
2561 case NRSSEG:
2562 if (iSelector == -1)
2563 {
2564 /* fixme */
2565 }
2566 uData.off16 = iSelector;
2567 cb = 2;
2568 break;
2569 case NRSPTR:
2570 if (iSelector == -1)
2571 {
2572 /* fixme */
2573 }
2574 uData.Far16.off = (KU16)uValue;
2575 uData.Far16.Sel = iSelector;
2576 cb = 4;
2577 break;
2578 case NRSOFF:
2579 uData.off16 = (KU16)uValue;
2580 cb = 2;
2581 break;
2582 case NRPTR48:
2583 if (iSelector == -1)
2584 {
2585 /* fixme */
2586 }
2587 uData.Far32.off = (KU32)uValue;
2588 uData.Far32.Sel = iSelector;
2589 cb = 6;
2590 break;
2591 case NROFF32:
2592 uData.off32 = (KU32)uValue;
2593 cb = 4;
2594 break;
2595 case NRSOFF32:
2596 uData.off32 = (KU32)uValue - (PageAddress + off + 4);
2597 cb = 4;
2598 break;
2599 default:
2600 return KLDR_ERR_LX_BAD_FIXUP_SECTION; /** @todo fix error, add more checks! */
2601 }
2602
2603 /*
2604 * Apply it. This is sloooow...
2605 */
2606 pbSrc = &uData.ab[0];
2607 pbDst = pbPage + off;
2608 while (cb-- > 0)
2609 {
2610 if (off > OBJPAGELEN)
2611 break;
2612 if (off >= 0)
2613 *pbDst = *pbSrc;
2614 pbSrc++;
2615 pbDst++;
2616 }
2617
2618 return 0;
2619}
2620
2621
2622/**
2623 * The LX module interpreter method table.
2624 */
2625KLDRMODOPS g_kLdrModLXOps =
2626{
2627 "LX",
2628 NULL,
2629 kldrModLXCreate,
2630 kldrModLXDestroy,
2631 kldrModLXQuerySymbol,
2632 kldrModLXEnumSymbols,
2633 kldrModLXGetImport,
2634 kldrModLXNumberOfImports,
2635 NULL /* can execute one is optional */,
2636 kldrModLXGetStackInfo,
2637 kldrModLXQueryMainEntrypoint,
2638 NULL /* fixme */,
2639 NULL /* fixme */,
2640 kldrModLXEnumDbgInfo,
2641 kldrModLXHasDbgInfo,
2642 kldrModLXMap,
2643 kldrModLXUnmap,
2644 kldrModLXAllocTLS,
2645 kldrModLXFreeTLS,
2646 kldrModLXReload,
2647 kldrModLXFixupMapping,
2648 kldrModLXCallInit,
2649 kldrModLXCallTerm,
2650 kldrModLXCallThread,
2651 kldrModLXSize,
2652 kldrModLXGetBits,
2653 kldrModLXRelocateBits,
2654 NULL /* fixme: pfnMostlyDone */,
2655 42 /* the end */
2656};
2657
Note: See TracBrowser for help on using the repository browser.