VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/ldr/ldrLX.cpp@ 102284

Last change on this file since 102284 was 102284, checked in by vboxsync, 14 months ago

IPRT/ldr: Corrected segment flag conversion for LX images. bugref:10371

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 107.8 KB
Line 
1/* $Id: ldrLX.cpp 102284 2023-11-24 02:36:38Z vboxsync $ */
2/** @file
3 * kLdr - The Module Interpreter for the Linear eXecutable (LX) Format.
4 */
5
6/*
7 * Copyright (C) 2007-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 * --------------------------------------------------------------------
36 *
37 * This code is based on: kLdr/kLdrModLX.c from kStuff r113.
38 *
39 * Copyright (c) 2006-2007 Knut St. Osmundsen <[email protected]>
40 *
41 * Permission is hereby granted, free of charge, to any person
42 * obtaining a copy of this software and associated documentation
43 * files (the "Software"), to deal in the Software without
44 * restriction, including without limitation the rights to use,
45 * copy, modify, merge, publish, distribute, sublicense, and/or sell
46 * copies of the Software, and to permit persons to whom the
47 * Software is furnished to do so, subject to the following
48 * conditions:
49 *
50 * The above copyright notice and this permission notice shall be
51 * included in all copies or substantial portions of the Software.
52 *
53 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
54 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
55 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
56 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
57 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
58 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
59 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
60 * OTHER DEALINGS IN THE SOFTWARE.
61 */
62
63
64/*********************************************************************************************************************************
65* Header Files *
66*********************************************************************************************************************************/
67#define LOG_GROUP RTLOGGROUP_LDR
68#include <iprt/ldr.h>
69#include "internal/iprt.h"
70
71#include <iprt/asm.h>
72#include <iprt/assert.h>
73#include <iprt/err.h>
74#include <iprt/log.h>
75#include <iprt/mem.h>
76#include <iprt/string.h>
77
78#include <iprt/formats/lx.h>
79#include <iprt/formats/pecoff.h>
80#include <iprt/formats/codeview.h>
81#include <iprt/formats/elf32.h>
82#include "internal/ldr.h"
83
84
85/*********************************************************************************************************************************
86* Defined Constants And Macros *
87*********************************************************************************************************************************/
88/** @def KLDRMODLX_STRICT
89 * Define KLDRMODLX_STRICT to enabled strict checks in KLDRMODLX. */
90#define KLDRMODLX_STRICT 1
91
92/** @def KLDRMODLX_ASSERT
93 * Assert that an expression is true when KLDR_STRICT is defined.
94 */
95#ifdef KLDRMODLX_STRICT
96# define KLDRMODLX_ASSERT(expr) Assert(expr)
97#else
98# define KLDRMODLX_ASSERT(expr) do {} while (0)
99#endif
100
101
102/*********************************************************************************************************************************
103* Structures and Typedefs *
104*********************************************************************************************************************************/
105/**
106 * Instance data for the LX module interpreter.
107 */
108typedef struct KLDRMODLX
109{
110 /** Core module structure. */
111 RTLDRMODINTERNAL Core;
112
113 /** Pointer to the user mapping. */
114 const void *pvMapping;
115 /** The size of the mapped LX image. */
116 size_t cbMapped;
117 /** Reserved flags. */
118 uint32_t f32Reserved;
119
120 /** The offset of the LX header. */
121 RTFOFF offHdr;
122 /** Copy of the LX header. */
123 struct e32_exe Hdr;
124
125 /** Pointer to the loader section.
126 * Allocated together with this strcture. */
127 const uint8_t *pbLoaderSection;
128 /** Pointer to the last byte in the loader section. */
129 const uint8_t *pbLoaderSectionLast;
130 /** Pointer to the object table in the loader section. */
131 const struct o32_obj *paObjs;
132 /** Pointer to the object page map table in the loader section. */
133 const struct o32_map *paPageMappings;
134 /** Pointer to the resource table in the loader section. */
135 const struct rsrc32 *paRsrcs;
136 /** Pointer to the resident name table in the loader section. */
137 const uint8_t *pbResNameTab;
138 /** Pointer to the entry table in the loader section. */
139 const uint8_t *pbEntryTab;
140
141 /** Pointer to the non-resident name table. */
142 uint8_t *pbNonResNameTab;
143 /** Pointer to the last byte in the non-resident name table. */
144 const uint8_t *pbNonResNameTabLast;
145
146 /** Pointer to the fixup section. */
147 uint8_t *pbFixupSection;
148 /** Pointer to the last byte in the fixup section. */
149 const uint8_t *pbFixupSectionLast;
150 /** Pointer to the fixup page table within pvFixupSection. */
151 const uint32_t *paoffPageFixups;
152 /** Pointer to the fixup record table within pvFixupSection. */
153 const uint8_t *pbFixupRecs;
154 /** Pointer to the import module name table within pvFixupSection. */
155 const uint8_t *pbImportMods;
156 /** Pointer to the import module name table within pvFixupSection. */
157 const uint8_t *pbImportProcs;
158
159 /** Pointer to the module name (in the resident name table). */
160 const char *pszName;
161 /** The name length. */
162 size_t cchName;
163
164 /** The target CPU. */
165 RTLDRCPU enmCpu;
166 /** Number of segments in aSegments. */
167 uint32_t cSegments;
168 /** Segment info. */
169 RT_FLEXIBLE_ARRAY_EXTENSION
170 RTLDRSEG aSegments[RT_FLEXIBLE_ARRAY];
171} KLDRMODLX, *PKLDRMODLX;
172
173
174/*********************************************************************************************************************************
175* Internal Functions *
176*********************************************************************************************************************************/
177static int kldrModLXHasDbgInfo(PRTLDRMODINTERNAL pMod, const void *pvBits);
178static DECLCALLBACK(int) rtldrLX_RelocateBits(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR NewBaseAddress,
179 RTUINTPTR OldBaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser);
180static const uint8_t *kldrModLXDoNameTableLookupByOrdinal(const uint8_t *pbNameTable, ssize_t cbNameTable, uint32_t iOrdinal);
181static int kldrModLXDoNameLookup(PKLDRMODLX pModLX, const char *pchSymbol, size_t cchSymbol, uint32_t *piSymbol);
182static const uint8_t *kldrModLXDoNameTableLookupByName(const uint8_t *pbNameTable, ssize_t cbNameTable,
183 const char *pchSymbol, size_t cchSymbol);
184static int kldrModLXGetImport(PKLDRMODLX pThis, const void *pvBits, uint32_t iImport,
185 char *pszName, size_t cchName, size_t *pcbNeeded);
186static int kldrModLXDoLoadBits(PKLDRMODLX pModLX, void *pvBits);
187static int kldrModLXDoIterDataUnpacking(uint8_t *pbDst, const uint8_t *pbSrc, int cbSrc);
188static int kldrModLXDoIterData2Unpacking(uint8_t *pbDst, const uint8_t *pbSrc, int cbSrc);
189static void kLdrModLXMemCopyW(uint8_t *pbDst, const uint8_t *pbSrc, int cb);
190static int kldrModLXDoForwarderQuery(PKLDRMODLX pModLX, const struct e32_entry *pEntry,
191 PFNRTLDRIMPORT pfnGetForwarder, void *pvUser, PRTLDRADDR puValue, uint32_t *pfKind);
192#if 0
193static int kldrModLXDoProtect(PKLDRMODLX pModLX, void *pvBits, unsigned fUnprotectOrProtect);
194static int kldrModLXDoCallDLL(PKLDRMODLX pModLX, void *pvMapping, unsigned uOp, uintptr_t uHandle);
195static int32_t kldrModLXDoCall(uintptr_t uEntrypoint, uintptr_t uHandle, uint32_t uOp, void *pvReserved);
196#endif
197static int kldrModLXDoLoadFixupSection(PKLDRMODLX pModLX);
198static int kldrModLXDoReloc(uint8_t *pbPage, int off, RTLDRADDR PageAddress, const struct r32_rlc *prlc,
199 int iSelector, RTLDRADDR uValue, uint32_t fKind);
200
201
202/**
203 * Separate function for reading creating the LX module instance to
204 * simplify cleanup on failure.
205 */
206static int kldrModLXDoCreate(PRTLDRREADER pRdr, RTFOFF offNewHdr, uint32_t fFlags, PKLDRMODLX *ppModLX, PRTERRINFO pErrInfo)
207{
208 struct e32_exe Hdr;
209 PKLDRMODLX pModLX;
210 uint32_t off, offEnd;
211 uint32_t i;
212 int fCanOptimizeMapping;
213 uint32_t NextRVA;
214
215 RT_NOREF(fFlags);
216 *ppModLX = NULL;
217
218 /*
219 * Read the signature and file header.
220 */
221 int rc = pRdr->pfnRead(pRdr, &Hdr, sizeof(Hdr), offNewHdr > 0 ? offNewHdr : 0);
222 if (RT_FAILURE(rc))
223 return RTErrInfoSetF(pErrInfo, rc, "Error reading LX header at %RTfoff: %Rrc", offNewHdr, rc);
224 if ( Hdr.e32_magic[0] != E32MAGIC1
225 || Hdr.e32_magic[1] != E32MAGIC2)
226 return RTErrInfoSetF(pErrInfo, VERR_INVALID_EXE_SIGNATURE, "Not LX magic: %02x %02x", Hdr.e32_magic[0], Hdr.e32_magic[1]);
227
228 /* We're not interested in anything but x86 images. */
229 if ( Hdr.e32_level != E32LEVEL
230 || Hdr.e32_border != E32LEBO
231 || Hdr.e32_worder != E32LEWO
232 || Hdr.e32_cpu < E32CPU286
233 || Hdr.e32_cpu > E32CPU486
234 || Hdr.e32_pagesize != OBJPAGELEN
235 )
236 return VERR_LDRLX_BAD_HEADER;
237
238 /* Some rough sanity checks. */
239 offEnd = pRdr->pfnSize(pRdr) >= (uint64_t)~(uint32_t)16 ? ~(uint32_t)16 : (uint32_t)pRdr->pfnSize(pRdr);
240 if ( Hdr.e32_itermap > offEnd
241 || Hdr.e32_datapage > offEnd
242 || Hdr.e32_nrestab > offEnd
243 || Hdr.e32_nrestab + Hdr.e32_cbnrestab > offEnd
244 || Hdr.e32_ldrsize > offEnd - offNewHdr - sizeof(Hdr)
245 || Hdr.e32_fixupsize > offEnd - offNewHdr - sizeof(Hdr)
246 || Hdr.e32_fixupsize + Hdr.e32_ldrsize > offEnd - offNewHdr - sizeof(Hdr))
247 return VERR_LDRLX_BAD_HEADER;
248
249 /* Verify the loader section. */
250 offEnd = Hdr.e32_objtab + Hdr.e32_ldrsize;
251 if (Hdr.e32_objtab < sizeof(Hdr) && Hdr.e32_objcnt)
252 return RTErrInfoSetF(pErrInfo, VERR_LDRLX_BAD_LOADER_SECTION,
253 "Object table is inside the header: %#x", Hdr.e32_objtab);
254 off = Hdr.e32_objtab + sizeof(struct o32_obj) * Hdr.e32_objcnt;
255 if (off > offEnd)
256 return RTErrInfoSetF(pErrInfo, VERR_LDRLX_BAD_LOADER_SECTION,
257 "Object table spans beyond the executable: e32_objcnt=%u", Hdr.e32_objcnt);
258 if (Hdr.e32_objcnt >= _32K)
259 return RTErrInfoSetF(pErrInfo, VERR_LDRLX_BAD_LOADER_SECTION, "Too many segments: %#x\n", Hdr.e32_objcnt);
260 if ( Hdr.e32_objmap
261 && (Hdr.e32_objmap < off || Hdr.e32_objmap > offEnd))
262 return RTErrInfoSetF(pErrInfo, VERR_LDRLX_BAD_LOADER_SECTION,
263 "Bad object page map table offset: %#x", Hdr.e32_objmap);
264 if ( Hdr.e32_rsrccnt
265 && ( Hdr.e32_rsrctab < off
266 || Hdr.e32_rsrctab > offEnd
267 || Hdr.e32_rsrctab + sizeof(struct rsrc32) * Hdr.e32_rsrccnt > offEnd))
268 return RTErrInfoSetF(pErrInfo, VERR_LDRLX_BAD_LOADER_SECTION,
269 "Resource table is out of bounds: %#x entries at %#x", Hdr.e32_rsrccnt, Hdr.e32_rsrctab);
270 if ( Hdr.e32_restab
271 && (Hdr.e32_restab < off || Hdr.e32_restab > offEnd - 2))
272 return VERR_LDRLX_BAD_LOADER_SECTION;
273 if ( Hdr.e32_enttab
274 && (Hdr.e32_enttab < off || Hdr.e32_enttab >= offEnd))
275 return VERR_LDRLX_BAD_LOADER_SECTION;
276 if ( Hdr.e32_dircnt
277 && (Hdr.e32_dirtab < off || Hdr.e32_dirtab > offEnd - 2))
278 return VERR_LDRLX_BAD_LOADER_SECTION;
279
280 /* Verify the fixup section. */
281 off = offEnd;
282 offEnd = off + Hdr.e32_fixupsize;
283 if ( Hdr.e32_fpagetab
284 && (Hdr.e32_fpagetab < off || Hdr.e32_fpagetab > offEnd))
285 {
286 /*
287 * wlink mixes the fixup section and the loader section.
288 */
289 off = Hdr.e32_fpagetab;
290 offEnd = off + Hdr.e32_fixupsize;
291 Hdr.e32_ldrsize = off - Hdr.e32_objtab;
292 }
293 if ( Hdr.e32_frectab
294 && (Hdr.e32_frectab < off || Hdr.e32_frectab > offEnd))
295 return VERR_LDRLX_BAD_FIXUP_SECTION;
296 if ( Hdr.e32_impmod
297 && (Hdr.e32_impmod < off || Hdr.e32_impmod > offEnd || Hdr.e32_impmod + Hdr.e32_impmodcnt > offEnd))
298 return VERR_LDRLX_BAD_FIXUP_SECTION;
299 if ( Hdr.e32_impproc
300 && (Hdr.e32_impproc < off || Hdr.e32_impproc > offEnd))
301 return VERR_LDRLX_BAD_FIXUP_SECTION;
302
303 /*
304 * Calc the instance size, allocate and initialize it.
305 */
306 size_t cbModLXAndSegments = RT_ALIGN_Z(RT_UOFFSETOF_DYN(KLDRMODLX, aSegments[Hdr.e32_objcnt + 1]), 8);
307 cbModLXAndSegments += sizeof("segXXXXX") * (Hdr.e32_objcnt + 1);
308
309 pModLX = (PKLDRMODLX)RTMemAlloc(cbModLXAndSegments + Hdr.e32_ldrsize + 2 /*for two extra zeros*/);
310 if (!pModLX)
311 return VERR_NO_MEMORY;
312 *ppModLX = pModLX;
313
314 /* Core & CPU. */
315 pModLX->Core.u32Magic = 0; /* set by caller. */
316 pModLX->Core.eState = LDR_STATE_OPENED;
317 pModLX->Core.pOps = NULL; /* set by caller. */
318 pModLX->Core.pReader = pRdr;
319 switch (Hdr.e32_cpu)
320 {
321 case E32CPU286:
322 pModLX->enmCpu = RTLDRCPU_I80286;
323 pModLX->Core.enmArch = RTLDRARCH_X86_16;
324 break;
325 case E32CPU386:
326 pModLX->enmCpu = RTLDRCPU_I386;
327 pModLX->Core.enmArch = RTLDRARCH_X86_32;
328 break;
329 case E32CPU486:
330 pModLX->enmCpu = RTLDRCPU_I486;
331 pModLX->Core.enmArch = RTLDRARCH_X86_32;
332 break;
333 }
334 pModLX->Core.enmEndian = RTLDRENDIAN_LITTLE;
335 pModLX->Core.enmFormat = RTLDRFMT_LX;
336 switch (Hdr.e32_mflags & E32MODMASK)
337 {
338 case E32MODEXE:
339 pModLX->Core.enmType = !(Hdr.e32_mflags & E32NOINTFIX)
340 ? RTLDRTYPE_EXECUTABLE_RELOCATABLE
341 : RTLDRTYPE_EXECUTABLE_FIXED;
342 break;
343
344 case E32MODDLL:
345 case E32PROTDLL:
346 case E32MODPROTDLL:
347 pModLX->Core.enmType = !(Hdr.e32_mflags & E32SYSDLL)
348 ? RTLDRTYPE_SHARED_LIBRARY_RELOCATABLE
349 : RTLDRTYPE_SHARED_LIBRARY_FIXED;
350 break;
351
352 case E32MODPDEV:
353 case E32MODVDEV:
354 pModLX->Core.enmType = RTLDRTYPE_SHARED_LIBRARY_RELOCATABLE;
355 break;
356 }
357
358 /* KLDRMODLX */
359 pModLX->cSegments = Hdr.e32_objcnt;
360 pModLX->pszName = NULL; /* finalized further down */
361 pModLX->cchName = 0;
362 pModLX->pvMapping = 0;
363 pModLX->cbMapped = 0;
364 pModLX->f32Reserved = 0;
365
366 pModLX->offHdr = offNewHdr >= 0 ? offNewHdr : 0;
367 memcpy(&pModLX->Hdr, &Hdr, sizeof(Hdr));
368
369 pModLX->pbLoaderSection = (uint8_t *)pModLX + cbModLXAndSegments;
370 pModLX->pbLoaderSectionLast = pModLX->pbLoaderSection + pModLX->Hdr.e32_ldrsize - 1;
371 pModLX->paObjs = NULL;
372 pModLX->paPageMappings = NULL;
373 pModLX->paRsrcs = NULL;
374 pModLX->pbResNameTab = NULL;
375 pModLX->pbEntryTab = NULL;
376
377 pModLX->pbNonResNameTab = NULL;
378 pModLX->pbNonResNameTabLast = NULL;
379
380 pModLX->pbFixupSection = NULL;
381 pModLX->pbFixupSectionLast = NULL;
382 pModLX->paoffPageFixups = NULL;
383 pModLX->pbFixupRecs = NULL;
384 pModLX->pbImportMods = NULL;
385 pModLX->pbImportProcs = NULL;
386
387 /*
388 * Read the loader data.
389 */
390 rc = pRdr->pfnRead(pRdr, (void *)pModLX->pbLoaderSection, pModLX->Hdr.e32_ldrsize, pModLX->Hdr.e32_objtab + pModLX->offHdr);
391 if (RT_FAILURE(rc))
392 return rc;
393 ((uint8_t *)pModLX->pbLoaderSectionLast)[1] = 0;
394 ((uint8_t *)pModLX->pbLoaderSectionLast)[2] = 0;
395 if (pModLX->Hdr.e32_objcnt)
396 pModLX->paObjs = (const struct o32_obj *)pModLX->pbLoaderSection;
397 if (pModLX->Hdr.e32_objmap)
398 pModLX->paPageMappings = (const struct o32_map *)(pModLX->pbLoaderSection + pModLX->Hdr.e32_objmap - pModLX->Hdr.e32_objtab);
399 if (pModLX->Hdr.e32_rsrccnt)
400 pModLX->paRsrcs = (const struct rsrc32 *)(pModLX->pbLoaderSection + pModLX->Hdr.e32_rsrctab - pModLX->Hdr.e32_objtab);
401 if (pModLX->Hdr.e32_restab)
402 pModLX->pbResNameTab = pModLX->pbLoaderSection + pModLX->Hdr.e32_restab - pModLX->Hdr.e32_objtab;
403 if (pModLX->Hdr.e32_enttab)
404 pModLX->pbEntryTab = pModLX->pbLoaderSection + pModLX->Hdr.e32_enttab - pModLX->Hdr.e32_objtab;
405
406 /*
407 * Get the soname from the resident name table.
408 * Very convenient that it's the 0 ordinal, because then we get a
409 * free string terminator.
410 * (The table entry consists of a pascal string followed by a 16-bit ordinal.)
411 */
412 if (pModLX->pbResNameTab)
413 pModLX->pszName = (const char *)kldrModLXDoNameTableLookupByOrdinal(pModLX->pbResNameTab,
414 pModLX->pbLoaderSectionLast - pModLX->pbResNameTab + 1,
415 0);
416 if (!pModLX->pszName)
417 return VERR_LDRLX_NO_SONAME;
418 pModLX->cchName = *(const uint8_t *)pModLX->pszName++;
419 if ( pModLX->pszName[pModLX->cchName] != '\0'
420 || pModLX->cchName != strlen(pModLX->pszName))
421 return VERR_LDRLX_BAD_SONAME;
422
423 /*
424 * Quick validation of the object table.
425 */
426 for (i = 0; i < pModLX->cSegments; i++)
427 {
428 if (pModLX->paObjs[i].o32_base & (OBJPAGELEN - 1))
429 return VERR_LDRLX_BAD_OBJECT_TABLE;
430 if (pModLX->paObjs[i].o32_base + pModLX->paObjs[i].o32_size <= pModLX->paObjs[i].o32_base)
431 return VERR_LDRLX_BAD_OBJECT_TABLE;
432 if (pModLX->paObjs[i].o32_mapsize > (pModLX->paObjs[i].o32_size + (OBJPAGELEN - 1)))
433 return VERR_LDRLX_BAD_OBJECT_TABLE;
434 if ( pModLX->paObjs[i].o32_mapsize
435 && ( (uint8_t *)&pModLX->paPageMappings[pModLX->paObjs[i].o32_pagemap] > pModLX->pbLoaderSectionLast
436 || (uint8_t *)&pModLX->paPageMappings[pModLX->paObjs[i].o32_pagemap + pModLX->paObjs[i].o32_mapsize]
437 > pModLX->pbLoaderSectionLast))
438 return VERR_LDRLX_BAD_OBJECT_TABLE;
439 if (i > 0 && !(pModLX->paObjs[i].o32_flags & OBJRSRC))
440 {
441 if (pModLX->paObjs[i].o32_base <= pModLX->paObjs[i - 1].o32_base)
442 return VERR_LDRLX_BAD_OBJECT_TABLE;
443 if (pModLX->paObjs[i].o32_base < pModLX->paObjs[i - 1].o32_base + pModLX->paObjs[i - 1].o32_mapsize)
444 return VERR_LDRLX_BAD_OBJECT_TABLE;
445 }
446 }
447
448 /*
449 * Check if we can optimize the mapping by using a different
450 * object alignment. The linker typically uses 64KB alignment,
451 * we can easily get away with page alignment in most cases.
452 *
453 * However, this screws up DwARF debug info, let's not do this
454 * when the purpose is reading debug info.
455 */
456 /** @todo Add flag for enabling this optimization. */
457 fCanOptimizeMapping = !(Hdr.e32_mflags & (E32NOINTFIX | E32SYSDLL))
458 && !(fFlags & (RTLDR_O_FOR_DEBUG | RTLDR_O_FOR_VALIDATION));
459 NextRVA = 0;
460
461 /*
462 * Setup the KLDRMOD segment array.
463 */
464 char *pszSegNm = (char *)&pModLX->aSegments[pModLX->cSegments];
465 for (i = 0; i < pModLX->cSegments; i++)
466 {
467 /* dummy segment name */
468 pModLX->aSegments[i].pszName = pszSegNm;
469 size_t cchName = RTStrPrintf(pszSegNm, sizeof("segXXXXX"), "seg%u", i);
470 pszSegNm += cchName + 1;
471 pModLX->aSegments[i].cchName = (uint32_t)cchName;
472
473 /* unused */
474 pModLX->aSegments[i].offFile = -1;
475 pModLX->aSegments[i].cbFile = -1;
476 pModLX->aSegments[i].SelFlat = 0;
477 pModLX->aSegments[i].Sel16bit = 0;
478
479 /* flags */
480 pModLX->aSegments[i].fFlags = 0;
481 if (!(pModLX->paObjs[i].o32_flags & OBJBIGDEF))
482 pModLX->aSegments[i].fFlags |= RTLDRSEG_FLAG_16BIT;
483 if (pModLX->paObjs[i].o32_flags & OBJALIAS16)
484 pModLX->aSegments[i].fFlags |= RTLDRSEG_FLAG_OS2_ALIAS16;
485 if (pModLX->paObjs[i].o32_flags & OBJCONFORM)
486 pModLX->aSegments[i].fFlags |= RTLDRSEG_FLAG_OS2_CONFORM;
487 if (pModLX->paObjs[i].o32_flags & OBJIOPL)
488 pModLX->aSegments[i].fFlags |= RTLDRSEG_FLAG_OS2_IOPL;
489
490 /* size and addresses */
491 pModLX->aSegments[i].Alignment = OBJPAGELEN;
492 pModLX->aSegments[i].cb = pModLX->paObjs[i].o32_size;
493 pModLX->aSegments[i].LinkAddress = pModLX->paObjs[i].o32_base;
494 pModLX->aSegments[i].RVA = NextRVA;
495 if ( fCanOptimizeMapping
496 || i + 1 >= pModLX->cSegments
497 || (pModLX->paObjs[i].o32_flags & OBJRSRC)
498 || (pModLX->paObjs[i + 1].o32_flags & OBJRSRC))
499 pModLX->aSegments[i].cbMapped = RT_ALIGN_Z(pModLX->paObjs[i].o32_size, OBJPAGELEN);
500 else
501 pModLX->aSegments[i].cbMapped = pModLX->paObjs[i + 1].o32_base - pModLX->paObjs[i].o32_base;
502 /** @todo Above probably doesn't work for os2krnl and other images
503 * non-sequential virtual address assignments. */
504 NextRVA += (uint32_t)pModLX->aSegments[i].cbMapped;
505
506 /* protection */
507 switch ( pModLX->paObjs[i].o32_flags
508 & (OBJSHARED | OBJREAD | OBJWRITE | OBJEXEC))
509 {
510 case 0:
511 case OBJSHARED:
512 pModLX->aSegments[i].fProt = 0;
513 break;
514 case OBJREAD:
515 case OBJREAD | OBJSHARED:
516 pModLX->aSegments[i].fProt = RTMEM_PROT_READ;
517 break;
518 case OBJWRITE:
519 case OBJWRITE | OBJREAD:
520 pModLX->aSegments[i].fProt = RTMEM_PROT_READ | RTMEM_PROT_WRITECOPY;
521 break;
522 case OBJWRITE | OBJSHARED:
523 case OBJWRITE | OBJSHARED | OBJREAD:
524 pModLX->aSegments[i].fProt = RTMEM_PROT_READ | RTMEM_PROT_WRITE;
525 break;
526 case OBJEXEC:
527 case OBJEXEC | OBJSHARED:
528 pModLX->aSegments[i].fProt = RTMEM_PROT_EXEC;
529 break;
530 case OBJEXEC | OBJREAD:
531 case OBJEXEC | OBJREAD | OBJSHARED:
532 pModLX->aSegments[i].fProt = RTMEM_PROT_EXEC | RTMEM_PROT_READ;
533 break;
534 case OBJEXEC | OBJWRITE:
535 case OBJEXEC | OBJWRITE | OBJREAD:
536 pModLX->aSegments[i].fProt = RTMEM_PROT_EXEC | RTMEM_PROT_READ | RTMEM_PROT_WRITECOPY;
537 break;
538 case OBJEXEC | OBJWRITE | OBJSHARED:
539 case OBJEXEC | OBJWRITE | OBJSHARED | OBJREAD:
540 pModLX->aSegments[i].fProt = RTMEM_PROT_EXEC | RTMEM_PROT_READ | RTMEM_PROT_WRITE;
541 break;
542 }
543 if ((pModLX->paObjs[i].o32_flags & (OBJREAD | OBJWRITE | OBJEXEC | OBJRSRC)) == OBJRSRC)
544 pModLX->aSegments[i].fProt = RTMEM_PROT_READ;
545 /*pModLX->aSegments[i].f16bit = !(pModLX->paObjs[i].o32_flags & OBJBIGDEF)
546 pModLX->aSegments[i].fIOPL = !(pModLX->paObjs[i].o32_flags & OBJIOPL)
547 pModLX->aSegments[i].fConforming = !(pModLX->paObjs[i].o32_flags & OBJCONFORM) */
548 }
549
550 /* set the mapping size */
551 pModLX->cbMapped = NextRVA;
552
553 /*
554 * We're done.
555 */
556 *ppModLX = pModLX;
557 return VINF_SUCCESS;
558}
559
560
561/**
562 * @interface_method_impl{RTLDROPS,pfnClose}
563 */
564static DECLCALLBACK(int) rtldrLX_Close(PRTLDRMODINTERNAL pMod)
565{
566 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
567 KLDRMODLX_ASSERT(!pModLX->pvMapping);
568
569 if (pModLX->pbNonResNameTab)
570 {
571 RTMemFree(pModLX->pbNonResNameTab);
572 pModLX->pbNonResNameTab = NULL;
573 }
574 if (pModLX->pbFixupSection)
575 {
576 RTMemFree(pModLX->pbFixupSection);
577 pModLX->pbFixupSection = NULL;
578 }
579 return VINF_SUCCESS;
580}
581
582
583/**
584 * Resolved base address aliases.
585 *
586 * @param pModLX The interpreter module instance
587 * @param pBaseAddress The base address, IN & OUT.
588 */
589static void kldrModLXResolveBaseAddress(PKLDRMODLX pModLX, PRTLDRADDR pBaseAddress)
590{
591 if (*pBaseAddress == RTLDR_BASEADDRESS_LINK)
592 *pBaseAddress = pModLX->aSegments[0].LinkAddress;
593}
594
595
596static int kldrModLXQuerySymbol(PRTLDRMODINTERNAL pMod, const void *pvBits, RTLDRADDR BaseAddress, uint32_t iSymbol,
597 const char *pchSymbol, size_t cchSymbol, const char *pszVersion,
598 PFNRTLDRIMPORT pfnGetForwarder, void *pvUser, PRTLDRADDR puValue, uint32_t *pfKind)
599{
600 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
601 uint32_t iOrdinal;
602 int rc;
603 const struct b32_bundle *pBundle;
604 RT_NOREF(pvBits);
605 RT_NOREF(pszVersion);
606
607 /*
608 * Give up at once if there is no entry table.
609 */
610 if (!pModLX->Hdr.e32_enttab)
611 return VERR_SYMBOL_NOT_FOUND;
612
613 /*
614 * Translate the symbol name into an ordinal.
615 */
616 if (pchSymbol)
617 {
618 rc = kldrModLXDoNameLookup(pModLX, pchSymbol, cchSymbol, &iSymbol);
619 if (RT_FAILURE(rc))
620 return rc;
621 }
622
623 /*
624 * Iterate the entry table.
625 * (The entry table is made up of bundles of similar exports.)
626 */
627 iOrdinal = 1;
628 pBundle = (const struct b32_bundle *)pModLX->pbEntryTab;
629 while (pBundle->b32_cnt && iOrdinal <= iSymbol)
630 {
631 static const size_t s_cbEntry[] = { 0, 3, 5, 5, 7 };
632
633 /*
634 * Check for a hit first.
635 */
636 iOrdinal += pBundle->b32_cnt;
637 if (iSymbol < iOrdinal)
638 {
639 uint32_t offObject;
640 const struct e32_entry *pEntry = (const struct e32_entry *)((uintptr_t)(pBundle + 1)
641 + (iSymbol - (iOrdinal - pBundle->b32_cnt))
642 * s_cbEntry[pBundle->b32_type]);
643
644 /*
645 * Calculate the return address.
646 */
647 kldrModLXResolveBaseAddress(pModLX, &BaseAddress);
648 switch (pBundle->b32_type)
649 {
650 /* empty bundles are place holders unused ordinal ranges. */
651 case EMPTY:
652 return VERR_SYMBOL_NOT_FOUND;
653
654 /* e32_flags + a 16-bit offset. */
655 case ENTRY16:
656 offObject = pEntry->e32_variant.e32_offset.offset16;
657 if (pfKind)
658 *pfKind = RTLDRSYMKIND_16BIT | RTLDRSYMKIND_NO_TYPE;
659 break;
660
661 /* e32_flags + a 16-bit offset + a 16-bit callgate selector. */
662 case GATE16:
663 offObject = pEntry->e32_variant.e32_callgate.offset;
664 if (pfKind)
665 *pfKind = RTLDRSYMKIND_16BIT | RTLDRSYMKIND_CODE;
666 break;
667
668 /* e32_flags + a 32-bit offset. */
669 case ENTRY32:
670 offObject = pEntry->e32_variant.e32_offset.offset32;
671 if (pfKind)
672 *pfKind = RTLDRSYMKIND_32BIT;
673 break;
674
675 /* e32_flags + 16-bit import module ordinal + a 32-bit procname or ordinal. */
676 case ENTRYFWD:
677 return kldrModLXDoForwarderQuery(pModLX, pEntry, pfnGetForwarder, pvUser, puValue, pfKind);
678
679 default:
680 /* anyone actually using TYPEINFO will end up here. */
681 KLDRMODLX_ASSERT(!"Bad bundle type");
682 return VERR_LDRLX_BAD_BUNDLE;
683 }
684
685 /*
686 * Validate the object number and calc the return address.
687 */
688 if ( pBundle->b32_obj <= 0
689 || pBundle->b32_obj > pModLX->cSegments)
690 return VERR_LDRLX_BAD_BUNDLE;
691 if (puValue)
692 *puValue = BaseAddress
693 + offObject
694 + pModLX->aSegments[pBundle->b32_obj - 1].RVA;
695 return VINF_SUCCESS;
696 }
697
698 /*
699 * Skip the bundle.
700 */
701 if (pBundle->b32_type > ENTRYFWD)
702 {
703 KLDRMODLX_ASSERT(!"Bad type"); /** @todo figure out TYPEINFO. */
704 return VERR_LDRLX_BAD_BUNDLE;
705 }
706 if (pBundle->b32_type == 0)
707 pBundle = (const struct b32_bundle *)((const uint8_t *)pBundle + 2);
708 else
709 pBundle = (const struct b32_bundle *)((const uint8_t *)(pBundle + 1) + s_cbEntry[pBundle->b32_type] * pBundle->b32_cnt);
710 }
711
712 return VERR_SYMBOL_NOT_FOUND;
713}
714
715
716/**
717 * @interface_method_impl{RTLDROPS,pfnGetSymbolEx}
718 */
719static DECLCALLBACK(int) rtldrLX_GetSymbolEx(PRTLDRMODINTERNAL pMod, const void *pvBits, RTUINTPTR BaseAddress,
720 uint32_t iOrdinal, const char *pszSymbol, RTUINTPTR *pValue)
721{
722 uint32_t fKind = RTLDRSYMKIND_REQ_FLAT;
723 return kldrModLXQuerySymbol(pMod, pvBits, BaseAddress, iOrdinal, pszSymbol, pszSymbol ? strlen(pszSymbol) : 0,
724 NULL, NULL, NULL, pValue, &fKind);
725}
726
727
728/**
729 * Do name lookup.
730 *
731 * @returns IPRT status code.
732 * @param pModLX The module to lookup the symbol in.
733 * @param pchSymbol The symbol to lookup.
734 * @param cchSymbol The symbol name length.
735 * @param piSymbol Where to store the symbol ordinal.
736 */
737static int kldrModLXDoNameLookup(PKLDRMODLX pModLX, const char *pchSymbol, size_t cchSymbol, uint32_t *piSymbol)
738{
739
740 /*
741 * First do a hash table lookup.
742 */
743 /** @todo hash name table for speed. */
744
745 /*
746 * Search the name tables.
747 */
748 const uint8_t *pbName = kldrModLXDoNameTableLookupByName(pModLX->pbResNameTab,
749 pModLX->pbLoaderSectionLast - pModLX->pbResNameTab + 1,
750 pchSymbol, cchSymbol);
751 if (!pbName)
752 {
753 if (!pModLX->pbNonResNameTab)
754 {
755 /* lazy load it */
756 /** @todo non-resident name table. */
757 }
758 if (pModLX->pbNonResNameTab)
759 pbName = kldrModLXDoNameTableLookupByName(pModLX->pbResNameTab,
760 pModLX->pbNonResNameTabLast - pModLX->pbResNameTab + 1,
761 pchSymbol, cchSymbol);
762 }
763 if (!pbName)
764 return VERR_SYMBOL_NOT_FOUND;
765
766 *piSymbol = *(const uint16_t *)(pbName + 1 + *pbName);
767 return VINF_SUCCESS;
768}
769
770
771/**
772 * Lookup a name table entry by name.
773 *
774 * @returns Pointer to the name table entry if found.
775 * @returns NULL if not found.
776 * @param pbNameTable Pointer to the name table that should be searched.
777 * @param cbNameTable The size of the name table.
778 * @param pchSymbol The name of the symbol we're looking for.
779 * @param cchSymbol The length of the symbol name.
780 */
781static const uint8_t *kldrModLXDoNameTableLookupByName(const uint8_t *pbNameTable, ssize_t cbNameTable,
782 const char *pchSymbol, size_t cchSymbol)
783{
784 /*
785 * Determin the namelength up front so we can skip anything which doesn't matches the length.
786 */
787 uint8_t cbSymbol8Bit = (uint8_t)cchSymbol;
788 if (cbSymbol8Bit != cchSymbol)
789 return NULL; /* too long. */
790
791 /*
792 * Walk the name table.
793 */
794 while (*pbNameTable != 0 && cbNameTable > 0)
795 {
796 const uint8_t cbName = *pbNameTable;
797
798 cbNameTable -= cbName + 1 + 2;
799 if (cbNameTable < 0)
800 break;
801
802 if ( cbName == cbSymbol8Bit
803 && !memcmp(pbNameTable + 1, pchSymbol, cbName))
804 return pbNameTable;
805
806 /* next entry */
807 pbNameTable += cbName + 1 + 2;
808 }
809
810 return NULL;
811}
812
813
814/**
815 * Deal with a forwarder entry.
816 *
817 * @returns IPRT status code.
818 * @param pModLX The PE module interpreter instance.
819 * @param pEntry The forwarder entry.
820 * @param pfnGetForwarder The callback for resolving forwarder symbols. (optional)
821 * @param pvUser The user argument for the callback.
822 * @param puValue Where to put the value. (optional)
823 * @param pfKind Where to put the symbol kind. (optional)
824 */
825static int kldrModLXDoForwarderQuery(PKLDRMODLX pModLX, const struct e32_entry *pEntry,
826 PFNRTLDRIMPORT pfnGetForwarder, void *pvUser, PRTLDRADDR puValue, uint32_t *pfKind)
827{
828 if (!pfnGetForwarder)
829 return VERR_LDR_FORWARDER;
830
831 /*
832 * Validate the entry import module ordinal.
833 */
834 if ( !pEntry->e32_variant.e32_fwd.modord
835 || pEntry->e32_variant.e32_fwd.modord > pModLX->Hdr.e32_impmodcnt)
836 return VERR_LDRLX_BAD_FORWARDER;
837
838 char szImpModule[256];
839 int rc = kldrModLXGetImport(pModLX, NULL, pEntry->e32_variant.e32_fwd.modord - 1, szImpModule, sizeof(szImpModule), NULL);
840 if (RT_FAILURE(rc))
841 return rc;
842
843 /*
844 * Figure out the parameters.
845 */
846 uint32_t iSymbol;
847 const char *pszSymbol;
848 char szSymbol[256];
849 if (pEntry->e32_flags & FWD_ORDINAL)
850 {
851 iSymbol = pEntry->e32_variant.e32_fwd.value;
852 pszSymbol = NULL; /* no symbol name. */
853 }
854 else
855 {
856 const uint8_t *pbName;
857
858 /* load the fixup section if necessary. */
859 if (!pModLX->pbImportProcs)
860 {
861 rc = kldrModLXDoLoadFixupSection(pModLX);
862 if (RT_FAILURE(rc))
863 return rc;
864 }
865
866 /* Make name pointer. */
867 pbName = pModLX->pbImportProcs + pEntry->e32_variant.e32_fwd.value;
868 if ( pbName >= pModLX->pbFixupSectionLast
869 || pbName < pModLX->pbFixupSection
870 || !*pbName)
871 return VERR_LDRLX_BAD_FORWARDER;
872
873
874 /* check for '#' name. */
875 if (pbName[1] == '#')
876 {
877 uint8_t cbLeft = *pbName;
878 const uint8_t *pb = pbName + 1;
879 unsigned uBase;
880
881 /* base detection */
882 uBase = 10;
883 if ( cbLeft > 1
884 && pb[1] == '0'
885 && (pb[2] == 'x' || pb[2] == 'X'))
886 {
887 uBase = 16;
888 pb += 2;
889 cbLeft -= 2;
890 }
891
892 /* ascii to integer */
893 iSymbol = 0;
894 while (cbLeft-- > 0)
895 {
896 /* convert char to digit. */
897 unsigned uDigit = *pb++;
898 if (uDigit >= '0' && uDigit <= '9')
899 uDigit -= '0';
900 else if (uDigit >= 'a' && uDigit <= 'z')
901 uDigit -= 'a' + 10;
902 else if (uDigit >= 'A' && uDigit <= 'Z')
903 uDigit -= 'A' + 10;
904 else if (!uDigit)
905 break;
906 else
907 return VERR_LDRLX_BAD_FORWARDER;
908 if (uDigit >= uBase)
909 return VERR_LDRLX_BAD_FORWARDER;
910
911 /* insert the digit */
912 iSymbol *= uBase;
913 iSymbol += uDigit;
914 }
915 if (!iSymbol)
916 return VERR_LDRLX_BAD_FORWARDER;
917
918 pszSymbol = NULL; /* no symbol name. */
919 }
920 else
921 {
922 memcpy(szSymbol, pbName + 1, *pbName);
923 szSymbol[*pbName] = '\0';
924 pszSymbol = szSymbol;
925 iSymbol = UINT32_MAX;
926 }
927 }
928
929 /*
930 * Resolve the forwarder.
931 */
932 rc = pfnGetForwarder(&pModLX->Core, szImpModule, pszSymbol, iSymbol, puValue, /*pfKind, */pvUser);
933 if (RT_SUCCESS(rc) && pfKind)
934 *pfKind |= RTLDRSYMKIND_FORWARDER;
935 return rc;
936}
937
938
939/**
940 * Loads the fixup section from the executable image.
941 *
942 * The fixup section isn't loaded until it's accessed. It's also freed by kLdrModDone().
943 *
944 * @returns IPRT status code.
945 * @param pModLX The PE module interpreter instance.
946 */
947static int kldrModLXDoLoadFixupSection(PKLDRMODLX pModLX)
948{
949 void *pv = RTMemAlloc(pModLX->Hdr.e32_fixupsize);
950 if (!pv)
951 return VERR_NO_MEMORY;
952
953 uint32_t off = pModLX->Hdr.e32_objtab + pModLX->Hdr.e32_ldrsize;
954 int rc = pModLX->Core.pReader->pfnRead(pModLX->Core.pReader, pv, pModLX->Hdr.e32_fixupsize,
955 off + pModLX->offHdr);
956 if (RT_SUCCESS(rc))
957 {
958 pModLX->pbFixupSection = (uint8_t *)pv;
959 pModLX->pbFixupSectionLast = pModLX->pbFixupSection + pModLX->Hdr.e32_fixupsize;
960 KLDRMODLX_ASSERT(!pModLX->paoffPageFixups);
961 if (pModLX->Hdr.e32_fpagetab)
962 pModLX->paoffPageFixups = (const uint32_t *)(pModLX->pbFixupSection + pModLX->Hdr.e32_fpagetab - off);
963 KLDRMODLX_ASSERT(!pModLX->pbFixupRecs);
964 if (pModLX->Hdr.e32_frectab)
965 pModLX->pbFixupRecs = pModLX->pbFixupSection + pModLX->Hdr.e32_frectab - off;
966 KLDRMODLX_ASSERT(!pModLX->pbImportMods);
967 if (pModLX->Hdr.e32_impmod)
968 pModLX->pbImportMods = pModLX->pbFixupSection + pModLX->Hdr.e32_impmod - off;
969 KLDRMODLX_ASSERT(!pModLX->pbImportProcs);
970 if (pModLX->Hdr.e32_impproc)
971 pModLX->pbImportProcs = pModLX->pbFixupSection + pModLX->Hdr.e32_impproc - off;
972 }
973 else
974 RTMemFree(pv);
975 return rc;
976}
977
978
979/**
980 * @interface_method_impl{RTLDROPS,pfnEnumSymbols}
981 */
982static DECLCALLBACK(int) rtldrLX_EnumSymbols(PRTLDRMODINTERNAL pMod, unsigned fFlags, const void *pvBits,
983 RTUINTPTR BaseAddress, PFNRTLDRENUMSYMS pfnCallback, void *pvUser)
984{
985 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
986 RT_NOREF(pvBits);
987 RT_NOREF(fFlags);
988
989 kldrModLXResolveBaseAddress(pModLX, &BaseAddress);
990
991 /*
992 * Enumerate the entry table.
993 * (The entry table is made up of bundles of similar exports.)
994 */
995 int rc = VINF_SUCCESS;
996 uint32_t iOrdinal = 1;
997 const struct b32_bundle *pBundle = (const struct b32_bundle *)pModLX->pbEntryTab;
998 while (pBundle->b32_cnt && iOrdinal)
999 {
1000 static const size_t s_cbEntry[] = { 0, 3, 5, 5, 7 };
1001
1002 /*
1003 * Enum the entries in the bundle.
1004 */
1005 if (pBundle->b32_type != EMPTY)
1006 {
1007 const struct e32_entry *pEntry;
1008 size_t cbEntry;
1009 RTLDRADDR BundleRVA;
1010 unsigned cLeft;
1011
1012
1013 /* Validate the bundle. */
1014 switch (pBundle->b32_type)
1015 {
1016 case ENTRY16:
1017 case GATE16:
1018 case ENTRY32:
1019 if ( pBundle->b32_obj <= 0
1020 || pBundle->b32_obj > pModLX->cSegments)
1021 return VERR_LDRLX_BAD_BUNDLE;
1022 BundleRVA = pModLX->aSegments[pBundle->b32_obj - 1].RVA;
1023 break;
1024
1025 case ENTRYFWD:
1026 BundleRVA = 0;
1027 break;
1028
1029 default:
1030 /* anyone actually using TYPEINFO will end up here. */
1031 KLDRMODLX_ASSERT(!"Bad bundle type");
1032 return VERR_LDRLX_BAD_BUNDLE;
1033 }
1034
1035 /* iterate the bundle entries. */
1036 cbEntry = s_cbEntry[pBundle->b32_type];
1037 pEntry = (const struct e32_entry *)(pBundle + 1);
1038 cLeft = pBundle->b32_cnt;
1039 while (cLeft-- > 0)
1040 {
1041 RTLDRADDR uValue;
1042 uint32_t fKind;
1043 int fFoundName;
1044 const uint8_t *pbName;
1045
1046 /*
1047 * Calc the symbol value and kind.
1048 */
1049 switch (pBundle->b32_type)
1050 {
1051 /* e32_flags + a 16-bit offset. */
1052 case ENTRY16:
1053 uValue = BaseAddress + BundleRVA + pEntry->e32_variant.e32_offset.offset16;
1054 fKind = RTLDRSYMKIND_16BIT | RTLDRSYMKIND_NO_TYPE;
1055 break;
1056
1057 /* e32_flags + a 16-bit offset + a 16-bit callgate selector. */
1058 case GATE16:
1059 uValue = BaseAddress + BundleRVA + pEntry->e32_variant.e32_callgate.offset;
1060 fKind = RTLDRSYMKIND_16BIT | RTLDRSYMKIND_CODE;
1061 break;
1062
1063 /* e32_flags + a 32-bit offset. */
1064 case ENTRY32:
1065 uValue = BaseAddress + BundleRVA + pEntry->e32_variant.e32_offset.offset32;
1066 fKind = RTLDRSYMKIND_32BIT;
1067 break;
1068
1069 /* e32_flags + 16-bit import module ordinal + a 32-bit procname or ordinal. */
1070 case ENTRYFWD:
1071 uValue = 0; /** @todo implement enumeration of forwarders properly. */
1072 fKind = RTLDRSYMKIND_FORWARDER;
1073 break;
1074
1075 default: /* shut up gcc. */
1076 uValue = 0;
1077 fKind = RTLDRSYMKIND_NO_BIT | RTLDRSYMKIND_NO_TYPE;
1078 break;
1079 }
1080
1081 /*
1082 * Any symbol names?
1083 */
1084 fFoundName = 0;
1085 char szName[256];
1086
1087 /* resident name table. */
1088 pbName = pModLX->pbResNameTab;
1089 if (pbName)
1090 {
1091 do
1092 {
1093 pbName = kldrModLXDoNameTableLookupByOrdinal(pbName, pModLX->pbLoaderSectionLast - pbName + 1, iOrdinal);
1094 if (!pbName)
1095 break;
1096 fFoundName = 1;
1097 memcpy(szName, (const char *)pbName + 1, *pbName);
1098 szName[*pbName] = '\0';
1099 rc = pfnCallback(pMod, szName, iOrdinal, uValue, /*fKind,*/ pvUser);
1100 if (rc != VINF_SUCCESS)
1101 return rc;
1102
1103 /* skip to the next entry */
1104 pbName += 1 + *pbName + 2;
1105 } while (pbName < pModLX->pbLoaderSectionLast);
1106 }
1107
1108 /* resident name table. */
1109 pbName = pModLX->pbNonResNameTab;
1110 /** @todo lazy load the non-resident name table. */
1111 if (pbName)
1112 {
1113 do
1114 {
1115 pbName = kldrModLXDoNameTableLookupByOrdinal(pbName, pModLX->pbNonResNameTabLast - pbName + 1, iOrdinal);
1116 if (!pbName)
1117 break;
1118 fFoundName = 1;
1119 memcpy(szName, (const char *)pbName + 1, *pbName);
1120 szName[*pbName] = '\0';
1121 rc = pfnCallback(pMod, szName, iOrdinal, uValue, /*fKind,*/ pvUser);
1122 if (rc != VINF_SUCCESS)
1123 return rc;
1124
1125 /* skip to the next entry */
1126 pbName += 1 + *pbName + 2;
1127 } while (pbName < pModLX->pbLoaderSectionLast);
1128 }
1129
1130 /*
1131 * If no names, call once with the ordinal only.
1132 */
1133 if (!fFoundName)
1134 {
1135 RT_NOREF(fKind);
1136 rc = pfnCallback(pMod, NULL /*pszName*/, iOrdinal, uValue, /*fKind,*/ pvUser);
1137 if (rc != VINF_SUCCESS)
1138 return rc;
1139 }
1140
1141 /* next */
1142 iOrdinal++;
1143 pEntry = (const struct e32_entry *)((uintptr_t)pEntry + cbEntry);
1144 }
1145 }
1146
1147 /*
1148 * The next bundle.
1149 */
1150 if (pBundle->b32_type > ENTRYFWD)
1151 {
1152 KLDRMODLX_ASSERT(!"Bad type"); /** @todo figure out TYPEINFO. */
1153 return VERR_LDRLX_BAD_BUNDLE;
1154 }
1155 if (pBundle->b32_type == 0)
1156 pBundle = (const struct b32_bundle *)((const uint8_t *)pBundle + 2);
1157 else
1158 pBundle = (const struct b32_bundle *)((const uint8_t *)(pBundle + 1) + s_cbEntry[pBundle->b32_type] * pBundle->b32_cnt);
1159 }
1160
1161 return VINF_SUCCESS;
1162}
1163
1164
1165/**
1166 * Lookup a name table entry by ordinal.
1167 *
1168 * @returns Pointer to the name table entry if found.
1169 * @returns NULL if not found.
1170 * @param pbNameTable Pointer to the name table that should be searched.
1171 * @param cbNameTable The size of the name table.
1172 * @param iOrdinal The ordinal to search for.
1173 */
1174static const uint8_t *kldrModLXDoNameTableLookupByOrdinal(const uint8_t *pbNameTable, ssize_t cbNameTable, uint32_t iOrdinal)
1175{
1176 while (*pbNameTable != 0 && cbNameTable > 0)
1177 {
1178 const uint8_t cbName = *pbNameTable;
1179 uint32_t iName;
1180
1181 cbNameTable -= cbName + 1 + 2;
1182 if (cbNameTable < 0)
1183 break;
1184
1185 iName = *(pbNameTable + cbName + 1)
1186 | ((unsigned)*(pbNameTable + cbName + 2) << 8);
1187 if (iName == iOrdinal)
1188 return pbNameTable;
1189
1190 /* next entry */
1191 pbNameTable += cbName + 1 + 2;
1192 }
1193
1194 return NULL;
1195}
1196
1197
1198static int kldrModLXGetImport(PKLDRMODLX pModLX, const void *pvBits, uint32_t iImport, char *pszName, size_t cchName,
1199 size_t *pcbNeeded)
1200{
1201 const uint8_t *pb;
1202 int rc;
1203 RT_NOREF(pvBits);
1204
1205 /*
1206 * Validate
1207 */
1208 if (iImport >= pModLX->Hdr.e32_impmodcnt)
1209 return VERR_LDRLX_IMPORT_ORDINAL_OUT_OF_BOUNDS;
1210
1211 /*
1212 * Lazy loading the fixup section.
1213 */
1214 if (!pModLX->pbImportMods)
1215 {
1216 rc = kldrModLXDoLoadFixupSection(pModLX);
1217 if (RT_FAILURE(rc))
1218 return rc;
1219 }
1220
1221 /*
1222 * Iterate the module import table until we reach the requested import ordinal.
1223 */
1224 pb = pModLX->pbImportMods;
1225 while (iImport-- > 0)
1226 pb += *pb + 1;
1227
1228 /*
1229 * Copy out the result.
1230 */
1231 if (pcbNeeded)
1232 *pcbNeeded = *pb + 1;
1233 if (*pb < cchName)
1234 {
1235 memcpy(pszName, pb + 1, *pb);
1236 pszName[*pb] = '\0';
1237 rc = VINF_SUCCESS;
1238 }
1239 else
1240 {
1241 memcpy(pszName, pb + 1, cchName);
1242 if (cchName)
1243 pszName[cchName - 1] = '\0';
1244 rc = VERR_BUFFER_OVERFLOW;
1245 }
1246
1247 return rc;
1248}
1249
1250#if 0
1251
1252/** @copydoc kLdrModNumberOfImports */
1253static int32_t kldrModLXNumberOfImports(PRTLDRMODINTERNAL pMod, const void *pvBits)
1254{
1255 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
1256 RT_NOREF(pvBits);
1257 return pModLX->Hdr.e32_impmodcnt;
1258}
1259
1260
1261/** @copydoc kLdrModGetStackInfo */
1262static int kldrModLXGetStackInfo(PRTLDRMODINTERNAL pMod, const void *pvBits, RTLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo)
1263{
1264 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
1265 const uint32_t i = pModLX->Hdr.e32_stackobj;
1266 RT_NOREF(pvBits);
1267
1268 if ( i
1269 && i <= pModLX->cSegments
1270 && pModLX->Hdr.e32_esp <= pModLX->aSegments[i - 1].LinkAddress + pModLX->aSegments[i - 1].cb
1271 && pModLX->Hdr.e32_stacksize
1272 && pModLX->Hdr.e32_esp - pModLX->Hdr.e32_stacksize >= pModLX->aSegments[i - 1].LinkAddress)
1273 {
1274
1275 kldrModLXResolveBaseAddress(pModLX, &BaseAddress);
1276 pStackInfo->LinkAddress = pModLX->Hdr.e32_esp - pModLX->Hdr.e32_stacksize;
1277 pStackInfo->Address = BaseAddress
1278 + pModLX->aSegments[i - 1].RVA
1279 + pModLX->Hdr.e32_esp - pModLX->Hdr.e32_stacksize - pModLX->aSegments[i - 1].LinkAddress;
1280 }
1281 else
1282 {
1283 pSt0ackInfo->Address = NIL_RTLDRADDR;
1284 pStackInfo->LinkAddress = NIL_RTLDRADDR;
1285 }
1286 pStackInfo->cbStack = pModLX->Hdr.e32_stacksize;
1287 pStackInfo->cbStackThread = 0;
1288
1289 return VINF_SUCCESS;
1290}
1291
1292
1293/** @copydoc kLdrModQueryMainEntrypoint */
1294static int kldrModLXQueryMainEntrypoint(PRTLDRMODINTERNAL pMod, const void *pvBits, RTLDRADDR BaseAddress, PRTLDRADDR pMainEPAddress)
1295{
1296 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
1297 RT_NOREF(pvBits);
1298
1299 /*
1300 * Convert the address from the header.
1301 */
1302 kldrModLXResolveBaseAddress(pModLX, &BaseAddress);
1303 *pMainEPAddress = pModLX->Hdr.e32_startobj
1304 && pModLX->Hdr.e32_startobj <= pModLX->cSegments
1305 && pModLX->Hdr.e32_eip < pModLX->aSegments[pModLX->Hdr.e32_startobj - 1].cb
1306 ? BaseAddress + pModLX->aSegments[pModLX->Hdr.e32_startobj - 1].RVA + pModLX->Hdr.e32_eip
1307 : NIL_RTLDRADDR;
1308 return VINF_SUCCESS;
1309}
1310
1311#endif
1312
1313/** Helper for rtldrLX_EnumDbgInfo. */
1314static int rtldrLx_EnumDbgInfoHelper(PKLDRMODLX pModLX, PFNRTLDRENUMDBG pfnCallback, void *pvUser,
1315 uint8_t *pbBuf, uint32_t cbRead, uint32_t offDbgInfo, bool *pfReturn)
1316{
1317 RTLDRDBGINFO DbgInfo;
1318 uint32_t iDbgInfo = 0;
1319 uint32_t cbDbgInfo = pModLX->Hdr.e32_debuglen;
1320
1321 /*
1322 * Recent watcom linkers emit PE style IMAGE_DEBUG_MISC for specifying
1323 * external file with CV info.
1324 */
1325 if (cbRead >= sizeof(IMAGE_DEBUG_MISC))
1326 {
1327 PCIMAGE_DEBUG_MISC pMisc = (PCIMAGE_DEBUG_MISC)pbBuf;
1328 if ( pMisc->DataType == IMAGE_DEBUG_MISC_EXENAME
1329 && pMisc->Length <= cbRead
1330 && pMisc->Length >= RT_UOFFSETOF(IMAGE_DEBUG_MISC, Data[4])
1331 && pMisc->Unicode == 0
1332 && pMisc->Reserved[0] == 0
1333 && pMisc->Reserved[1] == 0
1334 && pMisc->Reserved[2] == 0
1335 && pMisc->Data[0] >= 0x20
1336 && pMisc->Data[0] < 0x7f
1337 && pMisc->Data[1] >= 0x20
1338 && pMisc->Data[1] < 0x7f
1339 && pMisc->Data[2] >= 0x20
1340 && pMisc->Data[2] < 0x7f )
1341 {
1342 uint32_t cchMaxName = pMisc->Length - RT_UOFFSETOF(IMAGE_DEBUG_MISC, Data[0]);
1343 for (uint32_t cchName = 3; cchName < cchMaxName; cchName++)
1344 {
1345 char const ch = pMisc->Data[cchName];
1346 if (ch == 0)
1347 {
1348 DbgInfo.enmType = RTLDRDBGINFOTYPE_CODEVIEW;
1349 DbgInfo.iDbgInfo = iDbgInfo;
1350 DbgInfo.offFile = offDbgInfo;
1351 DbgInfo.LinkAddress = NIL_RTLDRADDR;
1352 DbgInfo.cb = pMisc->Length;
1353 DbgInfo.pszExtFile = (char *)&pMisc->Data[0];
1354 DbgInfo.u.Cv.cbImage = pModLX->Hdr.e32_mpages * pModLX->Hdr.e32_pagesize;
1355 DbgInfo.u.Cv.uTimestamp = 0;
1356 DbgInfo.u.Cv.uMajorVer = 0;
1357 DbgInfo.u.Cv.uMinorVer = 0;
1358
1359 *pfReturn = true;
1360 int rc = pfnCallback(&pModLX->Core, &DbgInfo, pvUser);
1361 if (rc != VINF_SUCCESS)
1362 return rc;
1363 }
1364 else if (ch >= 0x30 && ch < 0x7f)
1365 continue;
1366 break;
1367 }
1368
1369 /* Skip it. */
1370 pbBuf += pMisc->Length;
1371 cbRead -= pMisc->Length;
1372 offDbgInfo += pMisc->Length;
1373 cbDbgInfo -= pMisc->Length;
1374 iDbgInfo++;
1375 }
1376 }
1377
1378 /*
1379 * Look for codeview signature.
1380 */
1381 RTCVHDR const *pCvHdr = (RTCVHDR const *)pbBuf;
1382 if ( cbRead > sizeof(*pCvHdr)
1383 && pCvHdr->off >= sizeof(*pCvHdr)
1384 && pCvHdr->off < cbDbgInfo)
1385 {
1386 switch (pCvHdr->u32Magic)
1387 {
1388 case RTCVHDR_MAGIC_NB11:
1389 case RTCVHDR_MAGIC_NB09:
1390 case RTCVHDR_MAGIC_NB08:
1391 case RTCVHDR_MAGIC_NB07:
1392 case RTCVHDR_MAGIC_NB06:
1393 case RTCVHDR_MAGIC_NB05:
1394 case RTCVHDR_MAGIC_NB04:
1395 case RTCVHDR_MAGIC_NB02:
1396 case RTCVHDR_MAGIC_NB01:
1397 case RTCVHDR_MAGIC_NB00:
1398 DbgInfo.enmType = RTLDRDBGINFOTYPE_CODEVIEW;
1399 DbgInfo.iDbgInfo = iDbgInfo;
1400 DbgInfo.offFile = offDbgInfo;
1401 DbgInfo.LinkAddress = NIL_RTLDRADDR;
1402 DbgInfo.cb = cbDbgInfo;
1403 DbgInfo.pszExtFile = NULL;
1404 DbgInfo.u.Cv.cbImage = pModLX->Hdr.e32_mpages * pModLX->Hdr.e32_pagesize;
1405 DbgInfo.u.Cv.uTimestamp = 0;
1406 DbgInfo.u.Cv.uMajorVer = 0;
1407 DbgInfo.u.Cv.uMinorVer = 0;
1408
1409 *pfReturn = true;
1410 return pfnCallback(&pModLX->Core, &DbgInfo, pvUser);
1411 }
1412 }
1413
1414 /*
1415 * Watcom wraps its DWARF output in an ELF image, so look for and ELF magic.
1416 */
1417 Elf32_Ehdr const *pElfHdr = (Elf32_Ehdr const *)pbBuf;
1418 if ( cbRead >= sizeof(*pElfHdr)
1419 && pElfHdr->e_ident[EI_MAG0] == ELFMAG0
1420 && pElfHdr->e_ident[EI_MAG1] == ELFMAG1
1421 && pElfHdr->e_ident[EI_MAG2] == ELFMAG2
1422 && pElfHdr->e_ident[EI_MAG3] == ELFMAG3
1423 && pElfHdr->e_ident[EI_CLASS] == ELFCLASS32
1424 && pElfHdr->e_ident[EI_DATA] == ELFDATA2LSB
1425 && pElfHdr->e_ident[EI_VERSION] == EV_CURRENT
1426 && pElfHdr->e_shentsize == sizeof(Elf32_Shdr)
1427 && pElfHdr->e_shnum >= 2
1428 && pElfHdr->e_shnum < _32K + 10
1429 && pElfHdr->e_shstrndx <= pElfHdr->e_shnum
1430 && pElfHdr->e_shstrndx > 0
1431 )
1432 {
1433 /** @todo try use pBuf for reading into and try to read more at once. */
1434 uint32_t const offShdrs = pElfHdr->e_shoff + offDbgInfo;
1435 uint32_t const cShdrs = pElfHdr->e_shnum;
1436 uint32_t const cbShdr = pElfHdr->e_shentsize;
1437 int rc = VINF_SUCCESS;
1438
1439 /* Read the section string table. */
1440 Elf32_Shdr Shdr;
1441 int rc2 = pModLX->Core.pReader->pfnRead(pModLX->Core.pReader, &Shdr, sizeof(Shdr),
1442 offShdrs + pElfHdr->e_shstrndx * cbShdr);
1443 if ( RT_SUCCESS(rc2)
1444 && Shdr.sh_offset > 0
1445 && Shdr.sh_size > 0
1446 && Shdr.sh_size < _256K
1447 && Shdr.sh_type == SHT_STRTAB)
1448 {
1449 uint32_t const cbStrTab = Shdr.sh_size;
1450 char * const pszStrTab = (char *)RTMemTmpAlloc(cbStrTab + 2);
1451 if (pszStrTab)
1452 {
1453 rc2 = pModLX->Core.pReader->pfnRead(pModLX->Core.pReader, pszStrTab, Shdr.sh_size, offDbgInfo + Shdr.sh_offset);
1454 if (RT_SUCCESS(rc2))
1455 {
1456 pszStrTab[cbStrTab] = '\0';
1457
1458 /* Iterate the sections, one by one. */
1459 for (uint32_t i = 1; i < cShdrs; i++)
1460 {
1461 rc = pModLX->Core.pReader->pfnRead(pModLX->Core.pReader, &Shdr, sizeof(Shdr), offShdrs + i * cbShdr);
1462 if ( RT_SUCCESS(rc)
1463 && Shdr.sh_name < cbStrTab
1464 && strncmp(&pszStrTab[Shdr.sh_name], RT_STR_TUPLE(".debug_")) == 0)
1465 {
1466 DbgInfo.enmType = RTLDRDBGINFOTYPE_DWARF;
1467 DbgInfo.iDbgInfo = iDbgInfo;
1468 DbgInfo.offFile = offDbgInfo + Shdr.sh_offset;
1469 DbgInfo.LinkAddress = NIL_RTLDRADDR;
1470 DbgInfo.cb = Shdr.sh_size;
1471 DbgInfo.pszExtFile = NULL;
1472 DbgInfo.u.Dwarf.pszSection = &pszStrTab[Shdr.sh_name];
1473
1474 *pfReturn = true;
1475 rc = pfnCallback(&pModLX->Core, &DbgInfo, pvUser);
1476 if (rc != VINF_SUCCESS)
1477 break;
1478 iDbgInfo++;
1479 }
1480 }
1481 }
1482 RTMemTmpFree(pszStrTab);
1483 }
1484 }
1485 return rc;
1486 }
1487
1488 /*
1489 * Watcom debug info? Don't know how to detect it...
1490 */
1491
1492 return VINF_SUCCESS;
1493}
1494
1495
1496/**
1497 * @interface_method_impl{RTLDROPS,pfnEnumDbgInfo}
1498 */
1499static DECLCALLBACK(int) rtldrLX_EnumDbgInfo(PRTLDRMODINTERNAL pMod, const void *pvBits,
1500 PFNRTLDRENUMDBG pfnCallback, void *pvUser)
1501{
1502 /*PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);*/
1503 RT_NOREF(pfnCallback);
1504 RT_NOREF(pvUser);
1505
1506 /*
1507 * Quit immediately if no debug info.
1508 */
1509 if (kldrModLXHasDbgInfo(pMod, pvBits))
1510 return VINF_SUCCESS;
1511 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
1512
1513 /*
1514 * Read the debug info and look for familiar magics and structures.
1515 */
1516 union
1517 {
1518 uint8_t ab[1024];
1519 IMAGE_DEBUG_MISC Misc;
1520 RTCVHDR CvHdr;
1521 } uBuf;
1522
1523 bool fReturn = false;
1524
1525 /* Try the offset without header displacement first. */
1526 uint32_t cbToRead = RT_MIN(pModLX->Hdr.e32_debuglen, sizeof(uBuf));
1527 int rc = pModLX->Core.pReader->pfnRead(pModLX->Core.pReader, &uBuf, cbToRead, pModLX->Hdr.e32_debuginfo);
1528 if (RT_SUCCESS(rc))
1529 rc = rtldrLx_EnumDbgInfoHelper(pModLX, pfnCallback, pvUser, &uBuf.ab[0], cbToRead, pModLX->Hdr.e32_debuginfo, &fReturn);
1530
1531 /* If that didn't yield anything, try displaying it by the header offset. */
1532 if (!fReturn && pModLX->offHdr > 0)
1533 {
1534 rc = pModLX->Core.pReader->pfnRead(pModLX->Core.pReader, &uBuf, cbToRead, pModLX->Hdr.e32_debuginfo + pModLX->offHdr);
1535 if (RT_SUCCESS(rc))
1536 rc = rtldrLx_EnumDbgInfoHelper(pModLX, pfnCallback, pvUser, &uBuf.ab[0], cbToRead,
1537 pModLX->Hdr.e32_debuginfo + pModLX->offHdr, &fReturn);
1538 }
1539 return rc;
1540}
1541
1542
1543static int kldrModLXHasDbgInfo(PRTLDRMODINTERNAL pMod, const void *pvBits)
1544{
1545 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
1546 RT_NOREF(pvBits);
1547
1548 /*
1549 * Don't currently bother with linkers which doesn't advertise it in the header.
1550 */
1551 if ( !pModLX->Hdr.e32_debuginfo
1552 || !pModLX->Hdr.e32_debuglen)
1553 return VERR_NOT_FOUND;
1554 return VINF_SUCCESS;
1555}
1556
1557#if 0
1558
1559/** @copydoc kLdrModMap */
1560static int kldrModLXMap(PRTLDRMODINTERNAL pMod)
1561{
1562 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
1563 unsigned fFixed;
1564 void *pvBase;
1565 int rc;
1566
1567 /*
1568 * Already mapped?
1569 */
1570 if (pModLX->pvMapping)
1571 return KLDR_ERR_ALREADY_MAPPED;
1572
1573 /*
1574 * Allocate memory for it.
1575 */
1576 /* fixed image? */
1577 fFixed = pModLX->Core.enmType == RTLDRTYPE_EXECUTABLE_FIXED
1578 || pModLX->Core.enmType == RTLDRTYPE_SHARED_LIBRARY_FIXED;
1579 if (!fFixed)
1580 pvBase = NULL;
1581 else
1582 {
1583 pvBase = (void *)(uintptr_t)pModLX->aSegments[0].LinkAddress;
1584 if ((uintptr_t)pvBase != pModLX->aSegments[0].LinkAddress)
1585 return KLDR_ERR_ADDRESS_OVERFLOW;
1586 }
1587 rc = kHlpPageAlloc(&pvBase, pModLX->cbMapped, KPROT_EXECUTE_READWRITE, fFixed);
1588 if (RT_FAILURE(rc))
1589 return rc;
1590
1591 /*
1592 * Load the bits, apply page protection, and update the segment table.
1593 */
1594 rc = kldrModLXDoLoadBits(pModLX, pvBase);
1595 if (RT_SUCCESS(rc))
1596 rc = kldrModLXDoProtect(pModLX, pvBase, 0 /* protect */);
1597 if (RT_SUCCESS(rc))
1598 {
1599 uint32_t i;
1600 for (i = 0; i < pModLX->cSegments; i++)
1601 {
1602 if (pModLX->aSegments[i].RVA != NIL_RTLDRADDR)
1603 pModLX->aSegments[i].MapAddress = (uintptr_t)pvBase + (uintptr_t)pModLX->aSegments[i].RVA;
1604 }
1605 pModLX->pvMapping = pvBase;
1606 }
1607 else
1608 kHlpPageFree(pvBase, pModLX->cbMapped);
1609 return rc;
1610}
1611
1612#endif
1613
1614/**
1615 * Loads the LX pages into the specified memory mapping.
1616 *
1617 * @returns IPRT status code.
1618 *
1619 * @param pModLX The LX module interpreter instance.
1620 * @param pvBits Where to load the bits.
1621 */
1622static int kldrModLXDoLoadBits(PKLDRMODLX pModLX, void *pvBits)
1623{
1624 const PRTLDRREADER pRdr = pModLX->Core.pReader;
1625 uint8_t *pbTmpPage = NULL;
1626 int rc = VINF_SUCCESS;
1627 uint32_t i;
1628
1629 /*
1630 * Iterate the segments.
1631 */
1632 for (i = 0; i < pModLX->Hdr.e32_objcnt; i++)
1633 {
1634 const struct o32_obj * const pObj = &pModLX->paObjs[i];
1635 const uint32_t cPages = (uint32_t)(pModLX->aSegments[i].cbMapped / OBJPAGELEN);
1636 uint32_t iPage;
1637 uint8_t *pbPage = (uint8_t *)pvBits + (uintptr_t)pModLX->aSegments[i].RVA;
1638
1639 /*
1640 * Iterate the page map pages.
1641 */
1642 for (iPage = 0; RT_SUCCESS(rc) && iPage < pObj->o32_mapsize; iPage++, pbPage += OBJPAGELEN)
1643 {
1644 const struct o32_map *pMap = &pModLX->paPageMappings[iPage + pObj->o32_pagemap - 1];
1645 switch (pMap->o32_pageflags)
1646 {
1647 case VALID:
1648 if (pMap->o32_pagesize == OBJPAGELEN)
1649 rc = pRdr->pfnRead(pRdr, pbPage, OBJPAGELEN,
1650 pModLX->Hdr.e32_datapage + (pMap->o32_pagedataoffset << pModLX->Hdr.e32_pageshift));
1651 else if (pMap->o32_pagesize < OBJPAGELEN)
1652 {
1653 rc = pRdr->pfnRead(pRdr, pbPage, pMap->o32_pagesize,
1654 pModLX->Hdr.e32_datapage + (pMap->o32_pagedataoffset << pModLX->Hdr.e32_pageshift));
1655 memset(pbPage + pMap->o32_pagesize, 0, OBJPAGELEN - pMap->o32_pagesize);
1656 }
1657 else
1658 rc = VERR_LDRLX_BAD_PAGE_MAP;
1659 break;
1660
1661 case ITERDATA:
1662 case ITERDATA2:
1663 /* make sure we've got a temp page .*/
1664 if (!pbTmpPage)
1665 {
1666 pbTmpPage = (uint8_t *)RTMemAlloc(OBJPAGELEN + 256);
1667 if (!pbTmpPage)
1668 break;
1669 }
1670 /* validate the size. */
1671 if (pMap->o32_pagesize > OBJPAGELEN + 252)
1672 {
1673 rc = VERR_LDRLX_BAD_PAGE_MAP;
1674 break;
1675 }
1676
1677 /* read it and ensure 4 extra zero bytes. */
1678 rc = pRdr->pfnRead(pRdr, pbTmpPage, pMap->o32_pagesize,
1679 pModLX->Hdr.e32_datapage + (pMap->o32_pagedataoffset << pModLX->Hdr.e32_pageshift));
1680 if (RT_FAILURE(rc))
1681 break;
1682 memset(pbTmpPage + pMap->o32_pagesize, 0, 4);
1683
1684 /* unpack it into the image page. */
1685 if (pMap->o32_pageflags == ITERDATA2)
1686 rc = kldrModLXDoIterData2Unpacking(pbPage, pbTmpPage, pMap->o32_pagesize);
1687 else
1688 rc = kldrModLXDoIterDataUnpacking(pbPage, pbTmpPage, pMap->o32_pagesize);
1689 break;
1690
1691 case INVALID: /* we're probably not dealing correctly with INVALID pages... */
1692 case ZEROED:
1693 memset(pbPage, 0, OBJPAGELEN);
1694 break;
1695
1696 case RANGE:
1697 KLDRMODLX_ASSERT(!"RANGE");
1698 RT_FALL_THRU();
1699 default:
1700 rc = VERR_LDRLX_BAD_PAGE_MAP;
1701 break;
1702 }
1703 }
1704 if (RT_FAILURE(rc))
1705 break;
1706
1707 /*
1708 * Zero the remaining pages.
1709 */
1710 if (iPage < cPages)
1711 memset(pbPage, 0, (cPages - iPage) * OBJPAGELEN);
1712 }
1713
1714 if (pbTmpPage)
1715 RTMemFree(pbTmpPage);
1716 return rc;
1717}
1718
1719
1720/**
1721 * Unpacks iterdata (aka EXEPACK).
1722 *
1723 * @returns IPRT status code.
1724 * @param pbDst Where to put the uncompressed data. (Assumes OBJPAGELEN size.)
1725 * @param pbSrc The compressed source data.
1726 * @param cbSrc The file size of the compressed data. The source buffer
1727 * contains 4 additional zero bytes.
1728 */
1729static int kldrModLXDoIterDataUnpacking(uint8_t *pbDst, const uint8_t *pbSrc, int cbSrc)
1730{
1731 const struct LX_Iter *pIter = (const struct LX_Iter *)pbSrc;
1732 int cbDst = OBJPAGELEN;
1733
1734 /* Validate size of data. */
1735 if (cbSrc >= (int)OBJPAGELEN - 2)
1736 return VERR_LDRLX_BAD_ITERDATA;
1737
1738 /*
1739 * Expand the page.
1740 */
1741 while (cbSrc > 0 && pIter->LX_nIter)
1742 {
1743 if (pIter->LX_nBytes == 1)
1744 {
1745 /*
1746 * Special case - one databyte.
1747 */
1748 cbDst -= pIter->LX_nIter;
1749 if (cbDst < 0)
1750 return VERR_LDRLX_BAD_ITERDATA;
1751
1752 cbSrc -= 4 + 1;
1753 if (cbSrc < -4)
1754 return VERR_LDRLX_BAD_ITERDATA;
1755
1756 memset(pbDst, pIter->LX_Iterdata, pIter->LX_nIter);
1757 pbDst += pIter->LX_nIter;
1758 pIter++;
1759 }
1760 else
1761 {
1762 /*
1763 * General.
1764 */
1765 int i;
1766
1767 cbDst -= pIter->LX_nIter * pIter->LX_nBytes;
1768 if (cbDst < 0)
1769 return VERR_LDRLX_BAD_ITERDATA;
1770
1771 cbSrc -= 4 + pIter->LX_nBytes;
1772 if (cbSrc < -4)
1773 return VERR_LDRLX_BAD_ITERDATA;
1774
1775 for (i = pIter->LX_nIter; i > 0; i--, pbDst += pIter->LX_nBytes)
1776 memcpy(pbDst, &pIter->LX_Iterdata, pIter->LX_nBytes);
1777 pIter = (struct LX_Iter *)((char*)pIter + 4 + pIter->LX_nBytes);
1778 }
1779 }
1780
1781 /*
1782 * Zero remainder of the page.
1783 */
1784 if (cbDst > 0)
1785 memset(pbDst, 0, cbDst);
1786
1787 return VINF_SUCCESS;
1788}
1789
1790
1791/**
1792 * Unpacks iterdata (aka EXEPACK).
1793 *
1794 * @returns IPRT status code.
1795 * @param pbDst Where to put the uncompressed data. (Assumes OBJPAGELEN size.)
1796 * @param pbSrc The compressed source data.
1797 * @param cbSrc The file size of the compressed data. The source buffer
1798 * contains 4 additional zero bytes.
1799 */
1800static int kldrModLXDoIterData2Unpacking(uint8_t *pbDst, const uint8_t *pbSrc, int cbSrc)
1801{
1802 int cbDst = OBJPAGELEN;
1803
1804 while (cbSrc > 0)
1805 {
1806 /*
1807 * Bit 0 and 1 is the encoding type.
1808 */
1809 switch (*pbSrc & 0x03)
1810 {
1811 /*
1812 *
1813 * 0 1 2 3 4 5 6 7
1814 * type | |
1815 * ----------------
1816 * cb <cb bytes of data>
1817 *
1818 * Bits 2-7 is, if not zero, the length of an uncompressed run
1819 * starting at the following byte.
1820 *
1821 * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
1822 * type | | | | | |
1823 * ---------------- ---------------------- -----------------------
1824 * zero cb char to multiply
1825 *
1826 * If the bits are zero, the following two bytes describes a 1 byte interation
1827 * run. First byte is count, second is the byte to copy. A count of zero is
1828 * means end of data, and we simply stops. In that case the rest of the data
1829 * should be zero.
1830 */
1831 case 0:
1832 {
1833 if (*pbSrc)
1834 {
1835 const int cb = *pbSrc >> 2;
1836 cbDst -= cb;
1837 if (cbDst < 0)
1838 return VERR_LDRLX_BAD_ITERDATA2;
1839 cbSrc -= cb + 1;
1840 if (cbSrc < 0)
1841 return VERR_LDRLX_BAD_ITERDATA2;
1842 memcpy(pbDst, ++pbSrc, cb);
1843 pbDst += cb;
1844 pbSrc += cb;
1845 }
1846 else if (cbSrc < 2)
1847 return VERR_LDRLX_BAD_ITERDATA2;
1848 else
1849 {
1850 const int cb = pbSrc[1];
1851 if (!cb)
1852 goto l_endloop;
1853 cbDst -= cb;
1854 if (cbDst < 0)
1855 return VERR_LDRLX_BAD_ITERDATA2;
1856 cbSrc -= 3;
1857 if (cbSrc < 0)
1858 return VERR_LDRLX_BAD_ITERDATA2;
1859 memset(pbDst, pbSrc[2], cb);
1860 pbDst += cb;
1861 pbSrc += 3;
1862 }
1863 break;
1864 }
1865
1866
1867 /*
1868 * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1869 * type | | | | | |
1870 * ---- ------- -------------------------
1871 * cb1 cb2 - 3 offset <cb1 bytes of data>
1872 *
1873 * Two bytes layed out as described above, followed by cb1 bytes of data to be copied.
1874 * The cb2(+3) and offset describes an amount of data to be copied from the expanded
1875 * data relative to the current position. The data copied as you would expect it to be.
1876 */
1877 case 1:
1878 {
1879 cbSrc -= 2;
1880 if (cbSrc < 0)
1881 return VERR_LDRLX_BAD_ITERDATA2;
1882 else
1883 {
1884 const unsigned off = ((unsigned)pbSrc[1] << 1) | (*pbSrc >> 7);
1885 const int cb1 = (*pbSrc >> 2) & 3;
1886 const int cb2 = ((*pbSrc >> 4) & 7) + 3;
1887
1888 pbSrc += 2;
1889 cbSrc -= cb1;
1890 if (cbSrc < 0)
1891 return VERR_LDRLX_BAD_ITERDATA2;
1892 cbDst -= cb1;
1893 if (cbDst < 0)
1894 return VERR_LDRLX_BAD_ITERDATA2;
1895 memcpy(pbDst, pbSrc, cb1);
1896 pbDst += cb1;
1897 pbSrc += cb1;
1898
1899 if (off > OBJPAGELEN - (unsigned)cbDst)
1900 return VERR_LDRLX_BAD_ITERDATA2;
1901 cbDst -= cb2;
1902 if (cbDst < 0)
1903 return VERR_LDRLX_BAD_ITERDATA2;
1904 memmove(pbDst, pbDst - off, cb2);
1905 pbDst += cb2;
1906 }
1907 break;
1908 }
1909
1910
1911 /*
1912 * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1913 * type | | | |
1914 * ---- ----------------------------------
1915 * cb-3 offset
1916 *
1917 * Two bytes layed out as described above.
1918 * The cb(+3) and offset describes an amount of data to be copied from the expanded
1919 * data relative to the current position.
1920 *
1921 * If offset == 1 the data is not copied as expected, but in the memcpyw manner.
1922 */
1923 case 2:
1924 {
1925 cbSrc -= 2;
1926 if (cbSrc < 0)
1927 return VERR_LDRLX_BAD_ITERDATA2;
1928 else
1929 {
1930 const unsigned off = ((unsigned)pbSrc[1] << 4) | (*pbSrc >> 4);
1931 const int cb = ((*pbSrc >> 2) & 3) + 3;
1932
1933 pbSrc += 2;
1934 if (off > OBJPAGELEN - (unsigned)cbDst)
1935 return VERR_LDRLX_BAD_ITERDATA2;
1936 cbDst -= cb;
1937 if (cbDst < 0)
1938 return VERR_LDRLX_BAD_ITERDATA2;
1939 kLdrModLXMemCopyW(pbDst, pbDst - off, cb);
1940 pbDst += cb;
1941 }
1942 break;
1943 }
1944
1945
1946 /*
1947 * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
1948 * type | | | | | |
1949 * ---------- ---------------- ----------------------------------
1950 * cb1 cb2 offset <cb1 bytes of data>
1951 *
1952 * Three bytes layed out as described above, followed by cb1 bytes of data to be copied.
1953 * The cb2 and offset describes an amount of data to be copied from the expanded
1954 * data relative to the current position.
1955 *
1956 * If offset == 1 the data is not copied as expected, but in the memcpyw manner.
1957 */
1958 case 3:
1959 {
1960 cbSrc -= 3;
1961 if (cbSrc < 0)
1962 return VERR_LDRLX_BAD_ITERDATA2;
1963 else
1964 {
1965 const int cb1 = (*pbSrc >> 2) & 0xf;
1966 const int cb2 = ((pbSrc[1] & 0xf) << 2) | (*pbSrc >> 6);
1967 const unsigned off = ((unsigned)pbSrc[2] << 4) | (pbSrc[1] >> 4);
1968
1969 pbSrc += 3;
1970 cbSrc -= cb1;
1971 if (cbSrc < 0)
1972 return VERR_LDRLX_BAD_ITERDATA2;
1973 cbDst -= cb1;
1974 if (cbDst < 0)
1975 return VERR_LDRLX_BAD_ITERDATA2;
1976 memcpy(pbDst, pbSrc, cb1);
1977 pbDst += cb1;
1978 pbSrc += cb1;
1979
1980 if (off > OBJPAGELEN - (unsigned)cbDst)
1981 return VERR_LDRLX_BAD_ITERDATA2;
1982 cbDst -= cb2;
1983 if (cbDst < 0)
1984 return VERR_LDRLX_BAD_ITERDATA2;
1985 kLdrModLXMemCopyW(pbDst, pbDst - off, cb2);
1986 pbDst += cb2;
1987 }
1988 break;
1989 }
1990 } /* type switch. */
1991 } /* unpack loop */
1992
1993l_endloop:
1994
1995
1996 /*
1997 * Zero remainder of the page.
1998 */
1999 if (cbDst > 0)
2000 memset(pbDst, 0, cbDst);
2001
2002 return VINF_SUCCESS;
2003}
2004
2005
2006/**
2007 * Special memcpy employed by the iterdata2 algorithm.
2008 *
2009 * Emulate a 16-bit memcpy (copying 16-bit at a time) and the effects this
2010 * has if src is very close to the destination.
2011 *
2012 * @param pbDst Destination pointer.
2013 * @param pbSrc Source pointer. Will always be <= pbDst.
2014 * @param cb Amount of data to be copied.
2015 * @remark This assumes that unaligned word and dword access is fine.
2016 */
2017static void kLdrModLXMemCopyW(uint8_t *pbDst, const uint8_t *pbSrc, int cb)
2018{
2019 switch (pbDst - pbSrc)
2020 {
2021 case 0:
2022 case 1:
2023 case 2:
2024 case 3:
2025 /* 16-bit copy (unaligned) */
2026 if (cb & 1)
2027 *pbDst++ = *pbSrc++;
2028 for (cb >>= 1; cb > 0; cb--, pbDst += 2, pbSrc += 2)
2029 *(uint16_t *)pbDst = *(const uint16_t *)pbSrc;
2030 break;
2031
2032 default:
2033 /* 32-bit copy (unaligned) */
2034 if (cb & 1)
2035 *pbDst++ = *pbSrc++;
2036 if (cb & 2)
2037 {
2038 *(uint16_t *)pbDst = *(const uint16_t *)pbSrc;
2039 pbDst += 2;
2040 pbSrc += 2;
2041 }
2042 for (cb >>= 2; cb > 0; cb--, pbDst += 4, pbSrc += 4)
2043 *(uint32_t *)pbDst = *(const uint32_t *)pbSrc;
2044 break;
2045 }
2046}
2047
2048#if 0
2049
2050/**
2051 * Unprotects or protects the specified image mapping.
2052 *
2053 * @returns IPRT status code.
2054 *
2055 * @param pModLX The LX module interpreter instance.
2056 * @param pvBits The mapping to protect.
2057 * @param UnprotectOrProtect If 1 unprotect (i.e. make all writable), otherwise
2058 * protect according to the object table.
2059 */
2060static int kldrModLXDoProtect(PKLDRMODLX pModLX, void *pvBits, unsigned fUnprotectOrProtect)
2061{
2062 uint32_t i;
2063
2064 /*
2065 * Change object protection.
2066 */
2067 for (i = 0; i < pModLX->cSegments; i++)
2068 {
2069 int rc;
2070 void *pv;
2071 KPROT enmProt;
2072
2073 /* calc new protection. */
2074 enmProt = pModLX->aSegments[i].enmProt;
2075 if (fUnprotectOrProtect)
2076 {
2077 switch (enmProt)
2078 {
2079 case KPROT_NOACCESS:
2080 case KPROT_READONLY:
2081 case KPROT_READWRITE:
2082 case KPROT_WRITECOPY:
2083 enmProt = KPROT_READWRITE;
2084 break;
2085 case KPROT_EXECUTE:
2086 case KPROT_EXECUTE_READ:
2087 case KPROT_EXECUTE_READWRITE:
2088 case KPROT_EXECUTE_WRITECOPY:
2089 enmProt = KPROT_EXECUTE_READWRITE;
2090 break;
2091 default:
2092 KLDRMODLX_ASSERT(!"bad enmProt");
2093 return -1;
2094 }
2095 }
2096 else
2097 {
2098 /* copy on write -> normal write. */
2099 if (enmProt == KPROT_EXECUTE_WRITECOPY)
2100 enmProt = KPROT_EXECUTE_READWRITE;
2101 else if (enmProt == KPROT_WRITECOPY)
2102 enmProt = KPROT_READWRITE;
2103 }
2104
2105
2106 /* calc the address and set page protection. */
2107 pv = (uint8_t *)pvBits + pModLX->aSegments[i].RVA;
2108
2109 rc = kHlpPageProtect(pv, pModLX->aSegments[i].cbMapped, enmProt);
2110 if (RT_FAILURE(rc))
2111 break;
2112
2113 /** @todo the gap page should be marked NOACCESS! */
2114 }
2115
2116 return VINF_SUCCESS;
2117}
2118
2119
2120/** @copydoc kLdrModUnmap */
2121static int kldrModLXUnmap(PRTLDRMODINTERNAL pMod)
2122{
2123 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
2124 uint32_t i;
2125 int rc;
2126
2127 /*
2128 * Mapped?
2129 */
2130 if (!pModLX->pvMapping)
2131 return KLDR_ERR_NOT_MAPPED;
2132
2133 /*
2134 * Free the mapping and update the segments.
2135 */
2136 rc = kHlpPageFree((void *)pModLX->pvMapping, pModLX->cbMapped);
2137 KLDRMODLX_ASSERT(!rc);
2138 pModLX->pvMapping = NULL;
2139
2140 for (i = 0; i < pModLX->cSegments; i++)
2141 pModLX->aSegments[i].MapAddress = 0;
2142
2143 return rc;
2144}
2145
2146
2147/** @copydoc kLdrModAllocTLS */
2148static int kldrModLXAllocTLS(PRTLDRMODINTERNAL pMod, void *pvMapping)
2149{
2150 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
2151
2152 /* no tls, just do the error checking. */
2153 if ( pvMapping == KLDRMOD_INT_MAP
2154 && pModLX->pvMapping)
2155 return KLDR_ERR_NOT_MAPPED;
2156 return VINF_SUCCESS;
2157}
2158
2159
2160/** @copydoc kLdrModFreeTLS */
2161static void kldrModLXFreeTLS(PRTLDRMODINTERNAL pMod, void *pvMapping)
2162{
2163 /* no tls. */
2164 RT_NOREF(pMod);
2165 RT_NOREF(pvMapping);
2166
2167}
2168
2169
2170/** @copydoc kLdrModReload */
2171static int kldrModLXReload(PRTLDRMODINTERNAL pMod)
2172{
2173 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
2174 int rc, rc2;
2175
2176 /*
2177 * Mapped?
2178 */
2179 if (!pModLX->pvMapping)
2180 return KLDR_ERR_NOT_MAPPED;
2181
2182 /*
2183 * Before doing anything we'll have to make all pages writable.
2184 */
2185 rc = kldrModLXDoProtect(pModLX, (void *)pModLX->pvMapping, 1 /* unprotect */);
2186 if (RT_FAILURE(rc))
2187 return rc;
2188
2189 /*
2190 * Load the bits again.
2191 */
2192 rc = kldrModLXDoLoadBits(pModLX, (void *)pModLX->pvMapping);
2193
2194 /*
2195 * Restore protection.
2196 */
2197 rc2 = kldrModLXDoProtect(pModLX, (void *)pModLX->pvMapping, 0 /* protect */);
2198 if (RT_SUCCESS(rc) && RT_FAILURE(rc2))
2199 rc = rc2;
2200 return rc;
2201}
2202
2203
2204/** @copydoc kLdrModFixupMapping */
2205static int kldrModLXFixupMapping(PRTLDRMODINTERNAL pMod, PFNRTLDRIMPORT pfnGetImport, void *pvUser)
2206{
2207 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
2208 int rc, rc2;
2209
2210 /*
2211 * Mapped?
2212 */
2213 if (!pModLX->pvMapping)
2214 return KLDR_ERR_NOT_MAPPED;
2215
2216 /*
2217 * Before doing anything we'll have to make all pages writable.
2218 */
2219 rc = kldrModLXDoProtect(pModLX, (void *)pModLX->pvMapping, 1 /* unprotect */);
2220 if (RT_FAILURE(rc))
2221 return rc;
2222
2223 /*
2224 * Apply fixups and resolve imports.
2225 */
2226 rc = rtldrLX_RelocateBits(pMod, (void *)pModLX->pvMapping, (uintptr_t)pModLX->pvMapping,
2227 pModLX->aSegments[0].LinkAddress, pfnGetImport, pvUser);
2228
2229 /*
2230 * Restore protection.
2231 */
2232 rc2 = kldrModLXDoProtect(pModLX, (void *)pModLX->pvMapping, 0 /* protect */);
2233 if (RT_SUCCESS(rc) && RT_FAILURE(rc2))
2234 rc = rc2;
2235 return rc;
2236}
2237
2238
2239/** @copydoc kLdrModCallInit */
2240static int kldrModLXCallInit(PRTLDRMODINTERNAL pMod, void *pvMapping, uintptr_t uHandle)
2241{
2242 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
2243 int rc;
2244
2245 /*
2246 * Mapped?
2247 */
2248 if (pvMapping == KLDRMOD_INT_MAP)
2249 {
2250 pvMapping = (void *)pModLX->pvMapping;
2251 if (!pvMapping)
2252 return KLDR_ERR_NOT_MAPPED;
2253 }
2254
2255 /*
2256 * Do TLS callbacks first and then call the init/term function if it's a DLL.
2257 */
2258 if ((pModLX->Hdr.e32_mflags & E32MODMASK) == E32MODDLL)
2259 rc = kldrModLXDoCallDLL(pModLX, pvMapping, 0 /* attach */, uHandle);
2260 else
2261 rc = VINF_SUCCESS;
2262 return rc;
2263}
2264
2265
2266/**
2267 * Call the DLL entrypoint.
2268 *
2269 * @returns 0 on success.
2270 * @returns KLDR_ERR_MODULE_INIT_FAILED or KLDR_ERR_THREAD_ATTACH_FAILED on failure.
2271 * @param pModLX The LX module interpreter instance.
2272 * @param pvMapping The module mapping to use (resolved).
2273 * @param uOp The operation (DLL_*).
2274 * @param uHandle The module handle to present.
2275 */
2276static int kldrModLXDoCallDLL(PKLDRMODLX pModLX, void *pvMapping, unsigned uOp, uintptr_t uHandle)
2277{
2278 int rc;
2279
2280 /*
2281 * If no entrypoint there isn't anything to be done.
2282 */
2283 if ( !pModLX->Hdr.e32_startobj
2284 || pModLX->Hdr.e32_startobj > pModLX->Hdr.e32_objcnt)
2285 return VINF_SUCCESS;
2286
2287 /*
2288 * Invoke the entrypoint and convert the boolean result to a kLdr status code.
2289 */
2290 rc = kldrModLXDoCall((uintptr_t)pvMapping
2291 + (uintptr_t)pModLX->aSegments[pModLX->Hdr.e32_startobj - 1].RVA
2292 + pModLX->Hdr.e32_eip,
2293 uHandle, uOp, NULL);
2294 if (rc)
2295 rc = VINF_SUCCESS;
2296 else if (uOp == 0 /* attach */)
2297 rc = KLDR_ERR_MODULE_INIT_FAILED;
2298 else /* detach: ignore failures */
2299 rc = VINF_SUCCESS;
2300 return rc;
2301}
2302
2303
2304/**
2305 * Do a 3 parameter callback.
2306 *
2307 * @returns 32-bit callback return.
2308 * @param uEntrypoint The address of the function to be called.
2309 * @param uHandle The first argument, the module handle.
2310 * @param uOp The second argumnet, the reason we're calling.
2311 * @param pvReserved The third argument, reserved argument. (figure this one out)
2312 */
2313static int32_t kldrModLXDoCall(uintptr_t uEntrypoint, uintptr_t uHandle, uint32_t uOp, void *pvReserved)
2314{
2315#if defined(__X86__) || defined(__i386__) || defined(_M_IX86)
2316 int32_t rc;
2317/** @todo try/except */
2318
2319 /*
2320 * Paranoia.
2321 */
2322# ifdef __GNUC__
2323 __asm__ __volatile__(
2324 "pushl %2\n\t"
2325 "pushl %1\n\t"
2326 "pushl %0\n\t"
2327 "lea 12(%%esp), %2\n\t"
2328 "call *%3\n\t"
2329 "movl %2, %%esp\n\t"
2330 : "=a" (rc)
2331 : "d" (uOp),
2332 "S" (0),
2333 "c" (uEntrypoint),
2334 "0" (uHandle));
2335# elif defined(_MSC_VER)
2336 __asm {
2337 mov eax, [uHandle]
2338 mov edx, [uOp]
2339 mov ecx, 0
2340 mov ebx, [uEntrypoint]
2341 push edi
2342 mov edi, esp
2343 push ecx
2344 push edx
2345 push eax
2346 call ebx
2347 mov esp, edi
2348 pop edi
2349 mov [rc], eax
2350 }
2351# else
2352# error "port me!"
2353# endif
2354 RT_NOREF(pvReserved);
2355 return rc;
2356
2357#else
2358 RT_NOREF(uEntrypoint);
2359 RT_NOREF(uHandle);
2360 RT_NOREF(uOp);
2361 RT_NOREF(pvReserved);
2362 return KCPU_ERR_ARCH_CPU_NOT_COMPATIBLE;
2363#endif
2364}
2365
2366
2367/** @copydoc kLdrModCallTerm */
2368static int kldrModLXCallTerm(PRTLDRMODINTERNAL pMod, void *pvMapping, uintptr_t uHandle)
2369{
2370 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
2371
2372 /*
2373 * Mapped?
2374 */
2375 if (pvMapping == KLDRMOD_INT_MAP)
2376 {
2377 pvMapping = (void *)pModLX->pvMapping;
2378 if (!pvMapping)
2379 return KLDR_ERR_NOT_MAPPED;
2380 }
2381
2382 /*
2383 * Do the call.
2384 */
2385 if ((pModLX->Hdr.e32_mflags & E32MODMASK) == E32MODDLL)
2386 kldrModLXDoCallDLL(pModLX, pvMapping, 1 /* detach */, uHandle);
2387
2388 return VINF_SUCCESS;
2389}
2390
2391
2392/** @copydoc kLdrModCallThread */
2393static int kldrModLXCallThread(PRTLDRMODINTERNAL pMod, void *pvMapping, uintptr_t uHandle, unsigned fAttachingOrDetaching)
2394{
2395 /* no thread attach/detach callout. */
2396 RT_NOREF(pMod);
2397 RT_NOREF(pvMapping);
2398 RT_NOREF(uHandle);
2399 RT_NOREF(fAttachingOrDetaching);
2400 return VINF_SUCCESS;
2401}
2402
2403#endif
2404
2405/**
2406 * @interface_method_impl{RTLDROPS,pfnGetImageSize}
2407 */
2408static DECLCALLBACK(size_t) rtldrLX_GetImageSize(PRTLDRMODINTERNAL pMod)
2409{
2410 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
2411 return pModLX->cbMapped;
2412}
2413
2414
2415/**
2416 * @interface_method_impl{RTLDROPS,pfnGetBits}
2417 */
2418static DECLCALLBACK(int) rtldrLX_GetBits(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR BaseAddress,
2419 PFNRTLDRIMPORT pfnGetImport, void *pvUser)
2420{
2421 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
2422
2423 /*
2424 * Load the image bits.
2425 */
2426 int rc = kldrModLXDoLoadBits(pModLX, pvBits);
2427 if (RT_SUCCESS(rc))
2428 {
2429 /*
2430 * Perform relocations.
2431 *
2432 * We force this to take place by messing with the OldBaseAddress as we
2433 * have to apply internal relocations even if the load address is the
2434 * same as the link address.
2435 */
2436 rc = rtldrLX_RelocateBits(pMod, pvBits, BaseAddress,
2437 _4K ^ BaseAddress ^ pModLX->aSegments[0].LinkAddress,
2438 pfnGetImport, pvUser);
2439 }
2440 return rc;
2441}
2442
2443
2444/* GCC goes boinkers if we put this inside the function. */
2445union RELOC_VISIBILITY_STUPIDITY
2446{
2447 const uint8_t *pb;
2448 const struct r32_rlc *prlc;
2449};
2450
2451/**
2452 * @interface_method_impl{RTLDROPS,pfnRelocate}
2453 */
2454static DECLCALLBACK(int) rtldrLX_RelocateBits(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR NewBaseAddress,
2455 RTUINTPTR OldBaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser)
2456{
2457 PKLDRMODLX pModLX = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
2458 uint32_t iSeg;
2459 int rc;
2460
2461 /*
2462 * Do we need to to *anything*?
2463 */
2464 if ( NewBaseAddress == OldBaseAddress
2465 && NewBaseAddress == pModLX->paObjs[0].o32_base
2466 && !pModLX->Hdr.e32_impmodcnt)
2467 return VINF_SUCCESS;
2468
2469 /*
2470 * Load the fixup section.
2471 */
2472 if (!pModLX->pbFixupSection)
2473 {
2474 rc = kldrModLXDoLoadFixupSection(pModLX);
2475 if (RT_FAILURE(rc))
2476 return rc;
2477 }
2478
2479 /*
2480 * Iterate the segments.
2481 */
2482 for (iSeg = 0; iSeg < pModLX->Hdr.e32_objcnt; iSeg++)
2483 {
2484 const struct o32_obj * const pObj = &pModLX->paObjs[iSeg];
2485 RTLDRADDR PageAddress = NewBaseAddress + pModLX->aSegments[iSeg].RVA;
2486 uint32_t iPage;
2487 uint8_t *pbPage = (uint8_t *)pvBits + (uintptr_t)pModLX->aSegments[iSeg].RVA;
2488
2489 /*
2490 * Iterate the page map pages.
2491 */
2492 for (iPage = 0, rc = VINF_SUCCESS;
2493 RT_SUCCESS(rc) && iPage < pObj->o32_mapsize;
2494 iPage++, pbPage += OBJPAGELEN, PageAddress += OBJPAGELEN)
2495 {
2496 const uint8_t * const pbFixupRecEnd = pModLX->pbFixupRecs + pModLX->paoffPageFixups[iPage + pObj->o32_pagemap];
2497 const uint8_t *pb = pModLX->pbFixupRecs + pModLX->paoffPageFixups[iPage + pObj->o32_pagemap - 1];
2498 RTLDRADDR uValue = NIL_RTLDRADDR;
2499 uint32_t fKind = 0;
2500 int iSelector;
2501
2502 /* sanity */
2503 if (pbFixupRecEnd < pb)
2504 return VERR_LDR_BAD_FIXUP;
2505 if (pbFixupRecEnd - 1 > pModLX->pbFixupSectionLast)
2506 return VERR_LDR_BAD_FIXUP;
2507 if (pb < pModLX->pbFixupSection)
2508 return VERR_LDR_BAD_FIXUP;
2509
2510 /*
2511 * Iterate the fixup record.
2512 */
2513 while (pb < pbFixupRecEnd)
2514 {
2515 union RELOC_VISIBILITY_STUPIDITY u;
2516 char szImpModule[256];
2517 u.pb = pb;
2518 pb += 3 + (u.prlc->nr_stype & NRCHAIN ? 0 : 1); /* place pch at the 4th member. */
2519
2520 /*
2521 * Figure out the target.
2522 */
2523 switch (u.prlc->nr_flags & NRRTYP)
2524 {
2525 /*
2526 * Internal fixup.
2527 */
2528 case NRRINT:
2529 {
2530 uint16_t iTrgObject;
2531 uint32_t offTrgObject;
2532
2533 /* the object */
2534 if (u.prlc->nr_flags & NR16OBJMOD)
2535 {
2536 iTrgObject = *(const uint16_t *)pb;
2537 pb += 2;
2538 }
2539 else
2540 iTrgObject = *pb++;
2541 iTrgObject--;
2542 if (iTrgObject >= pModLX->Hdr.e32_objcnt)
2543 return VERR_LDR_BAD_FIXUP;
2544
2545 /* the target */
2546 if ((u.prlc->nr_stype & NRSRCMASK) != NRSSEG)
2547 {
2548 if (u.prlc->nr_flags & NR32BITOFF)
2549 {
2550 offTrgObject = *(const uint32_t *)pb;
2551 pb += 4;
2552 }
2553 else
2554 {
2555 offTrgObject = *(const uint16_t *)pb;
2556 pb += 2;
2557 }
2558
2559 /* calculate the symbol info. */
2560 uValue = offTrgObject + NewBaseAddress + pModLX->aSegments[iTrgObject].RVA;
2561 }
2562 else
2563 uValue = NewBaseAddress + pModLX->aSegments[iTrgObject].RVA;
2564 if ( (u.prlc->nr_stype & NRALIAS)
2565 || (pModLX->aSegments[iTrgObject].fFlags & RTLDRSEG_FLAG_16BIT))
2566 iSelector = pModLX->aSegments[iTrgObject].Sel16bit;
2567 else
2568 iSelector = pModLX->aSegments[iTrgObject].SelFlat;
2569 fKind = 0;
2570 break;
2571 }
2572
2573 /*
2574 * Import by symbol ordinal.
2575 */
2576 case NRRORD:
2577 {
2578 uint16_t iModule;
2579 uint32_t iSymbol;
2580
2581 /* the module ordinal */
2582 if (u.prlc->nr_flags & NR16OBJMOD)
2583 {
2584 iModule = *(const uint16_t *)pb;
2585 pb += 2;
2586 }
2587 else
2588 iModule = *pb++;
2589 iModule--;
2590 if (iModule >= pModLX->Hdr.e32_impmodcnt)
2591 return VERR_LDR_BAD_FIXUP;
2592 rc = kldrModLXGetImport(pModLX, NULL, iModule, szImpModule, sizeof(szImpModule), NULL);
2593 if (RT_FAILURE(rc))
2594 return rc;
2595
2596#if 1
2597 if (u.prlc->nr_flags & NRICHAIN)
2598 return VERR_LDR_BAD_FIXUP;
2599#endif
2600
2601 /* . */
2602 if (u.prlc->nr_flags & NR32BITOFF)
2603 {
2604 iSymbol = *(const uint32_t *)pb;
2605 pb += 4;
2606 }
2607 else if (!(u.prlc->nr_flags & NR8BITORD))
2608 {
2609 iSymbol = *(const uint16_t *)pb;
2610 pb += 2;
2611 }
2612 else
2613 iSymbol = *pb++;
2614
2615 /* resolve it. */
2616 rc = pfnGetImport(pMod, szImpModule, NULL, iSymbol, &uValue, /*&fKind,*/ pvUser);
2617 if (RT_FAILURE(rc))
2618 return rc;
2619 iSelector = -1;
2620 break;
2621 }
2622
2623 /*
2624 * Import by symbol name.
2625 */
2626 case NRRNAM:
2627 {
2628 uint32_t iModule;
2629 uint16_t offSymbol;
2630 const uint8_t *pbSymbol;
2631
2632 /* the module ordinal */
2633 if (u.prlc->nr_flags & NR16OBJMOD)
2634 {
2635 iModule = *(const uint16_t *)pb;
2636 pb += 2;
2637 }
2638 else
2639 iModule = *pb++;
2640 iModule--;
2641 if (iModule >= pModLX->Hdr.e32_impmodcnt)
2642 return VERR_LDR_BAD_FIXUP;
2643 rc = kldrModLXGetImport(pModLX, NULL, iModule, szImpModule, sizeof(szImpModule), NULL);
2644 if (RT_FAILURE(rc))
2645 return rc;
2646#if 1
2647 if (u.prlc->nr_flags & NRICHAIN)
2648 return VERR_LDR_BAD_FIXUP;
2649#endif
2650
2651 /* . */
2652 if (u.prlc->nr_flags & NR32BITOFF)
2653 {
2654 offSymbol = *(const uint32_t *)pb;
2655 pb += 4;
2656 }
2657 else if (!(u.prlc->nr_flags & NR8BITORD))
2658 {
2659 offSymbol = *(const uint16_t *)pb;
2660 pb += 2;
2661 }
2662 else
2663 offSymbol = *pb++;
2664 pbSymbol = pModLX->pbImportProcs + offSymbol;
2665 if ( pbSymbol < pModLX->pbImportProcs
2666 || pbSymbol > pModLX->pbFixupSectionLast)
2667 return VERR_LDR_BAD_FIXUP;
2668 char szSymbol[256];
2669 memcpy(szSymbol, pbSymbol + 1, *pbSymbol);
2670 szSymbol[*pbSymbol] = '\0';
2671
2672 /* resolve it. */
2673 rc = pfnGetImport(pMod, szImpModule, szSymbol, UINT32_MAX, &uValue, /*&fKind,*/ pvUser);
2674 if (RT_FAILURE(rc))
2675 return rc;
2676 iSelector = -1;
2677 break;
2678 }
2679
2680 case NRRENT:
2681 KLDRMODLX_ASSERT(!"NRRENT");
2682 RT_FALL_THRU();
2683 default:
2684 iSelector = -1;
2685 break;
2686 }
2687
2688 /* addend */
2689 if (u.prlc->nr_flags & NRADD)
2690 {
2691 if (u.prlc->nr_flags & NR32BITADD)
2692 {
2693 uValue += *(const uint32_t *)pb;
2694 pb += 4;
2695 }
2696 else
2697 {
2698 uValue += *(const uint16_t *)pb;
2699 pb += 2;
2700 }
2701 }
2702
2703
2704 /*
2705 * Deal with the 'source' (i.e. the place that should be modified - very logical).
2706 */
2707 if (!(u.prlc->nr_stype & NRCHAIN))
2708 {
2709 int off = u.prlc->r32_soff;
2710
2711 /* common / simple */
2712 if ( (u.prlc->nr_stype & NRSRCMASK) == NROFF32
2713 && off >= 0
2714 && off <= (int)OBJPAGELEN - 4)
2715 *(uint32_t *)&pbPage[off] = (uint32_t)uValue;
2716 else if ( (u.prlc->nr_stype & NRSRCMASK) == NRSOFF32
2717 && off >= 0
2718 && off <= (int)OBJPAGELEN - 4)
2719 *(uint32_t *)&pbPage[off] = (uint32_t)(uValue - (PageAddress + off + 4));
2720 else
2721 {
2722 /* generic */
2723 rc = kldrModLXDoReloc(pbPage, off, PageAddress, u.prlc, iSelector, uValue, fKind);
2724 if (RT_FAILURE(rc))
2725 return rc;
2726 }
2727 }
2728 else if (!(u.prlc->nr_flags & NRICHAIN))
2729 {
2730 const int16_t *poffSrc = (const int16_t *)pb;
2731 uint8_t c = u.pb[2];
2732
2733 /* common / simple */
2734 if ((u.prlc->nr_stype & NRSRCMASK) == NROFF32)
2735 {
2736 while (c-- > 0)
2737 {
2738 int off = *poffSrc++;
2739 if (off >= 0 && off <= (int)OBJPAGELEN - 4)
2740 *(uint32_t *)&pbPage[off] = (uint32_t)uValue;
2741 else
2742 {
2743 rc = kldrModLXDoReloc(pbPage, off, PageAddress, u.prlc, iSelector, uValue, fKind);
2744 if (RT_FAILURE(rc))
2745 return rc;
2746 }
2747 }
2748 }
2749 else if ((u.prlc->nr_stype & NRSRCMASK) == NRSOFF32)
2750 {
2751 while (c-- > 0)
2752 {
2753 int off = *poffSrc++;
2754 if (off >= 0 && off <= (int)OBJPAGELEN - 4)
2755 *(uint32_t *)&pbPage[off] = (uint32_t)(uValue - (PageAddress + off + 4));
2756 else
2757 {
2758 rc = kldrModLXDoReloc(pbPage, off, PageAddress, u.prlc, iSelector, uValue, fKind);
2759 if (RT_FAILURE(rc))
2760 return rc;
2761 }
2762 }
2763 }
2764 else
2765 {
2766 while (c-- > 0)
2767 {
2768 rc = kldrModLXDoReloc(pbPage, *poffSrc++, PageAddress, u.prlc, iSelector, uValue, fKind);
2769 if (RT_FAILURE(rc))
2770 return rc;
2771 }
2772 }
2773 pb = (const uint8_t *)poffSrc;
2774 }
2775 else
2776 {
2777 /* This is a pain because it will require virgin pages on a relocation. */
2778 KLDRMODLX_ASSERT(!"NRICHAIN");
2779 return VERR_LDRLX_NRICHAIN_NOT_SUPPORTED;
2780 }
2781 }
2782 }
2783 }
2784
2785 return VINF_SUCCESS;
2786}
2787
2788
2789/**
2790 * Applies the relocation to one 'source' in a page.
2791 *
2792 * This takes care of the more esotic case while the common cases
2793 * are dealt with seperately.
2794 *
2795 * @returns IPRT status code.
2796 * @param pbPage The page in which to apply the fixup.
2797 * @param off Page relative offset of where to apply the offset.
2798 * @param PageAddress The page address.
2799 * @param prlc The relocation record.
2800 * @param iSelector Selector value, -1 if flat.
2801 * @param uValue The target value.
2802 * @param fKind The target kind.
2803 */
2804static int kldrModLXDoReloc(uint8_t *pbPage, int off, RTLDRADDR PageAddress, const struct r32_rlc *prlc,
2805 int iSelector, RTLDRADDR uValue, uint32_t fKind)
2806{
2807#pragma pack(1) /* just to be sure */
2808 union
2809 {
2810 uint8_t ab[6];
2811 uint32_t off32;
2812 uint16_t off16;
2813 uint8_t off8;
2814 struct
2815 {
2816 uint16_t off;
2817 uint16_t Sel;
2818 } Far16;
2819 struct
2820 {
2821 uint32_t off;
2822 uint16_t Sel;
2823 } Far32;
2824 } uData;
2825#pragma pack()
2826 const uint8_t *pbSrc;
2827 uint8_t *pbDst;
2828 uint8_t cb;
2829
2830 RT_NOREF(fKind);
2831
2832 /*
2833 * Compose the fixup data.
2834 */
2835 switch (prlc->nr_stype & NRSRCMASK)
2836 {
2837 case NRSBYT:
2838 uData.off8 = (uint8_t)uValue;
2839 cb = 1;
2840 break;
2841 case NRSSEG:
2842 if (iSelector == -1)
2843 {
2844 /* fixme */
2845 }
2846 uData.off16 = iSelector;
2847 cb = 2;
2848 break;
2849 case NRSPTR:
2850 if (iSelector == -1)
2851 {
2852 /* fixme */
2853 }
2854 uData.Far16.off = (uint16_t)uValue;
2855 uData.Far16.Sel = iSelector;
2856 cb = 4;
2857 break;
2858 case NRSOFF:
2859 uData.off16 = (uint16_t)uValue;
2860 cb = 2;
2861 break;
2862 case NRPTR48:
2863 if (iSelector == -1)
2864 {
2865 /* fixme */
2866 }
2867 uData.Far32.off = (uint32_t)uValue;
2868 uData.Far32.Sel = iSelector;
2869 cb = 6;
2870 break;
2871 case NROFF32:
2872 uData.off32 = (uint32_t)uValue;
2873 cb = 4;
2874 break;
2875 case NRSOFF32:
2876 uData.off32 = (uint32_t)(uValue - (PageAddress + off + 4));
2877 cb = 4;
2878 break;
2879 default:
2880 return VERR_LDRLX_BAD_FIXUP_SECTION; /** @todo fix error, add more checks! */
2881 }
2882
2883 /*
2884 * Apply it. This is sloooow...
2885 */
2886 pbSrc = &uData.ab[0];
2887 pbDst = pbPage + off;
2888 while (cb-- > 0)
2889 {
2890 if (off > (int)OBJPAGELEN)
2891 break;
2892 if (off >= 0)
2893 *pbDst = *pbSrc;
2894 pbSrc++;
2895 pbDst++;
2896 }
2897
2898 return VINF_SUCCESS;
2899}
2900
2901
2902/**
2903 * @interface_method_impl{RTLDROPS,pfnEnumSegments}
2904 */
2905static DECLCALLBACK(int) rtldrLX_EnumSegments(PRTLDRMODINTERNAL pMod, PFNRTLDRENUMSEGS pfnCallback, void *pvUser)
2906{
2907 PKLDRMODLX pThis = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
2908 uint32_t const cSegments = pThis->cSegments;
2909 for (uint32_t iSeg = 0; iSeg < cSegments; iSeg++)
2910 {
2911 int rc = pfnCallback(pMod, &pThis->aSegments[iSeg], pvUser);
2912 if (rc != VINF_SUCCESS)
2913 return rc;
2914 }
2915
2916 return VINF_SUCCESS;
2917}
2918
2919
2920/**
2921 * @interface_method_impl{RTLDROPS,pfnLinkAddressToSegOffset}
2922 */
2923static DECLCALLBACK(int) rtldrLX_LinkAddressToSegOffset(PRTLDRMODINTERNAL pMod, RTLDRADDR LinkAddress,
2924 uint32_t *piSeg, PRTLDRADDR poffSeg)
2925{
2926 PKLDRMODLX pThis = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
2927 uint32_t const cSegments = pThis->cSegments;
2928 for (uint32_t iSeg = 0; iSeg < cSegments; iSeg++)
2929 {
2930 RTLDRADDR offSeg = LinkAddress - pThis->aSegments[iSeg].LinkAddress;
2931 if ( offSeg < pThis->aSegments[iSeg].cbMapped
2932 || offSeg < pThis->aSegments[iSeg].cb)
2933 {
2934 *piSeg = iSeg;
2935 *poffSeg = offSeg;
2936 return VINF_SUCCESS;
2937 }
2938 }
2939
2940 return VERR_LDR_INVALID_LINK_ADDRESS;
2941}
2942
2943
2944/**
2945 * @interface_method_impl{RTLDROPS,pfnLinkAddressToRva}
2946 */
2947static DECLCALLBACK(int) rtldrLX_LinkAddressToRva(PRTLDRMODINTERNAL pMod, RTLDRADDR LinkAddress, PRTLDRADDR pRva)
2948{
2949 PKLDRMODLX pThis = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
2950 uint32_t const cSegments = pThis->cSegments;
2951 for (uint32_t iSeg = 0; iSeg < cSegments; iSeg++)
2952 {
2953 RTLDRADDR offSeg = LinkAddress - pThis->aSegments[iSeg].LinkAddress;
2954 if ( offSeg < pThis->aSegments[iSeg].cbMapped
2955 || offSeg < pThis->aSegments[iSeg].cb)
2956 {
2957 *pRva = pThis->aSegments[iSeg].RVA + offSeg;
2958 return VINF_SUCCESS;
2959 }
2960 }
2961
2962 return VERR_LDR_INVALID_RVA;
2963}
2964
2965
2966/**
2967 * @interface_method_impl{RTLDROPS,pfnSegOffsetToRva}
2968 */
2969static DECLCALLBACK(int) rtldrLX_SegOffsetToRva(PRTLDRMODINTERNAL pMod, uint32_t iSeg, RTLDRADDR offSeg, PRTLDRADDR pRva)
2970{
2971 PKLDRMODLX pThis = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
2972
2973 if (iSeg >= pThis->cSegments)
2974 return VERR_LDR_INVALID_SEG_OFFSET;
2975 PCRTLDRSEG pSegment = &pThis->aSegments[iSeg];
2976
2977 if ( offSeg > pSegment->cbMapped
2978 && offSeg > pSegment->cb
2979 && ( pSegment->cbFile < 0
2980 || offSeg > (uint64_t)pSegment->cbFile))
2981 return VERR_LDR_INVALID_SEG_OFFSET;
2982
2983 *pRva = pSegment->RVA + offSeg;
2984 return VINF_SUCCESS;
2985}
2986
2987
2988/**
2989 * @interface_method_impl{RTLDROPS,pfnRvaToSegOffset}
2990 */
2991static DECLCALLBACK(int) rtldrLX_RvaToSegOffset(PRTLDRMODINTERNAL pMod, RTLDRADDR Rva, uint32_t *piSeg, PRTLDRADDR poffSeg)
2992{
2993 PKLDRMODLX pThis = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
2994 uint32_t const cSegments = pThis->cSegments;
2995 for (uint32_t iSeg = 0; iSeg < cSegments; iSeg++)
2996 {
2997 RTLDRADDR offSeg = Rva - pThis->aSegments[iSeg].RVA;
2998 if ( offSeg < pThis->aSegments[iSeg].cbMapped
2999 || offSeg < pThis->aSegments[iSeg].cb)
3000 {
3001 *piSeg = iSeg;
3002 *poffSeg = offSeg;
3003 return VINF_SUCCESS;
3004 }
3005 }
3006
3007 return VERR_LDR_INVALID_RVA;
3008}
3009
3010
3011/**
3012 * @interface_method_impl{RTLDROPS,pfnReadDbgInfo}
3013 */
3014static DECLCALLBACK(int) rtldrLX_ReadDbgInfo(PRTLDRMODINTERNAL pMod, uint32_t iDbgInfo, RTFOFF off, size_t cb, void *pvBuf)
3015{
3016 PKLDRMODLX pThis = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
3017 RT_NOREF(iDbgInfo);
3018 return pThis->Core.pReader->pfnRead(pThis->Core.pReader, pvBuf, cb, off);
3019}
3020
3021
3022/**
3023 * @interface_method_impl{RTLDROPS,pfnQueryProp}
3024 */
3025static DECLCALLBACK(int) rtldrLX_QueryProp(PRTLDRMODINTERNAL pMod, RTLDRPROP enmProp, void const *pvBits,
3026 void *pvBuf, size_t cbBuf, size_t *pcbRet)
3027{
3028 PKLDRMODLX pThis = RT_FROM_MEMBER(pMod, KLDRMODLX, Core);
3029 int rc;
3030 switch (enmProp)
3031 {
3032 case RTLDRPROP_IMPORT_COUNT:
3033 Assert(cbBuf == sizeof(uint32_t));
3034 Assert(*pcbRet == cbBuf);
3035 *(uint32_t *)pvBuf = pThis->Hdr.e32_impmodcnt;
3036 rc = VINF_SUCCESS;
3037 break;
3038
3039 case RTLDRPROP_IMPORT_MODULE:
3040 rc = kldrModLXGetImport(pThis, pvBits, *(uint32_t const *)pvBuf, (char *)pvBuf, cbBuf, pcbRet);
3041 break;
3042
3043 case RTLDRPROP_INTERNAL_NAME:
3044 *pcbRet = pThis->cchName + 1;
3045 if (cbBuf >= pThis->cchName + 1)
3046 {
3047 memcpy(pvBuf, pThis->pszName, pThis->cchName + 1);
3048 rc = VINF_SUCCESS;
3049 }
3050 else
3051 rc = VERR_BUFFER_OVERFLOW;
3052 break;
3053
3054
3055 default:
3056 rc = VERR_NOT_FOUND;
3057 break;
3058 }
3059 RT_NOREF_PV(pvBits);
3060 return rc;
3061}
3062
3063
3064/**
3065 * Operations for a Mach-O module interpreter.
3066 */
3067static const RTLDROPS s_rtldrLXOps=
3068{
3069 "LX",
3070 rtldrLX_Close,
3071 NULL,
3072 NULL /*pfnDone*/,
3073 rtldrLX_EnumSymbols,
3074 /* ext */
3075 rtldrLX_GetImageSize,
3076 rtldrLX_GetBits,
3077 rtldrLX_RelocateBits,
3078 rtldrLX_GetSymbolEx,
3079 NULL /*pfnQueryForwarderInfo*/,
3080 rtldrLX_EnumDbgInfo,
3081 rtldrLX_EnumSegments,
3082 rtldrLX_LinkAddressToSegOffset,
3083 rtldrLX_LinkAddressToRva,
3084 rtldrLX_SegOffsetToRva,
3085 rtldrLX_RvaToSegOffset,
3086 rtldrLX_ReadDbgInfo,
3087 rtldrLX_QueryProp,
3088 NULL /*pfnVerifySignature*/,
3089 NULL /*pfnHashImage*/,
3090 NULL /*pfnUnwindFrame*/,
3091 42
3092};
3093
3094
3095/**
3096 * Handles opening LX images.
3097 */
3098DECLHIDDEN(int) rtldrLXOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, RTFOFF offLxHdr,
3099 PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo)
3100{
3101
3102 /*
3103 * Create the instance data and do a minimal header validation.
3104 */
3105 PKLDRMODLX pThis = NULL;
3106 int rc = kldrModLXDoCreate(pReader, offLxHdr, fFlags, &pThis, pErrInfo);
3107 if (RT_SUCCESS(rc))
3108 {
3109 /*
3110 * Match up against the requested CPU architecture.
3111 */
3112 if ( enmArch == RTLDRARCH_WHATEVER
3113 || pThis->Core.enmArch == enmArch)
3114 {
3115 pThis->Core.pOps = &s_rtldrLXOps;
3116 pThis->Core.u32Magic = RTLDRMOD_MAGIC;
3117 *phLdrMod = &pThis->Core;
3118 return VINF_SUCCESS;
3119 }
3120 rc = VERR_LDR_ARCH_MISMATCH;
3121 }
3122 if (pThis)
3123 RTMemFree(pThis);
3124 return rc;
3125
3126}
3127
3128
3129RTDECL(int) RTLdrLxSetSegmentSelectors(RTLDRMOD hLdrMod, uint32_t iSegment, uint16_t Sel16bit, uint16_t SelFlat)
3130{
3131 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
3132 PKLDRMODLX pThis = RT_FROM_MEMBER(hLdrMod, KLDRMODLX, Core);
3133 AssertReturn(pThis->Core.pOps == &s_rtldrLXOps, VERR_NOT_SUPPORTED);
3134 AssertReturn(iSegment < pThis->cSegments, VERR_OUT_OF_RANGE);
3135 pThis->aSegments[iSegment].Sel16bit = Sel16bit;
3136 pThis->aSegments[iSegment].SelFlat = SelFlat;
3137 return VINF_SUCCESS;
3138}
3139
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette