1 | /* $Id: kLdr.c 2944 2007-01-13 15:55:40Z bird $ */
|
---|
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 |
|
---|
27 | /** @mainpage 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 |
|
---|
48 |
|
---|
49 |
|
---|
50 | /*******************************************************************************
|
---|
51 | * Header Files *
|
---|
52 | *******************************************************************************/
|
---|
53 | #include "kLdr.h"
|
---|
54 | #include "kLdrHlp.h"
|
---|
55 | #include "kLdrInternal.h"
|
---|
56 |
|
---|
57 |
|
---|
58 | /*******************************************************************************
|
---|
59 | * Global Variables *
|
---|
60 | *******************************************************************************/
|
---|
61 | /** Flag indicating whether we've initialized the loader or not.
|
---|
62 | *
|
---|
63 | * 0 if not initialized.
|
---|
64 | * -1 if we're initializing or terminating.
|
---|
65 | * 1 if we've successfully initialized it.
|
---|
66 | * -2 if initialization failed.
|
---|
67 | */
|
---|
68 | static int volatile g_fInitialized;
|
---|
69 |
|
---|
70 |
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Initializes the loader.
|
---|
74 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
75 | */
|
---|
76 | int kldrInit(void)
|
---|
77 | {
|
---|
78 | int rc;
|
---|
79 |
|
---|
80 | /* check we're already good. */
|
---|
81 | if (g_fInitialized == 1)
|
---|
82 | return 0;
|
---|
83 |
|
---|
84 | /* a tiny serialization effort. */
|
---|
85 | for (;;)
|
---|
86 | {
|
---|
87 | if (g_fInitialized == 1)
|
---|
88 | return 0;
|
---|
89 | if (g_fInitialized == -2)
|
---|
90 | return -1;
|
---|
91 | /** @todo atomic test and set if we care. */
|
---|
92 | if (g_fInitialized == 0)
|
---|
93 | {
|
---|
94 | g_fInitialized = -1;
|
---|
95 | break;
|
---|
96 | }
|
---|
97 | kldrHlpSleep(1);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Do the initialization.
|
---|
102 | */
|
---|
103 | rc = kldrHlpHeapInit();
|
---|
104 | if (!rc)
|
---|
105 | {
|
---|
106 | rc = kldrHlpSemInit();
|
---|
107 | if (!rc)
|
---|
108 | {
|
---|
109 | rc = kldrDyldInit();
|
---|
110 | if (!rc)
|
---|
111 | {
|
---|
112 | g_fInitialized = 1;
|
---|
113 | return 0;
|
---|
114 | }
|
---|
115 | kldrHlpSemTerm();
|
---|
116 | }
|
---|
117 | kldrHlpHeapTerm();
|
---|
118 | }
|
---|
119 | g_fInitialized = -2;
|
---|
120 | return rc;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Terminates the loader.
|
---|
126 | */
|
---|
127 | void kldrTerm(void)
|
---|
128 | {
|
---|
129 | /* can't terminate unless it's initialized. */
|
---|
130 | if (g_fInitialized != 1)
|
---|
131 | return;
|
---|
132 | g_fInitialized = -1;
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * Do the termination.
|
---|
136 | */
|
---|
137 | kldrHlpSemTerm();
|
---|
138 | kldrHlpHeapTerm();
|
---|
139 |
|
---|
140 | /* done */
|
---|
141 | g_fInitialized = 0;
|
---|
142 | }
|
---|
143 |
|
---|