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