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"?>
4 <!-- $LastChangedRevision$ -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <manualpage metafile="dso.xml.meta">
25 <title>Dynamic Shared Object (DSO) Support</title>
28 <p>The Apache HTTP Server is a modular program where the
29 administrator can choose the functionality to include in the
30 server by selecting a set of modules.
31 Modules will be compiled as Dynamic Shared Objects (DSOs)
32 that exist separately from the main <program>httpd</program>
33 binary file. DSO modules may be compiled at the time the server
34 is built, or they may be compiled and added at a later time
35 using the Apache Extension Tool (<program>apxs</program>).</p>
36 <p>Alternatively, the modules can be statically compiled into
37 the <program>httpd</program> binary when the server is built.</p>
39 <p>This document describes how to use DSO modules as well as
40 the theory behind their use.</p>
44 <section id="implementation"><title>Implementation</title>
48 <module>mod_so</module>
51 <directive module="mod_so">LoadModule</directive>
55 <p>The DSO support for loading individual Apache httpd modules is based
56 on a module named <module>mod_so</module> which must be statically
57 compiled into the Apache httpd core. It is the only module besides
58 <module>core</module> which cannot be put into a DSO
59 itself. Practically all other distributed Apache httpd modules will then
60 be placed into a DSO. After a module is compiled into a DSO named
61 <code>mod_foo.so</code> you can use <module>mod_so</module>'s <directive
62 module="mod_so">LoadModule</directive> command in your
63 <code>httpd.conf</code> file to load this module at server startup
65 <p>The DSO builds for individual modules can be disabled via
66 <program>configure</program>'s <code>--enable-mods-static</code>
67 option as discussed in the <a href="install.html">install
68 documentation</a>.</p>
70 <p>To simplify this creation of DSO files for Apache httpd modules
71 (especially for third-party modules) a support program
72 named <program>apxs</program> (<dfn>APache
73 eXtenSion</dfn>) is available. It can be used to build DSO based
74 modules <em>outside of</em> the Apache httpd source tree. The idea is
75 simple: When installing Apache HTTP Server the <program>configure</program>'s
76 <code>make install</code> procedure installs the Apache httpd C
77 header files and puts the platform-dependent compiler and
78 linker flags for building DSO files into the <program>apxs</program>
79 program. This way the user can use <program>apxs</program> to compile
80 his Apache httpd module sources without the Apache httpd distribution
81 source tree and without having to fiddle with the
82 platform-dependent compiler and linker flags for DSO
86 <section id="usage"><title>Usage Summary</title>
88 <p>To give you an overview of the DSO features of Apache HTTP Server 2.x,
89 here is a short and concise summary:</p>
93 <p>Build and install a <em>distributed</em> Apache httpd module, say
94 <code>mod_foo.c</code>, into its own DSO
95 <code>mod_foo.so</code>:</p>
98 $ ./configure --prefix=/path/to/install --enable-foo<br />
104 <p>Configure Apache HTTP Server with all modules enabled, and loaded
105 as shared objects. You can then remove individual ones by
106 commenting out the <directive
107 module="mod_so">LoadModule</directive> directives in
108 <code>httpd.conf</code>.</p>
111 $ ./configure --enable-mods-shared=all<br />
117 Build and install a <em>third-party</em> Apache httpd module, say
118 <code>mod_foo.c</code>, into its own DSO
119 <code>mod_foo.so</code> <em>outside of</em> the Apache httpd
120 source tree using <program>apxs</program>:
123 $ cd /path/to/3rdparty<br />
124 $ apxs -cia mod_foo.c
129 <p>In all cases, once the shared module is compiled, you must
130 use a <directive module="mod_so">LoadModule</directive>
131 directive in <code>httpd.conf</code> to tell Apache httpd to activate
134 <p>See the <a href="programs/apxs.html">apxs documentation</a> for more details.</p>
137 <section id="background"><title>Background</title>
139 <p>On modern Unix derivatives there exists a nifty mechanism
140 usually called dynamic linking/loading of <em>Dynamic Shared
141 Objects</em> (DSO) which provides a way to build a piece of
142 program code in a special format for loading it at run-time
143 into the address space of an executable program.</p>
145 <p>This loading can usually be done in two ways: Automatically
146 by a system program called <code>ld.so</code> when an
147 executable program is started or manually from within the
148 executing program via a programmatic system interface to the
149 Unix loader through the system calls
150 <code>dlopen()/dlsym()</code>.</p>
152 <p>In the first way the DSO's are usually called <em>shared
153 libraries</em> or <em>DSO libraries</em> and named
154 <code>libfoo.so</code> or <code>libfoo.so.1.2</code>. They
155 reside in a system directory (usually <code>/usr/lib</code>)
156 and the link to the executable program is established at
157 build-time by specifying <code>-lfoo</code> to the linker
158 command. This hard-codes library references into the executable
159 program file so that at start-time the Unix loader is able to
160 locate <code>libfoo.so</code> in <code>/usr/lib</code>, in
161 paths hard-coded via linker-options like <code>-R</code> or in
162 paths configured via the environment variable
163 <code>LD_LIBRARY_PATH</code>. It then resolves any (yet
164 unresolved) symbols in the executable program which are
165 available in the DSO.</p>
167 <p>Symbols in the executable program are usually not referenced
168 by the DSO (because it's a reusable library of general code)
169 and hence no further resolving has to be done. The executable
170 program has no need to do anything on its own to use the
171 symbols from the DSO because the complete resolving is done by
172 the Unix loader. (In fact, the code to invoke
173 <code>ld.so</code> is part of the run-time startup code which
174 is linked into every executable program which has been bound
175 non-static). The advantage of dynamic loading of common library
176 code is obvious: the library code needs to be stored only once,
177 in a system library like <code>libc.so</code>, saving disk
178 space for every program.</p>
180 <p>In the second way the DSO's are usually called <em>shared
181 objects</em> or <em>DSO files</em> and can be named with an
182 arbitrary extension (although the canonical name is
183 <code>foo.so</code>). These files usually stay inside a
184 program-specific directory and there is no automatically
185 established link to the executable program where they are used.
186 Instead the executable program manually loads the DSO at
187 run-time into its address space via <code>dlopen()</code>. At
188 this time no resolving of symbols from the DSO for the
189 executable program is done. But instead the Unix loader
190 automatically resolves any (yet unresolved) symbols in the DSO
191 from the set of symbols exported by the executable program and
192 its already loaded DSO libraries (especially all symbols from
193 the ubiquitous <code>libc.so</code>). This way the DSO gets
194 knowledge of the executable program's symbol set as if it had
195 been statically linked with it in the first place.</p>
197 <p>Finally, to take advantage of the DSO's API the executable
198 program has to resolve particular symbols from the DSO via
199 <code>dlsym()</code> for later use inside dispatch tables
200 <em>etc.</em> In other words: The executable program has to
201 manually resolve every symbol it needs to be able to use it.
202 The advantage of such a mechanism is that optional program
203 parts need not be loaded (and thus do not spend memory) until
204 they are needed by the program in question. When required,
205 these program parts can be loaded dynamically to extend the
206 base program's functionality.</p>
208 <p>Although this DSO mechanism sounds straightforward there is
209 at least one difficult step here: The resolving of symbols from
210 the executable program for the DSO when using a DSO to extend a
211 program (the second way). Why? Because "reverse resolving" DSO
212 symbols from the executable program's symbol set is against the
213 library design (where the library has no knowledge about the
214 programs it is used by) and is neither available under all
215 platforms nor standardized. In practice the executable
216 program's global symbols are often not re-exported and thus not
217 available for use in a DSO. Finding a way to force the linker
218 to export all global symbols is the main problem one has to
219 solve when using DSO for extending a program at run-time.</p>
221 <p>The shared library approach is the typical one, because it
222 is what the DSO mechanism was designed for, hence it is used
223 for nearly all types of libraries the operating system
224 provides. On the other hand using shared objects for extending
225 a program is not used by a lot of programs.</p>
229 <section id="advantages"><title>Advantages and Disadvantages</title>
231 <p>The above DSO based features have the following
235 <li>The server package is more flexible at run-time because
236 the actual server process can be assembled at run-time via
237 <directive module="mod_so">LoadModule</directive>
238 <code>httpd.conf</code> configuration commands instead of
239 <program>configure</program> options at build-time. For instance
240 this way one is able to run different server instances
241 (standard & SSL version, minimalistic & powered up
242 version [mod_perl, PHP3], <em>etc.</em>) with only one Apache httpd
245 <li>The server package can be easily extended with
246 third-party modules even after installation. This is at least
247 a great benefit for vendor package maintainers who can create
248 a Apache httpd core package and additional packages containing
249 extensions like PHP, mod_perl, mod_security,
252 <li>Easier Apache httpd module prototyping because with the
253 DSO/<program>apxs</program> pair you can both work outside the
254 Apache httpd source tree and only need an <code>apxs -i</code>
255 command followed by an <code>apachectl restart</code> to
256 bring a new version of your currently developed module into
257 the running Apache HTTP Server.</li>
260 <p>DSO has the following disadvantages:</p>
263 <li>The DSO mechanism cannot be used on every platform
264 because not all operating systems support dynamic loading of
265 code into the address space of a program.</li>
267 <li>The server is approximately 20% slower at startup time
268 because of the symbol resolving overhead the Unix loader now
271 <li>The server is approximately 5% slower at execution time
272 under some platforms because position independent code (PIC)
273 sometimes needs complicated assembler tricks for relative
274 addressing which are not necessarily as fast as absolute
277 <li>Because DSO modules cannot be linked against other
278 DSO-based libraries (<code>ld -lfoo</code>) on all platforms
279 (for instance a.out-based platforms usually don't provide
280 this functionality while ELF-based platforms do) you cannot
281 use the DSO mechanism for all types of modules. Or in other
282 words, modules compiled as DSO files are restricted to only
283 use symbols from the Apache httpd core, from the C library
284 (<code>libc</code>) and all other dynamic or static libraries
285 used by the Apache httpd core, or from static library archives
286 (<code>libfoo.a</code>) containing position independent code.
287 The only chances to use other code is to either make sure the
288 httpd core itself already contains a reference to it or
289 loading the code yourself via <code>dlopen()</code>.</li>