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