| 1 | /* $Id: kLdrDyldOS.c 3598 2007-10-04 18:46:12Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * kLdr - The Dynamic Loader, OS specific operations.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2006 knut st. osmundsen <bird@anduin.net>
|
|---|
| 7 | *
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of kLdr.
|
|---|
| 10 | *
|
|---|
| 11 | * kLdr is free software; you can redistribute it and/or modify
|
|---|
| 12 | * it under the terms of the GNU General Public License as published by
|
|---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 14 | * (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * kLdr is distributed in the hope that it will be useful,
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 19 | * GNU General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU General Public License
|
|---|
| 22 | * along with kLdr; if not, write to the Free Software
|
|---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | /*******************************************************************************
|
|---|
| 29 | * Header Files *
|
|---|
| 30 | *******************************************************************************/
|
|---|
| 31 | #include <k/kLdr.h>
|
|---|
| 32 | #include "kLdrInternal.h"
|
|---|
| 33 |
|
|---|
| 34 | #if K_OS == K_OS_OS2
|
|---|
| 35 | # define INCL_BASE
|
|---|
| 36 | # define INCL_ERRORS
|
|---|
| 37 | # include <os2.h>
|
|---|
| 38 |
|
|---|
| 39 | #elif K_OS == K_OS_WINDOWS
|
|---|
| 40 | # undef IMAGE_DOS_SIGNATURE
|
|---|
| 41 | # undef IMAGE_NT_SIGNATURE
|
|---|
| 42 | # include <Windows.h>
|
|---|
| 43 |
|
|---|
| 44 | #else
|
|---|
| 45 | # include <k/kHlpAlloc.h>
|
|---|
| 46 |
|
|---|
| 47 | #endif
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * Allocates a stack.
|
|---|
| 52 | *
|
|---|
| 53 | * @returns Pointer to the stack. NULL on allocation failure (assumes out of memory).
|
|---|
| 54 | * @param cb The size of the stack. This shall be page aligned.
|
|---|
| 55 | * If 0, a OS specific default stack size will be employed.
|
|---|
| 56 | */
|
|---|
| 57 | void *kldrDyldOSAllocStack(KSIZE cb)
|
|---|
| 58 | {
|
|---|
| 59 | #if K_OS == K_OS_OS2
|
|---|
| 60 | APIRET rc;
|
|---|
| 61 | PVOID pv;
|
|---|
| 62 |
|
|---|
| 63 | if (!cb)
|
|---|
| 64 | cb = 1 * 1024*1024; /* 1MB */
|
|---|
| 65 |
|
|---|
| 66 | rc = DosAllocMem(&pv, cb, OBJ_TILE | PAG_COMMIT | PAG_WRITE | PAG_READ);
|
|---|
| 67 | if (rc == NO_ERROR)
|
|---|
| 68 | return pv;
|
|---|
| 69 | return NULL;
|
|---|
| 70 |
|
|---|
| 71 | #elif K_OS == K_OS_WINDOWS
|
|---|
| 72 |
|
|---|
| 73 | if (!cb)
|
|---|
| 74 | cb = 1 *1024*1024; /* 1MB */
|
|---|
| 75 |
|
|---|
| 76 | return VirtualAlloc(NULL, cb, MEM_COMMIT, PAGE_READWRITE);
|
|---|
| 77 |
|
|---|
| 78 | #else
|
|---|
| 79 | void *pv;
|
|---|
| 80 |
|
|---|
| 81 | if (!cb)
|
|---|
| 82 | cb = 1 * 1024*1024; /* 1MB */
|
|---|
| 83 |
|
|---|
| 84 | if (!kHlpPageAlloc(&pv, cb, KPROT_READWRITE, K_FALSE))
|
|---|
| 85 | return pv;
|
|---|
| 86 | return NULL;
|
|---|
| 87 | #endif
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * Invokes the main executable entry point with whatever
|
|---|
| 93 | * parameters specific to the host OS and/or module format.
|
|---|
| 94 | *
|
|---|
| 95 | * @returns
|
|---|
| 96 | * @param uMainEPAddress The address of the main entry point.
|
|---|
| 97 | * @param pvStack Pointer to the stack object.
|
|---|
| 98 | * @param cbStack The size of the stack object.
|
|---|
| 99 | */
|
|---|
| 100 | int kldrDyldOSStartExe(KUPTR uMainEPAddress, void *pvStack, KSIZE cbStack)
|
|---|
| 101 | {
|
|---|
| 102 | #if K_OS == K_OS_WINDOWS
|
|---|
| 103 | /*
|
|---|
| 104 | * Invoke the entrypoint on the current stack for now.
|
|---|
| 105 | * Deal with other formats and stack switching another day.
|
|---|
| 106 | */
|
|---|
| 107 | int rc;
|
|---|
| 108 | int (*pfnEP)(void);
|
|---|
| 109 | pfnEP = (int (*)(void))uMainEPAddress;
|
|---|
| 110 |
|
|---|
| 111 | rc = pfnEP();
|
|---|
| 112 |
|
|---|
| 113 | TerminateProcess(GetCurrentProcess(), rc);
|
|---|
| 114 | kHlpAssert(!"TerminateProcess failed");
|
|---|
| 115 | for (;;)
|
|---|
| 116 | TerminateProcess(GetCurrentProcess(), rc);
|
|---|
| 117 | #endif
|
|---|
| 118 |
|
|---|
| 119 | return -1;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 | void kldrDyldDoLoadExeStackSwitch(PKLDRDYLDMOD pExe, void *pvStack, KSIZE cbStack)
|
|---|
| 124 | {
|
|---|
| 125 | /*kHlpAssert(!"not implemented");*/
|
|---|
| 126 |
|
|---|
| 127 | /** @todo implement this properly! */
|
|---|
| 128 | kldrDyldDoLoadExe(pExe);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|