1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
3 <TITLE>Apache 2.0 Dynamic Shared Object (DSO) support</TITLE>
6 <!-- Background white, links blue (unvisited), navy (visited), red (active) -->
15 <!--#include virtual="header.html" -->
21 Dynamic Shared Object (DSO)<BR>
25 <ADDRESS>Originally written by<BR>
26 Ralf S. Engelschall <rse@apache.org>, April 1998</ADDRESS>
32 <P>On modern Unix derivatives there exists a nifty mechanism usually called
33 dynamic linking/loading of <EM>Dynamic Shared Objects</EM> (DSO) which
34 provides a way to build a piece of program code in a special format for
35 loading it at run-time into the address space of an executable program.
37 <P>This loading can usually be done in two ways: Automatically by a system
38 program called <CODE>ld.so</CODE> when an executable program is started or
39 manually from within the executing program via a programmatic system interface
40 to the Unix loader through the system calls <CODE>dlopen()/dlsym()</CODE>.
42 <P>In the first way the DSO's are usually called <EM>shared libraries</EM> or
43 <EM>DSO libraries</EM> and named <CODE>libfoo.so</CODE> or
44 <CODE>libfoo.so.1.2</CODE>. They reside in a system directory (usually
45 <CODE>/usr/lib</CODE>) and the link to the executable program is established
46 at build-time by specifying <CODE>-lfoo</CODE> to the linker command. This
47 hard-codes library references into the executable program file so that at
48 start-time the Unix loader is able to locate <CODE>libfoo.so</CODE> in
49 <CODE>/usr/lib</CODE>, in paths hard-coded via linker-options like
50 <CODE>-R</CODE> or in paths configured via the environment variable
51 <CODE>LD_LIBRARY_PATH</CODE>. It then resolves any (yet unresolved) symbols in
52 the executable program which are available in the DSO.
54 <P>Symbols in the executable program are usually not referenced by the DSO
55 (because it's a reusable library of general code) and hence no further
56 resolving has to be done. The executable program has no need to do anything on
57 its own to use the symbols from the DSO because the complete resolving is done
58 by the Unix loader. (In fact, the code to invoke <CODE>ld.so</CODE> is part of
59 the run-time startup code which is linked into every executable program which
60 has been bound non-static). The advantage of dynamic loading of common library
61 code is obvious: the library code needs to be stored only once, in a system
62 library like <CODE>libc.so</CODE>, saving disk space for every program.
64 <P>In the second way the DSO's are usually called <EM>shared objects</EM> or
65 <EM>DSO files</EM> and can be named with an arbitrary extension (although the
66 canonical name is <CODE>foo.so</CODE>). These files usually stay inside a
67 program-specific directory and there is no automatically established link to
68 the executable program where they are used. Instead the executable program
69 manually loads the DSO at run-time into its address space via
70 <CODE>dlopen()</CODE>. At this time no resolving of symbols from the DSO for
71 the executable program is done. But instead the Unix loader automatically
72 resolves any (yet unresolved) symbols in the DSO from the set of symbols
73 exported by the executable program and its already loaded DSO libraries
74 (especially all symbols from the ubiquitous <CODE>libc.so</CODE>). This way
75 the DSO gets knowledge of the executable program's symbol set as if it had
76 been statically linked with it in the first place.
78 <P>Finally, to take advantage of the DSO's API the executable program has to
79 resolve particular symbols from the DSO via <CODE>dlsym()</CODE> for later use
80 inside dispatch tables <EM>etc.</EM> In other words: The executable program has to
81 manually resolve every symbol it needs to be able to use it. The advantage of
82 such a mechanism is that optional program parts need not be loaded (and thus
83 do not spend memory) until they are needed by the program in question. When
84 required, these program parts can be loaded dynamically to extend the base
85 program's functionality.
87 <P>Although this DSO mechanism sounds straightforward there is at least one
88 difficult step here: The resolving of symbols from the executable program for
89 the DSO when using a DSO to extend a program (the second way). Why? Because
90 "reverse resolving" DSO symbols from the executable program's symbol set is
91 against the library design (where the library has no knowledge about the
92 programs it is used by) and is neither available under all platforms nor
93 standardized. In practice the executable program's global symbols are often
94 not re-exported and thus not available for use in a DSO. Finding a way to
95 force the linker to export all global symbols is the main problem one has to
96 solve when using DSO for extending a program at run-time.
98 <H3>Practical Usage</H3>
100 <P>The shared library approach is the typical one, because it is what the DSO
101 mechanism was designed for, hence it is used for nearly all types of libraries
102 the operating system provides. On the other hand using shared objects for
103 extending a program is not used by a lot of programs.
105 <P>As of 1998 there are only a few software packages available which use the
106 DSO mechanism to actually extend their functionality at run-time: Perl 5 (via
107 its XS mechanism and the DynaLoader module), Netscape Server, <EM>etc.</EM> Starting
108 with version 1.3, Apache joined the crew, because Apache already uses a module
109 concept to extend its functionality and internally uses a dispatch-list-based
110 approach to link external modules into the Apache core functionality. So,
111 Apache is really predestined for using DSO to load its modules at run-time.
113 <H3>Implementation</H3>
115 <P>The DSO support for loading individual Apache modules is based on a module
116 named <A HREF="mod/mod_so.html"><CODE>mod_so.c</CODE></A> which has to be
117 statically compiled into the Apache core. It is the only module besides
118 <CODE>core.c</CODE> which cannot be put into a DSO itself
119 (bootstrapping!). Practically all other distributed Apache modules then can
120 then be placed into a DSO by individually enabling the DSO build for them via
121 <CODE>configure</CODE>'s <CODE>--enable-<i>module</i>=shared</CODE> option
122 (see top-level <CODE>INSTALL</CODE> file). After a module is compiled into
123 a DSO named <CODE>mod_foo.so</CODE> you can use <A
124 HREF="mod/mod_so.html"><CODE>mod_so</CODE></A>'s <A
125 HREF="mod/mod_so.html#loadmodule"><CODE>LoadModule</CODE></A> command in your
126 <CODE>httpd.conf</CODE> file to load this module at server startup or restart.
128 <P>To simplify this creation of DSO files for Apache modules
129 (especially for third-party modules) a new support program named <a
130 href="programs/apxs.html">apxs</a> (<EM>APache eXtenSion</EM>) is
131 available. It can be used to build DSO based modules <EM>outside
132 of</EM> the Apache source tree. The idea is simple: When installing
133 Apache the <CODE>configure</CODE>'s <CODE>make install</CODE>
134 procedure installs the Apache C header files and puts the
135 platform-dependent compiler and linker flags for building DSO files
136 into the <CODE>apxs</CODE> program. This way the user can use
137 <CODE>apxs</CODE> to compile his Apache module sources without the
138 Apache distribution source tree and without having to fiddle with the
139 platform-dependent compiler and linker flags for DSO support.
141 <H3>Supported Platforms</H3>
143 <P>Apache's <CODE>src/Configure</CODE> script currently has only limited but
144 adequate built-in knowledge on how to compile DSO files, because as already
145 mentioned this is heavily platform-dependent. Nevertheless all major Unix
146 platforms are supported. The definitive current state (May 1999) is this:
150 <LI>Out-of-the-box supported platforms:<BR>
151 (actually tested versions in parenthesis)
154 o FreeBSD (2.1.5, 2.2.x, 3.x, 4.x)
158 o Linux (Debian/1.3.1, RedHat/4.2)
159 o Solaris (2.4, 2.5, 2.6, 2.7)
164 o UnixWare (2.01, 2.1.2)
166 o AIX (3.2, 4.1.5, 4.2, 4.3)
167 o ReliantUNIX/SINIX (5.43)
169 o Mac OS X Server (1.0)
170 o Mac OS (10.0 preview 1)
171 o OpenStep/Mach (4.2)
177 <LI> Explicitly unsupported platforms:
180 o Ultrix (no dlopen-style interface under this platform)
185 <H3>Usage Summary</H3>
187 <P>To give you an overview of the DSO features of Apache 2.0, here is a short
194 <LI>Build and install via <CODE>configure</CODE>:
195 <TABLE BGCOLOR="#f0f0f0" CELLPADDING=10><TR><TD>
197 $ ./configure --prefix=/path/to/install ...
203 <LI>Build and install a <EM>distributed</EM> Apache module, say
204 <CODE>mod_foo.c</CODE>, into its own DSO <CODE>mod_foo.so</CODE>:
208 <LI>Build and install via <CODE>configure</CODE>:
209 <TABLE BGCOLOR="#f0f0f0" CELLPADDING=10><TR><TD>
211 $ ./configure --prefix=/path/to/install
218 <LI>Build and install a <EM>third-party</EM> Apache module, say
219 <CODE>mod_foo.c</CODE>, into its own DSO <CODE>mod_foo.so</CODE>
223 <LI>Build and install via <CODE>configure</CODE>:
224 <TABLE BGCOLOR="#f0f0f0" CELLPADDING=10><TR><TD>
226 $ ./configure --add-module=module_type:/path/to/3rdparty/mod_foo.c
234 <LI>Build and install a <EM>third-party</EM> Apache module, say
235 <CODE>mod_foo.c</CODE>, into its own DSO <CODE>mod_foo.so</CODE> <EM>outside
236 of</EM> the Apache source tree:
240 <LI>Build and install via <a href="programs/apxs.html">apxs</a>:
241 <TABLE BGCOLOR="#f0f0f0" CELLPADDING=10><TR><TD>
243 $ cd /path/to/3rdparty
245 $ apxs -i -a -n foo mod_foo.so
252 <H3>Advantages & Disadvantages</H3>
254 <P>The above DSO based features of Apache 2.0 have the following advantages:
257 <LI> The server package is more flexible at run-time because the actual server
258 process can be assembled at run-time via <A
259 HREF="mod/mod_so.html#loadmodule"><CODE>LoadModule</CODE></A>
260 <CODE>httpd.conf</CODE> configuration commands instead of
261 <CODE>configure</CODE> options at build-time.
262 For instance this way one is able to run different server instances
263 (standard & SSL version, minimalistic & powered up version
264 [mod_perl, PHP3], <EM>etc.</EM>) with only one Apache installation.
266 <LI> The server package can be easily extended with third-party modules even
267 after installation. This is at least a great benefit for vendor package
268 maintainers who can create a Apache core package and additional packages
269 containing extensions like PHP3, mod_perl, mod_fastcgi, <EM>etc.</EM>
271 <LI> Easier Apache module prototyping because with the DSO/<CODE>apxs</CODE>
272 pair you can both work outside the Apache source tree and only need an
273 <CODE>apxs -i</CODE> command followed by an <CODE>apachectl
274 restart</CODE> to bring a new version of your currently developed module
275 into the running Apache server.
278 <P>DSO has the following disadvantages:
281 <LI> The DSO mechanism cannot be used on every platform because not all
282 operating systems support dynamic loading of code into the address space
285 <LI> The server is approximately 20% slower at startup time because of the
286 symbol resolving overhead the Unix loader now has to do.
288 <LI> The server is approximately 5% slower at execution time under some
289 platforms because position independent code (PIC) sometimes needs
290 complicated assembler tricks for relative addressing which are not
291 necessarily as fast as absolute addressing.
293 <LI> Because DSO modules cannot be linked against other DSO-based libraries
294 (<CODE>ld -lfoo</CODE>) on all platforms (for instance a.out-based
295 platforms usually don't provide this functionality while ELF-based
296 platforms do) you cannot use the DSO mechanism for all types of modules.
297 Or in other words, modules compiled as DSO files are restricted to only
298 use symbols from the Apache core, from the C library (<CODE>libc</CODE>)
299 and all other dynamic or static libraries used by the Apache core, or
300 from static library archives (<CODE>libfoo.a</CODE>) containing position
301 independent code. The only chances to use other code is to either make
302 sure the Apache core itself already contains a reference to it or loading
303 the code yourself via <CODE>dlopen()</CODE>.
307 <!--#include virtual="footer.html" -->