| [2] | 1 | /* $Id: kLdr.c 29 2009-07-01 20:30:29Z bird $ */ | 
|---|
|  | 2 | /** @file | 
|---|
|  | 3 | * kLdr - The Dynamic Loader. | 
|---|
|  | 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 | /** @mainpage   kLdr - The Dynamic Loader | 
|---|
|  | 33 | * | 
|---|
|  | 34 | * The purpose of kLdr is to provide a generic interface for querying | 
|---|
|  | 35 | * information about and loading executable image modules. | 
|---|
|  | 36 | * | 
|---|
|  | 37 | * kLdr defines the term executable image to include all kinds of files that contains | 
|---|
|  | 38 | * binary code that can be executed on a CPU - linker objects (OBJs/Os), shared | 
|---|
|  | 39 | * objects (SOs), dynamic link libraries (DLLs), executables (EXEs), and all kinds | 
|---|
|  | 40 | * of kernel modules / device drivers (SYSs). | 
|---|
|  | 41 | * | 
|---|
|  | 42 | * kLdr provides two types of services: | 
|---|
|  | 43 | *      -# Inspect or/and load individual modules (kLdrMod). | 
|---|
|  | 44 | *      -# Work as a dynamic loader - construct and maintain an address space (kLdrDy). | 
|---|
|  | 45 | * | 
|---|
|  | 46 | * The kLdrMod API works on KLDRMOD structures where all the internals are exposed, while | 
|---|
|  | 47 | * the kLdrDy API works opque KLDRDY structures. KLDRDY are in reality simple wrappers | 
|---|
|  | 48 | * around KLDRMOD with some extra linking and attributes. | 
|---|
|  | 49 | * | 
|---|
|  | 50 | */ | 
|---|
|  | 51 |  | 
|---|
|  | 52 |  | 
|---|
|  | 53 | /******************************************************************************* | 
|---|
|  | 54 | *   Header Files                                                               * | 
|---|
|  | 55 | *******************************************************************************/ | 
|---|
|  | 56 | #include <k/kLdr.h> | 
|---|
|  | 57 | #include "kLdrInternal.h" | 
|---|
|  | 58 |  | 
|---|
|  | 59 |  | 
|---|
|  | 60 | /******************************************************************************* | 
|---|
|  | 61 | *   Global Variables                                                           * | 
|---|
|  | 62 | *******************************************************************************/ | 
|---|
|  | 63 | /** Flag indicating whether we've initialized the loader or not. | 
|---|
|  | 64 | * | 
|---|
|  | 65 | * 0 if not initialized. | 
|---|
|  | 66 | * -1 if we're initializing or terminating. | 
|---|
|  | 67 | * 1 if we've successfully initialized it. | 
|---|
|  | 68 | * -2 if initialization failed. | 
|---|
|  | 69 | */ | 
|---|
|  | 70 | static int volatile g_fInitialized; | 
|---|
|  | 71 |  | 
|---|
|  | 72 |  | 
|---|
|  | 73 |  | 
|---|
|  | 74 | /** | 
|---|
|  | 75 | * Initializes the loader. | 
|---|
|  | 76 | * @returns 0 on success, non-zero OS status code on failure. | 
|---|
|  | 77 | */ | 
|---|
|  | 78 | int kldrInit(void) | 
|---|
|  | 79 | { | 
|---|
|  | 80 | int rc; | 
|---|
|  | 81 |  | 
|---|
|  | 82 | /* check we're already good. */ | 
|---|
|  | 83 | if (g_fInitialized == 1) | 
|---|
|  | 84 | return 0; | 
|---|
|  | 85 |  | 
|---|
|  | 86 | /* a tiny serialization effort. */ | 
|---|
|  | 87 | for (;;) | 
|---|
|  | 88 | { | 
|---|
|  | 89 | if (g_fInitialized == 1) | 
|---|
|  | 90 | return 0; | 
|---|
|  | 91 | if (g_fInitialized == -2) | 
|---|
|  | 92 | return -1; | 
|---|
|  | 93 | /** @todo atomic test and set if we care. */ | 
|---|
|  | 94 | if (g_fInitialized == 0) | 
|---|
|  | 95 | { | 
|---|
|  | 96 | g_fInitialized = -1; | 
|---|
|  | 97 | break; | 
|---|
|  | 98 | } | 
|---|
|  | 99 | kHlpSleep(1); | 
|---|
|  | 100 | } | 
|---|
|  | 101 |  | 
|---|
|  | 102 | /* | 
|---|
|  | 103 | * Do the initialization. | 
|---|
|  | 104 | */ | 
|---|
|  | 105 | rc = kHlpHeapInit(); | 
|---|
|  | 106 | if (!rc) | 
|---|
|  | 107 | { | 
|---|
|  | 108 | rc = kLdrDyldSemInit(); | 
|---|
|  | 109 | if (!rc) | 
|---|
|  | 110 | { | 
|---|
|  | 111 | rc = kldrDyldInit(); | 
|---|
|  | 112 | if (!rc) | 
|---|
|  | 113 | { | 
|---|
|  | 114 | g_fInitialized = 1; | 
|---|
|  | 115 | return 0; | 
|---|
|  | 116 | } | 
|---|
|  | 117 | kLdrDyldSemTerm(); | 
|---|
|  | 118 | } | 
|---|
|  | 119 | kHlpHeapTerm(); | 
|---|
|  | 120 | } | 
|---|
|  | 121 | g_fInitialized = -2; | 
|---|
|  | 122 | return rc; | 
|---|
|  | 123 | } | 
|---|
|  | 124 |  | 
|---|
|  | 125 |  | 
|---|
|  | 126 | /** | 
|---|
|  | 127 | * Terminates the loader. | 
|---|
|  | 128 | */ | 
|---|
|  | 129 | void kldrTerm(void) | 
|---|
|  | 130 | { | 
|---|
|  | 131 | /* can't terminate unless it's initialized. */ | 
|---|
|  | 132 | if (g_fInitialized != 1) | 
|---|
|  | 133 | return; | 
|---|
|  | 134 | g_fInitialized = -1; | 
|---|
|  | 135 |  | 
|---|
|  | 136 | /* | 
|---|
|  | 137 | * Do the termination. | 
|---|
|  | 138 | */ | 
|---|
|  | 139 | kLdrDyldSemTerm(); | 
|---|
|  | 140 | kHlpHeapTerm(); | 
|---|
|  | 141 |  | 
|---|
|  | 142 | /* done */ | 
|---|
|  | 143 | g_fInitialized = 0; | 
|---|
|  | 144 | } | 
|---|
|  | 145 |  | 
|---|