1 | /* $Id: kLdr.c 2 2007-11-16 16:07:14Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kLdr - The Dynamic Loader.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2006-2007 knut st. osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * This file is part of kStuff.
|
---|
10 | *
|
---|
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.
|
---|
15 | *
|
---|
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.
|
---|
21 | *
|
---|
22 | * kStuff is distributed in the hope that it will be useful,
|
---|
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
25 | * Lesser General Public License for more details.
|
---|
26 | *
|
---|
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
|
---|
31 | */
|
---|
32 |
|
---|
33 |
|
---|
34 | /** @mainpage kLdr - The Dynamic Loader
|
---|
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 |
|
---|
55 | /*******************************************************************************
|
---|
56 | * Header Files *
|
---|
57 | *******************************************************************************/
|
---|
58 | #include <k/kLdr.h>
|
---|
59 | #include "kLdrInternal.h"
|
---|
60 |
|
---|
61 |
|
---|
62 | /*******************************************************************************
|
---|
63 | * Global Variables *
|
---|
64 | *******************************************************************************/
|
---|
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;
|
---|
73 |
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Initializes the loader.
|
---|
78 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
79 | */
|
---|
80 | int kldrInit(void)
|
---|
81 | {
|
---|
82 | int rc;
|
---|
83 |
|
---|
84 | /* check we're already good. */
|
---|
85 | if (g_fInitialized == 1)
|
---|
86 | return 0;
|
---|
87 |
|
---|
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)
|
---|
97 | {
|
---|
98 | g_fInitialized = -1;
|
---|
99 | break;
|
---|
100 | }
|
---|
101 | kHlpSleep(1);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Do the initialization.
|
---|
106 | */
|
---|
107 | rc = kHlpHeapInit();
|
---|
108 | if (!rc)
|
---|
109 | {
|
---|
110 | rc = kLdrDyldSemInit();
|
---|
111 | if (!rc)
|
---|
112 | {
|
---|
113 | rc = kldrDyldInit();
|
---|
114 | if (!rc)
|
---|
115 | {
|
---|
116 | g_fInitialized = 1;
|
---|
117 | return 0;
|
---|
118 | }
|
---|
119 | kLdrDyldSemTerm();
|
---|
120 | }
|
---|
121 | kHlpHeapTerm();
|
---|
122 | }
|
---|
123 | g_fInitialized = -2;
|
---|
124 | return rc;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
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;
|
---|
137 |
|
---|
138 | /*
|
---|
139 | * Do the termination.
|
---|
140 | */
|
---|
141 | kLdrDyldSemTerm();
|
---|
142 | kHlpHeapTerm();
|
---|
143 |
|
---|
144 | /* done */
|
---|
145 | g_fInitialized = 0;
|
---|
146 | }
|
---|
147 |
|
---|