]> granicus.if.org Git - apache/blob - docs/manual/dso.xml
XML update.
[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.
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>
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 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> directive in your
63     <code>httpd.conf</code> file to load this module at server startup
64     or restart.</p>
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>
69
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
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 HTTP Server 2.x,
89     here is a short and concise summary:</p>
90
91     <ol>
92       <li>
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>
96
97 <example>
98 $ ./configure --prefix=/path/to/install --enable-foo<br />
99 $ make install
100 </example>
101       </li>
102
103       <li>
104       <p>Configure Apache HTTP Server with all modules enabled. Only a basic
105       set will be loaded during server startup. You can change the set of loaded
106       modules by activating or deactivating the <directive
107       module="mod_so">LoadModule</directive> directives in
108       <code>httpd.conf</code>.</p>
109
110 <example>
111 $ ./configure --enable-mods-shared=all<br />
112 $ make install
113 </example>
114       </li>
115
116       <li>
117       <p>Some modules are only useful for developers and will not be build.
118       when using the module set <em>all</em>. To build all available modules
119       including developer modules use <em>reallyall</em>. In addition the
120       <directive module="mod_so">LoadModule</directive> directives for all
121       built modules can be activated via the configure option
122       <code>--enable-load-all-modules</code>.</p>
123
124 <example>
125 $ ./configure --enable-mods-shared=reallyall --enable-load-all-modules<br />
126 $ make install
127 </example>
128       </li>
129
130       <li>
131         Build and install a <em>third-party</em> Apache httpd module, say
132         <code>mod_foo.c</code>, into its own DSO
133         <code>mod_foo.so</code> <em>outside of</em> the Apache httpd
134         source tree using <program>apxs</program>:
135
136 <example>
137 $ cd /path/to/3rdparty<br />
138 $ apxs -cia mod_foo.c
139 </example>
140       </li>
141     </ol>
142
143     <p>In all cases, once the shared module is compiled, you must
144     use a <directive module="mod_so">LoadModule</directive>
145     directive in <code>httpd.conf</code> to tell Apache httpd to activate
146     the module.</p>
147
148     <p>See the <a href="programs/apxs.html">apxs documentation</a> for more details.</p>
149 </section>
150
151 <section id="background"><title>Background</title>
152
153     <p>On modern Unix derivatives there exists a mechanism
154     called dynamic linking/loading of <em>Dynamic Shared
155     Objects</em> (DSO) which provides a way to build a piece of
156     program code in a special format for loading it at run-time
157     into the address space of an executable program.</p>
158
159     <p>This loading can usually be done in two ways: automatically
160     by a system program called <code>ld.so</code> when an
161     executable program is started or manually from within the
162     executing program via a programmatic system interface to the
163     Unix loader through the system calls
164     <code>dlopen()/dlsym()</code>.</p>
165
166     <p>In the first way the DSO's are usually called <em>shared
167     libraries</em> or <em>DSO libraries</em> and named
168     <code>libfoo.so</code> or <code>libfoo.so.1.2</code>. They
169     reside in a system directory (usually <code>/usr/lib</code>)
170     and the link to the executable program is established at
171     build-time by specifying <code>-lfoo</code> to the linker
172     command. This hard-codes library references into the executable
173     program file so that at start-time the Unix loader is able to
174     locate <code>libfoo.so</code> in <code>/usr/lib</code>, in
175     paths hard-coded via linker-options like <code>-R</code> or in
176     paths configured via the environment variable
177     <code>LD_LIBRARY_PATH</code>. It then resolves any (yet
178     unresolved) symbols in the executable program which are
179     available in the DSO.</p>
180
181     <p>Symbols in the executable program are usually not referenced
182     by the DSO (because it's a reusable library of general code)
183     and hence no further resolving has to be done. The executable
184     program has no need to do anything on its own to use the
185     symbols from the DSO because the complete resolving is done by
186     the Unix loader. (In fact, the code to invoke
187     <code>ld.so</code> is part of the run-time startup code which
188     is linked into every executable program which has been bound
189     non-static). The advantage of dynamic loading of common library
190     code is obvious: the library code needs to be stored only once,
191     in a system library like <code>libc.so</code>, saving disk
192     space for every program.</p>
193
194     <p>In the second way the DSO's are usually called <em>shared
195     objects</em> or <em>DSO files</em> and can be named with an
196     arbitrary extension (although the canonical name is
197     <code>foo.so</code>). These files usually stay inside a
198     program-specific directory and there is no automatically
199     established link to the executable program where they are used.
200     Instead the executable program manually loads the DSO at
201     run-time into its address space via <code>dlopen()</code>. At
202     this time no resolving of symbols from the DSO for the
203     executable program is done. But instead the Unix loader
204     automatically resolves any (yet unresolved) symbols in the DSO
205     from the set of symbols exported by the executable program and
206     its already loaded DSO libraries (especially all symbols from
207     the ubiquitous <code>libc.so</code>). This way the DSO gets
208     knowledge of the executable program's symbol set as if it had
209     been statically linked with it in the first place.</p>
210
211     <p>Finally, to take advantage of the DSO's API the executable
212     program has to resolve particular symbols from the DSO via
213     <code>dlsym()</code> for later use inside dispatch tables
214     <em>etc.</em> In other words: The executable program has to
215     manually resolve every symbol it needs to be able to use it.
216     The advantage of such a mechanism is that optional program
217     parts need not be loaded (and thus do not spend memory) until
218     they are needed by the program in question. When required,
219     these program parts can be loaded dynamically to extend the
220     base program's functionality.</p>
221
222     <p>Although this DSO mechanism sounds straightforward there is
223     at least one difficult step here: The resolving of symbols from
224     the executable program for the DSO when using a DSO to extend a
225     program (the second way). Why? Because "reverse resolving" DSO
226     symbols from the executable program's symbol set is against the
227     library design (where the library has no knowledge about the
228     programs it is used by) and is neither available under all
229     platforms nor standardized. In practice the executable
230     program's global symbols are often not re-exported and thus not
231     available for use in a DSO. Finding a way to force the linker
232     to export all global symbols is the main problem one has to
233     solve when using DSO for extending a program at run-time.</p>
234
235     <p>The shared library approach is the typical one, because it
236     is what the DSO mechanism was designed for, hence it is used
237     for nearly all types of libraries the operating system
238     provides.</p>
239
240 </section>
241
242 <section id="advantages"><title>Advantages and Disadvantages</title>
243
244     <p>The above DSO based features have the following
245     advantages:</p>
246
247     <ul>
248       <li>The server package is more flexible at run-time because
249       the server process can be assembled at run-time via
250       <directive module="mod_so">LoadModule</directive>
251       <code>httpd.conf</code> configuration directives instead of
252       <program>configure</program> options at build-time. For instance,
253       this way one is able to run different server instances
254       (standard &amp; SSL version, minimalistic &amp; dynamic
255       version [mod_perl, mod_php], <em>etc.</em>) with only one Apache httpd
256       installation.</li>
257
258       <li>The server package can be easily extended with
259       third-party modules even after installation. This is
260       a great benefit for vendor package maintainers, who can create
261       an Apache httpd core package and additional packages containing
262       extensions like PHP, mod_perl, mod_security, <em>etc.</em></li>
263
264       <li>Easier Apache httpd module prototyping, because with the
265       DSO/<program>apxs</program> pair you can both work outside the
266       Apache httpd source tree and only need an <code>apxs -i</code>
267       command followed by an <code>apachectl restart</code> to
268       bring a new version of your currently developed module into
269       the running Apache HTTP Server.</li>
270     </ul>
271
272     <p>DSO has the following disadvantages:</p>
273
274     <ul>
275       <li>The server is approximately 20% slower at startup time
276       because of the symbol resolving overhead the Unix loader now
277       has to do.</li>
278
279       <li>The server is approximately 5% slower at execution time
280       under some platforms, because position independent code (PIC)
281       sometimes needs complicated assembler tricks for relative
282       addressing, which are not necessarily as fast as absolute
283       addressing.</li>
284
285       <li>Because DSO modules cannot be linked against other
286       DSO-based libraries (<code>ld -lfoo</code>) on all platforms
287       (for instance a.out-based platforms usually don't provide
288       this functionality while ELF-based platforms do) you cannot
289       use the DSO mechanism for all types of modules. Or in other
290       words, modules compiled as DSO files are restricted to only
291       use symbols from the Apache httpd core, from the C library
292       (<code>libc</code>) and all other dynamic or static libraries
293       used by the Apache httpd core, or from static library archives
294       (<code>libfoo.a</code>) containing position independent code.
295       The only chances to use other code is to either make sure the
296       httpd core itself already contains a reference to it or
297       loading the code yourself via <code>dlopen()</code>.</li>
298     </ul>
299
300 </section>
301
302 </manualpage>