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