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"?>
5 <manualpage metafile="dso.xml.meta">
7 <title>Dynamic Shared Object (DSO) Support</title>
10 <p>The Apache HTTP Server is a modular program where the
11 administrator can choose the functionality to include in the
12 server by selecting a set of modules. The modules can be
13 statically compiled into the <code>httpd</code> binary when the
14 server is built. Alternatively, modules can be compiled as
15 Dynamic Shared Objects (DSOs) that exist separately from the
16 main <code>httpd</code> binary file. DSO modules may be
17 compiled at the time the server is built, or they may be
18 compiled and added at a later time using the Apache Extension
19 Tool (<a href="programs/apxs.html">apxs</a>).</p>
21 <p>This document describes how to use DSO modules as well as
22 the theory behind their use.</p>
26 <section id="implementation"><title>Implementation</title>
30 <module>mod_so</module>
33 <directive module="mod_so">LoadModule</directive>
37 <p>The DSO support for loading individual Apache modules is based
38 on a module named <module>mod_so</module> which must be statically
39 compiled into the Apache core. It is the only module besides
40 <module>core</module> which cannot be put into a DSO
41 itself. Practically all other distributed Apache modules can then
42 be placed into a DSO by individually enabling the DSO build for
43 them via <code>configure</code>'s
44 <code>--enable-<em>module</em>=shared</code> option as discussed
45 in the <a href="install.html">install documentation</a>. After a
46 module is compiled into a DSO named <code>mod_foo.so</code> you
47 can use <module>mod_so</module>'s <directive
48 module="mod_so">LoadModule</directive> command in your
49 <code>httpd.conf</code> file to load this module at server startup
52 <p>To simplify this creation of DSO files for Apache modules
53 (especially for third-party modules) a new support program
54 named <a href="programs/apxs.html">apxs</a> (<em>APache
55 eXtenSion</em>) is available. It can be used to build DSO based
56 modules <em>outside of</em> the Apache source tree. The idea is
57 simple: When installing Apache the <code>configure</code>'s
58 <code>make install</code> procedure installs the Apache C
59 header files and puts the platform-dependent compiler and
60 linker flags for building DSO files into the <code>apxs</code>
61 program. This way the user can use <code>apxs</code> to compile
62 his Apache module sources without the Apache distribution
63 source tree and without having to fiddle with the
64 platform-dependent compiler and linker flags for DSO
68 <section id="usage"><title>Usage Summary</title>
70 <p>To give you an overview of the DSO features of Apache 2.0,
71 here is a short and concise summary:</p>
75 Build and install a <em>distributed</em> Apache module, say
76 <code>mod_foo.c</code>, into its own DSO
77 <code>mod_foo.so</code>:
80 $ ./configure --prefix=/path/to/install --enable-foo=shared<br />
86 Build and install a <em>third-party</em> Apache module, say
87 <code>mod_foo.c</code>, into its own DSO
88 <code>mod_foo.so</code>:
91 $ ./configure --add-module=module_type:/path/to/3rdparty/mod_foo.c --enable-foo=shared<br />
97 Configure Apache for <em>later installation</em> of shared
101 $ ./configure --enable-so<br />
107 Build and install a <em>third-party</em> Apache module, say
108 <code>mod_foo.c</code>, into its own DSO
109 <code>mod_foo.so</code> <em>outside of</em> the Apache
110 source tree using <a href="programs/apxs.html">apxs</a>:
113 $ cd /path/to/3rdparty<br />
114 $ apxs -c mod_foo.c<br />
115 $ apxs -i -a -n foo mod_foo.la
120 <p>In all cases, once the shared module is compiled, you must
121 use a <directive module="mod_so">LoadModule</directive>
122 directive in <code>httpd.conf</code> to tell Apache to activate
126 <section id="background"><title>Background</title>
128 <p>On modern Unix derivatives there exists a nifty mechanism
129 usually called dynamic linking/loading of <em>Dynamic Shared
130 Objects</em> (DSO) which provides a way to build a piece of
131 program code in a special format for loading it at run-time
132 into the address space of an executable program.</p>
134 <p>This loading can usually be done in two ways: Automatically
135 by a system program called <code>ld.so</code> when an
136 executable program is started or manually from within the
137 executing program via a programmatic system interface to the
138 Unix loader through the system calls
139 <code>dlopen()/dlsym()</code>.</p>
141 <p>In the first way the DSO's are usually called <em>shared
142 libraries</em> or <em>DSO libraries</em> and named
143 <code>libfoo.so</code> or <code>libfoo.so.1.2</code>. They
144 reside in a system directory (usually <code>/usr/lib</code>)
145 and the link to the executable program is established at
146 build-time by specifying <code>-lfoo</code> to the linker
147 command. This hard-codes library references into the executable
148 program file so that at start-time the Unix loader is able to
149 locate <code>libfoo.so</code> in <code>/usr/lib</code>, in
150 paths hard-coded via linker-options like <code>-R</code> or in
151 paths configured via the environment variable
152 <code>LD_LIBRARY_PATH</code>. It then resolves any (yet
153 unresolved) symbols in the executable program which are
154 available in the DSO.</p>
156 <p>Symbols in the executable program are usually not referenced
157 by the DSO (because it's a reusable library of general code)
158 and hence no further resolving has to be done. The executable
159 program has no need to do anything on its own to use the
160 symbols from the DSO because the complete resolving is done by
161 the Unix loader. (In fact, the code to invoke
162 <code>ld.so</code> is part of the run-time startup code which
163 is linked into every executable program which has been bound
164 non-static). The advantage of dynamic loading of common library
165 code is obvious: the library code needs to be stored only once,
166 in a system library like <code>libc.so</code>, saving disk
167 space for every program.</p>
169 <p>In the second way the DSO's are usually called <em>shared
170 objects</em> or <em>DSO files</em> and can be named with an
171 arbitrary extension (although the canonical name is
172 <code>foo.so</code>). These files usually stay inside a
173 program-specific directory and there is no automatically
174 established link to the executable program where they are used.
175 Instead the executable program manually loads the DSO at
176 run-time into its address space via <code>dlopen()</code>. At
177 this time no resolving of symbols from the DSO for the
178 executable program is done. But instead the Unix loader
179 automatically resolves any (yet unresolved) symbols in the DSO
180 from the set of symbols exported by the executable program and
181 its already loaded DSO libraries (especially all symbols from
182 the ubiquitous <code>libc.so</code>). This way the DSO gets
183 knowledge of the executable program's symbol set as if it had
184 been statically linked with it in the first place.</p>
186 <p>Finally, to take advantage of the DSO's API the executable
187 program has to resolve particular symbols from the DSO via
188 <code>dlsym()</code> for later use inside dispatch tables
189 <em>etc.</em> In other words: The executable program has to
190 manually resolve every symbol it needs to be able to use it.
191 The advantage of such a mechanism is that optional program
192 parts need not be loaded (and thus do not spend memory) until
193 they are needed by the program in question. When required,
194 these program parts can be loaded dynamically to extend the
195 base program's functionality.</p>
197 <p>Although this DSO mechanism sounds straightforward there is
198 at least one difficult step here: The resolving of symbols from
199 the executable program for the DSO when using a DSO to extend a
200 program (the second way). Why? Because "reverse resolving" DSO
201 symbols from the executable program's symbol set is against the
202 library design (where the library has no knowledge about the
203 programs it is used by) and is neither available under all
204 platforms nor standardized. In practice the executable
205 program's global symbols are often not re-exported and thus not
206 available for use in a DSO. Finding a way to force the linker
207 to export all global symbols is the main problem one has to
208 solve when using DSO for extending a program at run-time.</p>
210 <p>The shared library approach is the typical one, because it
211 is what the DSO mechanism was designed for, hence it is used
212 for nearly all types of libraries the operating system
213 provides. On the other hand using shared objects for extending
214 a program is not used by a lot of programs.</p>
216 <p>As of 1998 there are only a few software packages available
217 which use the DSO mechanism to actually extend their
218 functionality at run-time: Perl 5 (via its XS mechanism and the
219 DynaLoader module), Netscape Server, <em>etc.</em> Starting
220 with version 1.3, Apache joined the crew, because Apache
221 already uses a module concept to extend its functionality and
222 internally uses a dispatch-list-based approach to link external
223 modules into the Apache core functionality. So, Apache is
224 really predestined for using DSO to load its modules at
228 <section id="advantages"><title>Advantages and Disadvantages</title>
230 <p>The above DSO based features have the following
234 <li>The server package is more flexible at run-time because
235 the actual server process can be assembled at run-time via
236 <directive module="mod_so">LoadModule</directive>
237 <code>httpd.conf</code> configuration commands instead of
238 <code>configure</code> options at build-time. For instance
239 this way one is able to run different server instances
240 (standard & SSL version, minimalistic & powered up
241 version [mod_perl, PHP3], <em>etc.</em>) with only one Apache
244 <li>The server package can be easily extended with
245 third-party modules even after installation. This is at least
246 a great benefit for vendor package maintainers who can create
247 a Apache core package and additional packages containing
248 extensions like PHP3, mod_perl, mod_fastcgi,
251 <li>Easier Apache module prototyping because with the
252 DSO/<code>apxs</code> pair you can both work outside the
253 Apache source tree and only need an <code>apxs -i</code>
254 command followed by an <code>apachectl restart</code> to
255 bring a new version of your currently developed module into
256 the running Apache server.</li>
259 <p>DSO has the following disadvantages:</p>
262 <li>The DSO mechanism cannot be used on every platform
263 because not all operating systems support dynamic loading of
264 code into the address space of a program.</li>
266 <li>The server is approximately 20% slower at startup time
267 because of the symbol resolving overhead the Unix loader now
270 <li>The server is approximately 5% slower at execution time
271 under some platforms because position independent code (PIC)
272 sometimes needs complicated assembler tricks for relative
273 addressing which are not necessarily as fast as absolute
276 <li>Because DSO modules cannot be linked against other
277 DSO-based libraries (<code>ld -lfoo</code>) on all platforms
278 (for instance a.out-based platforms usually don't provide
279 this functionality while ELF-based platforms do) you cannot
280 use the DSO mechanism for all types of modules. Or in other
281 words, modules compiled as DSO files are restricted to only
282 use symbols from the Apache core, from the C library
283 (<code>libc</code>) and all other dynamic or static libraries
284 used by the Apache core, or from static library archives
285 (<code>libfoo.a</code>) containing position independent code.
286 The only chances to use other code is to either make sure the
287 Apache core itself already contains a reference to it or
288 loading the code yourself via <code>dlopen()</code>.</li>