1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE manualpage SYSTEM "./style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="./style/manual.en.xsl"?>
7 Copyright 2002-2004 The Apache Software Foundation
9 Licensed under the Apache License, Version 2.0 (the "License");
10 you may not use this file except in compliance with the License.
11 You may obtain a copy of the License at
13 http://www.apache.org/licenses/LICENSE-2.0
15 Unless required by applicable law or agreed to in writing, software
16 distributed under the License is distributed on an "AS IS" BASIS,
17 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 See the License for the specific language governing permissions and
19 limitations under the License.
22 <manualpage metafile="dso.xml.meta">
24 <title>Dynamic Shared Object (DSO) Support</title>
27 <p>The Apache HTTP Server is a modular program where the
28 administrator can choose the functionality to include in the
29 server by selecting a set of modules. The modules can be
30 statically compiled into the <code>httpd</code> binary when the
31 server is built. Alternatively, modules can be compiled as
32 Dynamic Shared Objects (DSOs) that exist separately from the
33 main <code>httpd</code> binary file. DSO modules may be
34 compiled at the time the server is built, or they may be
35 compiled and added at a later time using the Apache Extension
36 Tool (<a href="programs/apxs.html">apxs</a>).</p>
38 <p>This document describes how to use DSO modules as well as
39 the theory behind their use.</p>
43 <section id="implementation"><title>Implementation</title>
47 <module>mod_so</module>
50 <directive module="mod_so">LoadModule</directive>
54 <p>The DSO support for loading individual Apache modules is based
55 on a module named <module>mod_so</module> which must be statically
56 compiled into the Apache core. It is the only module besides
57 <module>core</module> which cannot be put into a DSO
58 itself. Practically all other distributed Apache modules can then
59 be placed into a DSO by individually enabling the DSO build for
60 them via <code>configure</code>'s
61 <code>--enable-<em>module</em>=shared</code> option as discussed
62 in the <a href="install.html">install documentation</a>. After a
63 module is compiled into a DSO named <code>mod_foo.so</code> you
64 can use <module>mod_so</module>'s <directive
65 module="mod_so">LoadModule</directive> command in your
66 <code>httpd.conf</code> file to load this module at server startup
69 <p>To simplify this creation of DSO files for Apache modules
70 (especially for third-party modules) a new support program
71 named <a href="programs/apxs.html">apxs</a> (<em>APache
72 eXtenSion</em>) is available. It can be used to build DSO based
73 modules <em>outside of</em> the Apache source tree. The idea is
74 simple: When installing Apache the <code>configure</code>'s
75 <code>make install</code> procedure installs the Apache C
76 header files and puts the platform-dependent compiler and
77 linker flags for building DSO files into the <code>apxs</code>
78 program. This way the user can use <code>apxs</code> to compile
79 his Apache module sources without the Apache distribution
80 source tree and without having to fiddle with the
81 platform-dependent compiler and linker flags for DSO
85 <section id="usage"><title>Usage Summary</title>
87 <p>To give you an overview of the DSO features of Apache 2.0,
88 here is a short and concise summary:</p>
92 Build and install a <em>distributed</em> Apache module, say
93 <code>mod_foo.c</code>, into its own DSO
94 <code>mod_foo.so</code>:
97 $ ./configure --prefix=/path/to/install --enable-foo=shared<br />
103 Build and install a <em>third-party</em> Apache module, say
104 <code>mod_foo.c</code>, into its own DSO
105 <code>mod_foo.so</code>:
108 $ ./configure --add-module=module_type:/path/to/3rdparty/mod_foo.c --enable-foo=shared<br />
114 Configure Apache for <em>later installation</em> of shared
118 $ ./configure --enable-so<br />
124 Build and install a <em>third-party</em> Apache module, say
125 <code>mod_foo.c</code>, into its own DSO
126 <code>mod_foo.so</code> <em>outside of</em> the Apache
127 source tree using <a href="programs/apxs.html">apxs</a>:
130 $ cd /path/to/3rdparty<br />
131 $ apxs -c mod_foo.c<br />
132 $ apxs -i -a -n foo mod_foo.la
137 <p>In all cases, once the shared module is compiled, you must
138 use a <directive module="mod_so">LoadModule</directive>
139 directive in <code>httpd.conf</code> to tell Apache to activate
143 <section id="background"><title>Background</title>
145 <p>On modern Unix derivatives there exists a nifty mechanism
146 usually called dynamic linking/loading of <em>Dynamic Shared
147 Objects</em> (DSO) which provides a way to build a piece of
148 program code in a special format for loading it at run-time
149 into the address space of an executable program.</p>
151 <p>This loading can usually be done in two ways: Automatically
152 by a system program called <code>ld.so</code> when an
153 executable program is started or manually from within the
154 executing program via a programmatic system interface to the
155 Unix loader through the system calls
156 <code>dlopen()/dlsym()</code>.</p>
158 <p>In the first way the DSO's are usually called <em>shared
159 libraries</em> or <em>DSO libraries</em> and named
160 <code>libfoo.so</code> or <code>libfoo.so.1.2</code>. They
161 reside in a system directory (usually <code>/usr/lib</code>)
162 and the link to the executable program is established at
163 build-time by specifying <code>-lfoo</code> to the linker
164 command. This hard-codes library references into the executable
165 program file so that at start-time the Unix loader is able to
166 locate <code>libfoo.so</code> in <code>/usr/lib</code>, in
167 paths hard-coded via linker-options like <code>-R</code> or in
168 paths configured via the environment variable
169 <code>LD_LIBRARY_PATH</code>. It then resolves any (yet
170 unresolved) symbols in the executable program which are
171 available in the DSO.</p>
173 <p>Symbols in the executable program are usually not referenced
174 by the DSO (because it's a reusable library of general code)
175 and hence no further resolving has to be done. The executable
176 program has no need to do anything on its own to use the
177 symbols from the DSO because the complete resolving is done by
178 the Unix loader. (In fact, the code to invoke
179 <code>ld.so</code> is part of the run-time startup code which
180 is linked into every executable program which has been bound
181 non-static). The advantage of dynamic loading of common library
182 code is obvious: the library code needs to be stored only once,
183 in a system library like <code>libc.so</code>, saving disk
184 space for every program.</p>
186 <p>In the second way the DSO's are usually called <em>shared
187 objects</em> or <em>DSO files</em> and can be named with an
188 arbitrary extension (although the canonical name is
189 <code>foo.so</code>). These files usually stay inside a
190 program-specific directory and there is no automatically
191 established link to the executable program where they are used.
192 Instead the executable program manually loads the DSO at
193 run-time into its address space via <code>dlopen()</code>. At
194 this time no resolving of symbols from the DSO for the
195 executable program is done. But instead the Unix loader
196 automatically resolves any (yet unresolved) symbols in the DSO
197 from the set of symbols exported by the executable program and
198 its already loaded DSO libraries (especially all symbols from
199 the ubiquitous <code>libc.so</code>). This way the DSO gets
200 knowledge of the executable program's symbol set as if it had
201 been statically linked with it in the first place.</p>
203 <p>Finally, to take advantage of the DSO's API the executable
204 program has to resolve particular symbols from the DSO via
205 <code>dlsym()</code> for later use inside dispatch tables
206 <em>etc.</em> In other words: The executable program has to
207 manually resolve every symbol it needs to be able to use it.
208 The advantage of such a mechanism is that optional program
209 parts need not be loaded (and thus do not spend memory) until
210 they are needed by the program in question. When required,
211 these program parts can be loaded dynamically to extend the
212 base program's functionality.</p>
214 <p>Although this DSO mechanism sounds straightforward there is
215 at least one difficult step here: The resolving of symbols from
216 the executable program for the DSO when using a DSO to extend a
217 program (the second way). Why? Because "reverse resolving" DSO
218 symbols from the executable program's symbol set is against the
219 library design (where the library has no knowledge about the
220 programs it is used by) and is neither available under all
221 platforms nor standardized. In practice the executable
222 program's global symbols are often not re-exported and thus not
223 available for use in a DSO. Finding a way to force the linker
224 to export all global symbols is the main problem one has to
225 solve when using DSO for extending a program at run-time.</p>
227 <p>The shared library approach is the typical one, because it
228 is what the DSO mechanism was designed for, hence it is used
229 for nearly all types of libraries the operating system
230 provides. On the other hand using shared objects for extending
231 a program is not used by a lot of programs.</p>
233 <p>As of 1998 there are only a few software packages available
234 which use the DSO mechanism to actually extend their
235 functionality at run-time: Perl 5 (via its XS mechanism and the
236 DynaLoader module), Netscape Server, <em>etc.</em> Starting
237 with version 1.3, Apache joined the crew, because Apache
238 already uses a module concept to extend its functionality and
239 internally uses a dispatch-list-based approach to link external
240 modules into the Apache core functionality. So, Apache is
241 really predestined for using DSO to load its modules at
245 <section id="advantages"><title>Advantages and Disadvantages</title>
247 <p>The above DSO based features have the following
251 <li>The server package is more flexible at run-time because
252 the actual server process can be assembled at run-time via
253 <directive module="mod_so">LoadModule</directive>
254 <code>httpd.conf</code> configuration commands instead of
255 <code>configure</code> options at build-time. For instance
256 this way one is able to run different server instances
257 (standard & SSL version, minimalistic & powered up
258 version [mod_perl, PHP3], <em>etc.</em>) with only one Apache
261 <li>The server package can be easily extended with
262 third-party modules even after installation. This is at least
263 a great benefit for vendor package maintainers who can create
264 a Apache core package and additional packages containing
265 extensions like PHP3, mod_perl, mod_fastcgi,
268 <li>Easier Apache module prototyping because with the
269 DSO/<code>apxs</code> pair you can both work outside the
270 Apache source tree and only need an <code>apxs -i</code>
271 command followed by an <code>apachectl restart</code> to
272 bring a new version of your currently developed module into
273 the running Apache server.</li>
276 <p>DSO has the following disadvantages:</p>
279 <li>The DSO mechanism cannot be used on every platform
280 because not all operating systems support dynamic loading of
281 code into the address space of a program.</li>
283 <li>The server is approximately 20% slower at startup time
284 because of the symbol resolving overhead the Unix loader now
287 <li>The server is approximately 5% slower at execution time
288 under some platforms because position independent code (PIC)
289 sometimes needs complicated assembler tricks for relative
290 addressing which are not necessarily as fast as absolute
293 <li>Because DSO modules cannot be linked against other
294 DSO-based libraries (<code>ld -lfoo</code>) on all platforms
295 (for instance a.out-based platforms usually don't provide
296 this functionality while ELF-based platforms do) you cannot
297 use the DSO mechanism for all types of modules. Or in other
298 words, modules compiled as DSO files are restricted to only
299 use symbols from the Apache core, from the C library
300 (<code>libc</code>) and all other dynamic or static libraries
301 used by the Apache core, or from static library archives
302 (<code>libfoo.a</code>) containing position independent code.
303 The only chances to use other code is to either make sure the
304 Apache core itself already contains a reference to it or
305 loading the code yourself via <code>dlopen()</code>.</li>