[2826] | 1 | /* $Id: kLdr.c 3601 2007-10-29 00:21:13Z bird $ */
|
---|
[2821] | 2 | /** @file
|
---|
| 3 | * kLdr - The Dynamic Loader.
|
---|
[3601] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
| 7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
|
---|
[2821] | 8 | *
|
---|
[3601] | 9 | * This file is part of kStuff.
|
---|
[2821] | 10 | *
|
---|
[3601] | 11 | * kStuff is free software; you can redistribute it and/or
|
---|
| 12 | * modify it under the terms of the GNU Lesser General Public
|
---|
| 13 | * License as published by the Free Software Foundation; either
|
---|
| 14 | * version 2.1 of the License, or (at your option) any later version.
|
---|
[2821] | 15 | *
|
---|
[3601] | 16 | * In addition to the permissions in the GNU Lesser General Public
|
---|
| 17 | * License, you are granted unlimited permission to link the compiled
|
---|
| 18 | * version of this file into combinations with other programs, and to
|
---|
| 19 | * distribute those combinations without any restriction coming from
|
---|
| 20 | * the use of this file.
|
---|
[2821] | 21 | *
|
---|
[3601] | 22 | * kStuff is distributed in the hope that it will be useful,
|
---|
[2821] | 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
[3601] | 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 25 | * Lesser General Public License for more details.
|
---|
[2821] | 26 | *
|
---|
[3601] | 27 | * You should have received a copy of the GNU Lesser General Public
|
---|
| 28 | * License along with kStuff; if not, write to the Free Software
|
---|
| 29 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
---|
| 30 | * 02110-1301, USA
|
---|
[2821] | 31 | */
|
---|
| 32 |
|
---|
[3601] | 33 |
|
---|
[2852] | 34 | /** @mainpage kLdr - The Dynamic Loader
|
---|
[2824] | 35 | *
|
---|
| 36 | * The purpose of kLdr is to provide a generic interface for querying
|
---|
| 37 | * information about and loading executable image modules.
|
---|
| 38 | *
|
---|
| 39 | * kLdr defines the term executable image to include all kinds of files that contains
|
---|
| 40 | * binary code that can be executed on a CPU - linker objects (OBJs/Os), shared
|
---|
| 41 | * objects (SOs), dynamic link libraries (DLLs), executables (EXEs), and all kinds
|
---|
| 42 | * of kernel modules / device drivers (SYSs).
|
---|
| 43 | *
|
---|
| 44 | * kLdr provides two types of services:
|
---|
| 45 | * -# Inspect or/and load individual modules (kLdrMod).
|
---|
| 46 | * -# Work as a dynamic loader - construct and maintain an address space (kLdrDy).
|
---|
| 47 | *
|
---|
| 48 | * The kLdrMod API works on KLDRMOD structures where all the internals are exposed, while
|
---|
| 49 | * the kLdrDy API works opque KLDRDY structures. KLDRDY are in reality simple wrappers
|
---|
| 50 | * around KLDRMOD with some extra linking and attributes.
|
---|
| 51 | *
|
---|
| 52 | */
|
---|
| 53 |
|
---|
| 54 |
|
---|
[2853] | 55 | /*******************************************************************************
|
---|
| 56 | * Header Files *
|
---|
| 57 | *******************************************************************************/
|
---|
[3570] | 58 | #include <k/kLdr.h>
|
---|
[2883] | 59 | #include "kLdrInternal.h"
|
---|
[2821] | 60 |
|
---|
[2830] | 61 |
|
---|
[2821] | 62 | /*******************************************************************************
|
---|
[2853] | 63 | * Global Variables *
|
---|
[2821] | 64 | *******************************************************************************/
|
---|
[2830] | 65 | /** Flag indicating whether we've initialized the loader or not.
|
---|
| 66 | *
|
---|
| 67 | * 0 if not initialized.
|
---|
| 68 | * -1 if we're initializing or terminating.
|
---|
| 69 | * 1 if we've successfully initialized it.
|
---|
| 70 | * -2 if initialization failed.
|
---|
| 71 | */
|
---|
| 72 | static int volatile g_fInitialized;
|
---|
[2821] | 73 |
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
[2830] | 77 | * Initializes the loader.
|
---|
| 78 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
[2821] | 79 | */
|
---|
| 80 | int kldrInit(void)
|
---|
| 81 | {
|
---|
[2830] | 82 | int rc;
|
---|
| 83 |
|
---|
| 84 | /* check we're already good. */
|
---|
| 85 | if (g_fInitialized == 1)
|
---|
[2821] | 86 | return 0;
|
---|
| 87 |
|
---|
[2830] | 88 | /* a tiny serialization effort. */
|
---|
| 89 | for (;;)
|
---|
| 90 | {
|
---|
| 91 | if (g_fInitialized == 1)
|
---|
| 92 | return 0;
|
---|
| 93 | if (g_fInitialized == -2)
|
---|
| 94 | return -1;
|
---|
| 95 | /** @todo atomic test and set if we care. */
|
---|
| 96 | if (g_fInitialized == 0)
|
---|
[2821] | 97 | {
|
---|
[2830] | 98 | g_fInitialized = -1;
|
---|
| 99 | break;
|
---|
[2821] | 100 | }
|
---|
[3573] | 101 | kHlpSleep(1);
|
---|
[2821] | 102 | }
|
---|
| 103 |
|
---|
| 104 | /*
|
---|
[2830] | 105 | * Do the initialization.
|
---|
[2821] | 106 | */
|
---|
[3573] | 107 | rc = kHlpHeapInit();
|
---|
[2830] | 108 | if (!rc)
|
---|
[2821] | 109 | {
|
---|
[3576] | 110 | rc = kLdrDyldSemInit();
|
---|
[2830] | 111 | if (!rc)
|
---|
| 112 | {
|
---|
[2869] | 113 | rc = kldrDyldInit();
|
---|
| 114 | if (!rc)
|
---|
| 115 | {
|
---|
| 116 | g_fInitialized = 1;
|
---|
| 117 | return 0;
|
---|
| 118 | }
|
---|
[3576] | 119 | kLdrDyldSemTerm();
|
---|
[2830] | 120 | }
|
---|
[3573] | 121 | kHlpHeapTerm();
|
---|
[2821] | 122 | }
|
---|
[2830] | 123 | g_fInitialized = -2;
|
---|
| 124 | return rc;
|
---|
| 125 | }
|
---|
[2821] | 126 |
|
---|
| 127 |
|
---|
[2830] | 128 | /**
|
---|
| 129 | * Terminates the loader.
|
---|
| 130 | */
|
---|
| 131 | void kldrTerm(void)
|
---|
| 132 | {
|
---|
| 133 | /* can't terminate unless it's initialized. */
|
---|
| 134 | if (g_fInitialized != 1)
|
---|
| 135 | return;
|
---|
| 136 | g_fInitialized = -1;
|
---|
[2821] | 137 |
|
---|
| 138 | /*
|
---|
[2830] | 139 | * Do the termination.
|
---|
[2821] | 140 | */
|
---|
[3576] | 141 | kLdrDyldSemTerm();
|
---|
[3573] | 142 | kHlpHeapTerm();
|
---|
[2821] | 143 |
|
---|
[2830] | 144 | /* done */
|
---|
| 145 | g_fInitialized = 0;
|
---|
[2821] | 146 | }
|
---|
| 147 |
|
---|