1<?xml version="1.0" encoding="ISO-8859-1"?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
4        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5              This file is generated from xml source: DO NOT EDIT
6        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7      -->
8<title>Dynamic Shared Object (DSO) Support - Apache HTTP Server</title>
9<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
10<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
11<link href="/style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="/style/css/prettify.css" />
12<script src="/style/scripts/prettify.js" type="text/javascript">
13</script>
14
15<link href="/images/favicon.ico" rel="shortcut icon" /></head>
16<body id="manual-page"><div id="page-header">
17<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p>
18<p class="apache">Apache HTTP Server Version 2.2</p>
19<img alt="" src="/images/feather.gif" /></div>
20<div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="/images/left.gif" /></a></div>
21<div id="path">
22<a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="./">Version 2.2</a></div><div id="page-content"><div id="preamble"><h1>Dynamic Shared Object (DSO) Support</h1>
23<div class="toplang">
24<p><span>Available Languages: </span><a href="/en/dso.html" title="English">&nbsp;en&nbsp;</a> |
25<a href="/ja/dso.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
26<a href="/ko/dso.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a> |
27<a href="/tr/dso.html" hreflang="tr" rel="alternate" title="T�rk�e">&nbsp;tr&nbsp;</a></p>
28</div>
29
30    <p>The Apache HTTP Server is a modular program where the
31    administrator can choose the functionality to include in the
32    server by selecting a set of modules. The modules can be
33    statically compiled into the <code class="program"><a href="/programs/httpd.html">httpd</a></code> binary when the
34    server is built. Alternatively, modules can be compiled as
35    Dynamic Shared Objects (DSOs) that exist separately from the
36    main <code class="program"><a href="/programs/httpd.html">httpd</a></code> binary file. DSO modules may be
37    compiled at the time the server is built, or they may be
38    compiled and added at a later time using the Apache Extension
39    Tool (<code class="program"><a href="/programs/apxs.html">apxs</a></code>).</p>
40
41    <p>This document describes how to use DSO modules as well as
42    the theory behind their use.</p>
43  </div>
44<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#implementation">Implementation</a></li>
45<li><img alt="" src="/images/down.gif" /> <a href="#usage">Usage Summary</a></li>
46<li><img alt="" src="/images/down.gif" /> <a href="#background">Background</a></li>
47<li><img alt="" src="/images/down.gif" /> <a href="#advantages">Advantages and Disadvantages</a></li>
48</ul><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
49<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
50<div class="section">
51<h2><a name="implementation" id="implementation">Implementation</a></h2>
52
53<table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="/mod/mod_so.html">mod_so</a></code></li></ul></td><td><ul><li><code class="directive"><a href="/mod/mod_so.html#loadmodule">LoadModule</a></code></li></ul></td></tr></table>
54
55    <p>The DSO support for loading individual Apache modules is based
56    on a module named <code class="module"><a href="/mod/mod_so.html">mod_so</a></code> which must be statically
57    compiled into the Apache core. It is the only module besides
58    <code class="module"><a href="/mod/core.html">core</a></code> 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 <code class="program"><a href="/programs/configure.html">configure</a></code>'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 <code class="module"><a href="/mod/mod_so.html">mod_so</a></code>'s <code class="directive"><a href="/mod/mod_so.html#loadmodule">LoadModule</a></code> command in your
66    <code>httpd.conf</code> file to load this module at server startup
67    or restart.</p>
68
69    <p>To simplify this creation of DSO files for Apache modules
70    (especially for third-party modules) a new support program
71    named <code class="program"><a href="/programs/apxs.html">apxs</a></code> (<dfn>APache
72    eXtenSion</dfn>) 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 class="program"><a href="/programs/configure.html">configure</a></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 class="program"><a href="/programs/apxs.html">apxs</a></code>
78    program. This way the user can use <code class="program"><a href="/programs/apxs.html">apxs</a></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
82    support.</p>
83</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
84<div class="section">
85<h2><a name="usage" id="usage">Usage Summary</a></h2>
86
87    <p>To give you an overview of the DSO features of Apache 2.x,
88    here is a short and concise summary:</p>
89
90    <ol>
91      <li>
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>: 
95
96<div class="example"><p><code>
97$ /configure --prefix=/path/to/install --enable-foo=shared<br />
98$ make install
99</code></p></div>
100      </li>
101
102      <li>
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>: 
106
107<div class="example"><p><code>
108$ /configure --add-module=<var>module_type</var>:/path/to/3rdparty/mod_foo.c \<br />
109<span class="indent">
110  --enable-foo=shared<br />
111</span>
112$ make install
113</code></p></div>
114      </li>
115
116      <li>
117        Configure Apache for <em>later installation</em> of shared
118        modules: 
119
120<div class="example"><p><code>
121$ /configure --enable-so<br />
122$ make install
123</code></p></div>
124      </li>
125
126      <li>
127        Build and install a <em>third-party</em> Apache module, say
128        <code>mod_foo.c</code>, into its own DSO
129        <code>mod_foo.so</code> <em>outside of</em> the Apache
130        source tree using <code class="program"><a href="/programs/apxs.html">apxs</a></code>: 
131
132<div class="example"><p><code>
133$ cd /path/to/3rdparty<br />
134$ apxs -c mod_foo.c<br />
135$ apxs -i -a -n foo mod_foo.la
136</code></p></div>
137      </li>
138    </ol>
139
140    <p>In all cases, once the shared module is compiled, you must
141    use a <code class="directive"><a href="/mod/mod_so.html#loadmodule">LoadModule</a></code>
142    directive in <code>httpd.conf</code> to tell Apache to activate
143    the module.</p>
144</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
145<div class="section">
146<h2><a name="background" id="background">Background</a></h2>
147
148    <p>On modern Unix derivatives there exists a nifty mechanism
149    usually called dynamic linking/loading of <em>Dynamic Shared
150    Objects</em> (DSO) which provides a way to build a piece of
151    program code in a special format for loading it at run-time
152    into the address space of an executable program.</p>
153
154    <p>This loading can usually be done in two ways: Automatically
155    by a system program called <code>ld.so</code> when an
156    executable program is started or manually from within the
157    executing program via a programmatic system interface to the
158    Unix loader through the system calls
159    <code>dlopen()/dlsym()</code>.</p>
160
161    <p>In the first way the DSO's are usually called <em>shared
162    libraries</em> or <em>DSO libraries</em> and named
163    <code>libfoo.so</code> or <code>libfoo.so.1.2</code>. They
164    reside in a system directory (usually <code>/usr/lib</code>)
165    and the link to the executable program is established at
166    build-time by specifying <code>-lfoo</code> to the linker
167    command. This hard-codes library references into the executable
168    program file so that at start-time the Unix loader is able to
169    locate <code>libfoo.so</code> in <code>/usr/lib</code>, in
170    paths hard-coded via linker-options like <code>-R</code> or in
171    paths configured via the environment variable
172    <code>LD_LIBRARY_PATH</code>. It then resolves any (yet
173    unresolved) symbols in the executable program which are
174    available in the DSO.</p>
175
176    <p>Symbols in the executable program are usually not referenced
177    by the DSO (because it's a reusable library of general code)
178    and hence no further resolving has to be done. The executable
179    program has no need to do anything on its own to use the
180    symbols from the DSO because the complete resolving is done by
181    the Unix loader. (In fact, the code to invoke
182    <code>ld.so</code> is part of the run-time startup code which
183    is linked into every executable program which has been bound
184    non-static). The advantage of dynamic loading of common library
185    code is obvious: the library code needs to be stored only once,
186    in a system library like <code>libc.so</code>, saving disk
187    space for every program.</p>
188
189    <p>In the second way the DSO's are usually called <em>shared
190    objects</em> or <em>DSO files</em> and can be named with an
191    arbitrary extension (although the canonical name is
192    <code>foo.so</code>). These files usually stay inside a
193    program-specific directory and there is no automatically
194    established link to the executable program where they are used.
195    Instead the executable program manually loads the DSO at
196    run-time into its address space via <code>dlopen()</code>. At
197    this time no resolving of symbols from the DSO for the
198    executable program is done. But instead the Unix loader
199    automatically resolves any (yet unresolved) symbols in the DSO
200    from the set of symbols exported by the executable program and
201    its already loaded DSO libraries (especially all symbols from
202    the ubiquitous <code>libc.so</code>). This way the DSO gets
203    knowledge of the executable program's symbol set as if it had
204    been statically linked with it in the first place.</p>
205
206    <p>Finally, to take advantage of the DSO's API the executable
207    program has to resolve particular symbols from the DSO via
208    <code>dlsym()</code> for later use inside dispatch tables
209    <em>etc.</em> In other words: The executable program has to
210    manually resolve every symbol it needs to be able to use it.
211    The advantage of such a mechanism is that optional program
212    parts need not be loaded (and thus do not spend memory) until
213    they are needed by the program in question. When required,
214    these program parts can be loaded dynamically to extend the
215    base program's functionality.</p>
216
217    <p>Although this DSO mechanism sounds straightforward there is
218    at least one difficult step here: The resolving of symbols from
219    the executable program for the DSO when using a DSO to extend a
220    program (the second way). Why? Because "reverse resolving" DSO
221    symbols from the executable program's symbol set is against the
222    library design (where the library has no knowledge about the
223    programs it is used by) and is neither available under all
224    platforms nor standardized. In practice the executable
225    program's global symbols are often not re-exported and thus not
226    available for use in a DSO. Finding a way to force the linker
227    to export all global symbols is the main problem one has to
228    solve when using DSO for extending a program at run-time.</p>
229
230    <p>The shared library approach is the typical one, because it
231    is what the DSO mechanism was designed for, hence it is used
232    for nearly all types of libraries the operating system
233    provides. On the other hand using shared objects for extending
234    a program is not used by a lot of programs.</p>
235
236    <p>As of 1998 there are only a few software packages available
237    which use the DSO mechanism to actually extend their
238    functionality at run-time: Perl 5 (via its XS mechanism and the
239    DynaLoader module), Netscape Server, <em>etc.</em> Starting
240    with version 1.3, Apache joined the crew, because Apache
241    already uses a module concept to extend its functionality and
242    internally uses a dispatch-list-based approach to link external
243    modules into the Apache core functionality. So, Apache is
244    really predestined for using DSO to load its modules at
245    run-time.</p>
246</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
247<div class="section">
248<h2><a name="advantages" id="advantages">Advantages and Disadvantages</a></h2>
249
250    <p>The above DSO based features have the following
251    advantages:</p>
252
253    <ul>
254      <li>The server package is more flexible at run-time because
255      the actual server process can be assembled at run-time via
256      <code class="directive"><a href="/mod/mod_so.html#loadmodule">LoadModule</a></code>
257      <code>httpd.conf</code> configuration commands instead of
258      <code class="program"><a href="/programs/configure.html">configure</a></code> options at build-time. For instance
259      this way one is able to run different server instances
260      (standard &amp; SSL version, minimalistic &amp; powered up
261      version [mod_perl, PHP3], <em>etc.</em>) with only one Apache
262      installation.</li>
263
264      <li>The server package can be easily extended with
265      third-party modules even after installation. This is at least
266      a great benefit for vendor package maintainers who can create
267      a Apache core package and additional packages containing
268      extensions like PHP3, mod_perl, mod_fastcgi,
269      <em>etc.</em></li>
270
271      <li>Easier Apache module prototyping because with the
272      DSO/<code class="program"><a href="/programs/apxs.html">apxs</a></code> pair you can both work outside the
273      Apache source tree and only need an <code>apxs -i</code>
274      command followed by an <code>apachectl restart</code> to
275      bring a new version of your currently developed module into
276      the running Apache server.</li>
277    </ul>
278
279    <p>DSO has the following disadvantages:</p>
280
281    <ul>
282      <li>The DSO mechanism cannot be used on every platform
283      because not all operating systems support dynamic loading of
284      code into the address space of a program.</li>
285
286      <li>The server is approximately 20% slower at startup time
287      because of the symbol resolving overhead the Unix loader now
288      has to do.</li>
289
290      <li>The server is approximately 5% slower at execution time
291      under some platforms because position independent code (PIC)
292      sometimes needs complicated assembler tricks for relative
293      addressing which are not necessarily as fast as absolute
294      addressing.</li>
295
296      <li>Because DSO modules cannot be linked against other
297      DSO-based libraries (<code>ld -lfoo</code>) on all platforms
298      (for instance a.out-based platforms usually don't provide
299      this functionality while ELF-based platforms do) you cannot
300      use the DSO mechanism for all types of modules. Or in other
301      words, modules compiled as DSO files are restricted to only
302      use symbols from the Apache core, from the C library
303      (<code>libc</code>) and all other dynamic or static libraries
304      used by the Apache core, or from static library archives
305      (<code>libfoo.a</code>) containing position independent code.
306      The only chances to use other code is to either make sure the
307      Apache core itself already contains a reference to it or
308      loading the code yourself via <code>dlopen()</code>.</li>
309    </ul>
310
311</div></div>
312<div class="bottomlang">
313<p><span>Available Languages: </span><a href="/en/dso.html" title="English">&nbsp;en&nbsp;</a> |
314<a href="/ja/dso.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
315<a href="/ko/dso.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a> |
316<a href="/tr/dso.html" hreflang="tr" rel="alternate" title="T�rk�e">&nbsp;tr&nbsp;</a></p>
317</div><div class="top"><a href="#page-header"><img src="/images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />This is not a Q&amp;A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div>
318<script type="text/javascript"><!--//--><![CDATA[//><!--
319var comments_shortname = 'httpd';
320var comments_identifier = 'http://httpd.apache.org/docs/2.2/dso.html';
321(function(w, d) {
322    if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
323        d.write('<div id="comments_thread"><\/div>');
324        var s = d.createElement('script');
325        s.type = 'text/javascript';
326        s.async = true;
327        s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
328        (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
329    }
330    else { 
331        d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
332    }
333})(window, document);
334//--><!]]></script></div><div id="footer">
335<p class="apache">Copyright 2013 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
336<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!--
337if (typeof(prettyPrint) !== 'undefined') {
338    prettyPrint();
339}
340//--><!]]></script>
341</body></html>