[2] | 1 | /* $Id: kLdrDyldOS.c 29 2009-07-01 20:30:29Z bird $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * kLdr - The Dynamic Loader, OS specific operations.
|
---|
| 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 |
|
---|
| 37 | #if K_OS == K_OS_OS2
|
---|
| 38 | # define INCL_BASE
|
---|
| 39 | # define INCL_ERRORS
|
---|
| 40 | # include <os2.h>
|
---|
| 41 |
|
---|
| 42 | #elif K_OS == K_OS_WINDOWS
|
---|
| 43 | # undef IMAGE_DOS_SIGNATURE
|
---|
| 44 | # undef IMAGE_NT_SIGNATURE
|
---|
| 45 | # include <Windows.h>
|
---|
| 46 |
|
---|
| 47 | #else
|
---|
| 48 | # include <k/kHlpAlloc.h>
|
---|
| 49 |
|
---|
| 50 | #endif
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * Allocates a stack.
|
---|
| 55 | *
|
---|
| 56 | * @returns Pointer to the stack. NULL on allocation failure (assumes out of memory).
|
---|
| 57 | * @param cb The size of the stack. This shall be page aligned.
|
---|
| 58 | * If 0, a OS specific default stack size will be employed.
|
---|
| 59 | */
|
---|
| 60 | void *kldrDyldOSAllocStack(KSIZE cb)
|
---|
| 61 | {
|
---|
| 62 | #if K_OS == K_OS_OS2
|
---|
| 63 | APIRET rc;
|
---|
| 64 | PVOID pv;
|
---|
| 65 |
|
---|
| 66 | if (!cb)
|
---|
| 67 | cb = 1 * 1024*1024; /* 1MB */
|
---|
| 68 |
|
---|
| 69 | rc = DosAllocMem(&pv, cb, OBJ_TILE | PAG_COMMIT | PAG_WRITE | PAG_READ);
|
---|
| 70 | if (rc == NO_ERROR)
|
---|
| 71 | return pv;
|
---|
| 72 | return NULL;
|
---|
| 73 |
|
---|
| 74 | #elif K_OS == K_OS_WINDOWS
|
---|
| 75 |
|
---|
| 76 | if (!cb)
|
---|
| 77 | cb = 1 *1024*1024; /* 1MB */
|
---|
| 78 |
|
---|
| 79 | return VirtualAlloc(NULL, cb, MEM_COMMIT, PAGE_READWRITE);
|
---|
| 80 |
|
---|
| 81 | #else
|
---|
| 82 | void *pv;
|
---|
| 83 |
|
---|
| 84 | if (!cb)
|
---|
| 85 | cb = 1 * 1024*1024; /* 1MB */
|
---|
| 86 |
|
---|
| 87 | if (!kHlpPageAlloc(&pv, cb, KPROT_READWRITE, K_FALSE))
|
---|
| 88 | return pv;
|
---|
| 89 | return NULL;
|
---|
| 90 | #endif
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
| 95 | * Invokes the main executable entry point with whatever
|
---|
| 96 | * parameters specific to the host OS and/or module format.
|
---|
| 97 | *
|
---|
| 98 | * @returns
|
---|
| 99 | * @param uMainEPAddress The address of the main entry point.
|
---|
| 100 | * @param pvStack Pointer to the stack object.
|
---|
| 101 | * @param cbStack The size of the stack object.
|
---|
| 102 | */
|
---|
| 103 | int kldrDyldOSStartExe(KUPTR uMainEPAddress, void *pvStack, KSIZE cbStack)
|
---|
| 104 | {
|
---|
| 105 | #if K_OS == K_OS_WINDOWS
|
---|
| 106 | /*
|
---|
| 107 | * Invoke the entrypoint on the current stack for now.
|
---|
| 108 | * Deal with other formats and stack switching another day.
|
---|
| 109 | */
|
---|
| 110 | int rc;
|
---|
| 111 | int (*pfnEP)(void);
|
---|
| 112 | pfnEP = (int (*)(void))uMainEPAddress;
|
---|
| 113 |
|
---|
| 114 | rc = pfnEP();
|
---|
| 115 |
|
---|
| 116 | TerminateProcess(GetCurrentProcess(), rc);
|
---|
| 117 | kHlpAssert(!"TerminateProcess failed");
|
---|
| 118 | for (;;)
|
---|
| 119 | TerminateProcess(GetCurrentProcess(), rc);
|
---|
| 120 | #endif
|
---|
| 121 |
|
---|
| 122 | return -1;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 | void kldrDyldDoLoadExeStackSwitch(PKLDRDYLDMOD pExe, void *pvStack, KSIZE cbStack)
|
---|
| 127 | {
|
---|
| 128 | /*kHlpAssert(!"not implemented");*/
|
---|
| 129 |
|
---|
| 130 | /** @todo implement this properly! */
|
---|
| 131 | kldrDyldDoLoadExe(pExe);
|
---|
| 132 | }
|
---|
| 133 |
|
---|