]> granicus.if.org Git - apache/blob - docs/manual/dso.html.en
Rename some references in the docs from the old "OSF1" term to the official
[apache] / docs / manual / dso.html.en
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
2 <HTML><HEAD>
3 <TITLE>Apache 1.3 Dynamic Shared Object (DSO) support</TITLE>
4 </HEAD>
5
6 <!-- Background white, links blue (unvisited), navy (visited), red (active) -->
7 <BODY
8  BGCOLOR="#FFFFFF"
9  TEXT="#000000"
10  LINK="#0000FF"
11  VLINK="#000080"
12  ALINK="#FF0000"
13 >
14 <BLOCKQUOTE>
15 <!--#include virtual="header.html" -->
16
17 <DIV ALIGN=CENTER>
18
19 <H1>
20 Apache 1.3<BR>
21 Dynamic Shared Object (DSO)<BR>
22 Support
23 </H1>
24
25 <ADDRESS>Originally written by<BR>
26 Ralf S. Engelschall &lt;rse@apache.org&gt, April 1998</ADDRESS>
27
28 </DIV>
29
30 <H3>Background</H3>
31
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.
36
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>.
41  
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.
53  
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.
63
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.
77  
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 etc. 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.
86
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.
97
98 <H3>Practical Usage</H3>
99
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.
104
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, etc.  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.
112
113 <P>As of Apache 1.3, the configuration system supports two optional features
114 for taking advantage of the modular DSO approach: compilation of the Apache
115 core program into a DSO library for shared usage and compilation of the
116 Apache modules into DSO files for explicit loading at run-time.
117
118 <H3>Implementation</H3>
119
120 <P>The DSO support for loading individual Apache modules is based on a module
121 named <A HREF="mod/mod_so.html"><CODE>mod_so.c</CODE></A> which has to be
122 statically compiled into the Apache core. It is the only module besides
123 <CODE>http_core.c</CODE> which cannot be put into a DSO itself
124 (bootstrapping!). Practically all other distributed Apache modules then can
125 then be placed into a DSO by individually enabling the DSO build for them via
126 <CODE>configure</CODE>'s <CODE>--enable-shared</CODE> option (see top-level
127 <CODE>INSTALL</CODE> file) or by changing the <CODE>AddModule</CODE> command
128 in your <CODE>src/Configuration</CODE> into a <CODE>SharedModule</CODE>
129 command (see <CODE>src/INSTALL</CODE> file).  After a module is compiled into
130 a DSO named <CODE>mod_foo.so</CODE> you can use <A
131 HREF="mod/mod_so.html"><CODE>mod_so</CODE></A>'s <A
132 HREF="mod/mod_so.html#loadmodule"><CODE>LoadModule</CODE></A> command in your
133 <CODE>httpd.conf</CODE> file to load this module at server startup or restart.
134
135 <P>To simplify this creation of DSO files for Apache modules (especially for
136 third-party modules) a new support program named <CODE>apxs</CODE> (<EM>APache
137 eXtenSion</EM>) is available. It can be used to build DSO based modules
138 <EM>outside of</EM> the Apache source tree. The idea is simple: When
139 installing Apache the <CODE>configure</CODE>'s <CODE>make install</CODE>
140 procedure installs the Apache C header files and puts the platform-dependent
141 compiler and linker flags for building DSO files into the <CODE>apxs</CODE>
142 program.  This way the user can use <CODE>apxs</CODE> to compile his Apache
143 module sources without the Apache distribution source tree and without having
144 to fiddle with the platform-dependent compiler and linker flags for DSO
145 support.
146
147 <P>To place the complete Apache core program into a DSO library (only required
148 on some of the supported platforms to force the linker to export the apache
149 core symbols -- a prerequisite for the DSO modularization) the rule
150 <CODE>SHARED_CORE</CODE> has to be enabled via <CODE>configure</CODE>'s
151 <CODE>--enable-rule=SHARED_CORE</CODE> option (see top-level
152 <CODE>INSTALL</CODE> file) or by changing the <CODE>Rule</CODE> command in
153 your <CODE>Configuration</CODE> file to <CODE>Rule SHARED_CORE=yes</CODE> (see
154 <CODE>src/INSTALL</CODE> file). The Apache core code is then placed into a DSO
155 library named <CODE>libhttpd.so</CODE>. Because one cannot link a DSO against
156 static libraries on all platforms, an additional executable program named
157 <CODE>libhttpd.ep</CODE> is created which both binds this static code and
158 provides a stub for the <CODE>main()</CODE> function.  Finally the
159 <CODE>httpd</CODE> executable program itself is replaced by a bootstrapping
160 code which automatically makes sure the Unix loader is able to load and start
161 <CODE>libhttpd.ep</CODE> by providing the <CODE>LD_LIBRARY_PATH</CODE> to
162 <CODE>libhttpd.so</CODE>.
163
164 <H3>Supported Platforms</H3>
165
166 <P>Apache's <CODE>src/Configure</CODE> script currently has only limited but
167 adequate built-in knowledge on how to compile DSO files, because as already
168 mentioned this is heavily platform-dependent. Nevertheless all major Unix
169 platforms are supported.  The definitive current state (May 1998) is this:
170
171 <P>
172 <UL>
173 <LI>Out-of-the-box supported platforms:<BR>
174 (actually tested versions in parenthesis)
175
176 <PRE>
177 o  FreeBSD            (2.1.5, 2.2.5, 2.2.6)
178 o  OpenBSD            (2.x)
179 o  NetBSD             (1.3.1)
180 o  Linux              (Debian/1.3.1, RedHat/4.2)
181 o  Solaris            (2.4, 2.5.1, 2.6)
182 o  SunOS              (4.1.3)
183 o  Digital UNIX       (4.0)
184 o  IRIX               (6.2)
185 o  HP/UX              (10.20)
186 o  UnixWare           (2.01, 2.1.2)
187 o  SCO                (5.0.4)
188 o  AIX                (3.2, 4.1.5, 4.2, 4.3)
189 o  ReliantUNIX/SINIX  (5.43)
190 o  SVR4               (-)
191 </PRE>
192
193 <P>
194 <LI> Explicitly unsupported platforms:
195
196 <PRE>
197 o  Ultrix             (no dlopen-style interface under this platform)
198 </PRE>
199
200 </UL>
201
202 <H3>Usage Summary</H3>
203
204 <P>To give you an overview of the DSO features of Apache 1.3, here is a short
205 and concise summary:
206
207 <OL>
208
209 <LI>Placing the Apache core code (all the stuff which usually forms the
210 <CODE>httpd</CODE> binary) into a DSO <CODE>libhttpd.so</CODE>, an executable
211 program <CODE>libhttpd.ep</CODE> and a bootstrapping executable program
212 <CODE>httpd</CODE> (Notice: this is only required on some of the supported
213 platforms to force the linker to export the Apache core symbols, which in turn
214 is a prerequisite for the DSO modularization):
215
216 <P>
217 <UL>
218 <LI>Build and install via <CODE>configure</CODE> (preferred):
219 <TABLE BGCOLOR="#f0f0f0" CELLPADDING=10><TR><TD>
220 <PRE>
221 $ ./configure --prefix=/path/to/install
222               --enable-rule=SHARED_CORE ...
223 $ make install
224 </PRE>
225 </TD></TR></TABLE>
226
227 <LI>Build and install manually: 
228 <TABLE BGCOLOR="#f0f0f0" CELLPADDING=10><TR><TD>
229 <PRE>
230 - Edit src/Configuration:
231   &lt;&lt; Rule SHARED_CORE=default
232   &gt;&gt; Rule SHARED_CORE=yes
233   &lt;&lt; EXTRA_CFLAGS= 
234   &gt;&gt; EXTRA_CFLAGS= -DSHARED_CORE_DIR=\"/path/to/install/libexec\"
235 $ make 
236 $ cp src/libhttpd.so* /path/to/install/libexec/
237 $ cp src/libhttpd.ep  /path/to/install/libexec/
238 $ cp src/httpd        /path/to/install/bin/
239 </PRE>    
240 </TD></TR></TABLE>
241 </UL>
242
243 <LI>Build and install a <EM>distributed</EM> Apache module, say
244 <CODE>mod_foo.c</CODE>, into its own DSO <CODE>mod_foo.so</CODE>:
245
246 <P>
247 <UL>
248 <LI>Build and install via <CODE>configure</CODE> (preferred):
249 <TABLE BGCOLOR="#f0f0f0" CELLPADDING=10><TR><TD>
250 <PRE>
251 $ ./configure --prefix=/path/to/install
252         --enable-shared=foo
253 $ make install
254 </PRE>
255 </TD></TR></TABLE>
256
257 <LI>Build and install manually: 
258 <TABLE BGCOLOR="#f0f0f0" CELLPADDING=10><TR><TD>
259 <PRE>
260 - Edit src/Configuration:
261   &lt;&lt; AddModule    modules/xxxx/mod_foo.o
262   &gt;&gt; SharedModule modules/xxxx/mod_foo.so
263 $ make
264 $ cp src/xxxx/mod_foo.so /path/to/install/libexec
265 - Edit /path/to/install/etc/httpd.conf
266   &gt;&gt; LoadModule foo_module /path/to/install/libexec/mod_foo.so
267 </PRE>
268 </TD></TR></TABLE>
269 </UL>
270
271 <LI>Build and install a <EM>third-party</EM> Apache module, say
272 <CODE>mod_foo.c</CODE>, into its own DSO <CODE>mod_foo.so</CODE>
273
274 <P>
275 <UL>
276 <LI>Build and install via <CODE>configure</CODE> (preferred):
277 <TABLE BGCOLOR="#f0f0f0" CELLPADDING=10><TR><TD>
278 <PRE>
279 $ ./configure --add-module=/path/to/3rdparty/mod_foo.c 
280         --enable-shared=foo
281 $ make install
282 </PRE>
283 </TD></TR></TABLE>
284
285 <LI>Build and install manually: 
286 <TABLE BGCOLOR="#f0f0f0" CELLPADDING=10><TR><TD>
287 <PRE>
288 $ cp /path/to/3rdparty/mod_foo.c /path/to/apache-1.3/src/modules/extra/
289 - Edit src/Configuration:
290   &gt;&gt; SharedModule modules/extra/mod_foo.so
291 $ make
292 $ cp src/xxxx/mod_foo.so /path/to/install/libexec
293 - Edit /path/to/install/etc/httpd.conf
294   &gt;&gt; LoadModule foo_module /path/to/install/libexec/mod_foo.so
295 </PRE>
296 </TD></TR></TABLE>
297 </UL>
298
299 <P>
300 <LI>Build and install a <EM>third-party</EM> Apache module, say
301 <CODE>mod_foo.c</CODE>, into its own DSO <CODE>mod_foo.so</CODE> <EM>outside
302 of</EM> the Apache source tree:
303
304 <P>
305 <UL>
306 <LI>Build and install via <CODE>apxs</CODE>: 
307 <TABLE BGCOLOR="#f0f0f0" CELLPADDING=10><TR><TD>
308 <PRE>
309 $ cd /path/to/3rdparty
310 $ apxs -c mod_foo.c
311 $ apxs -i -a -n foo mod_foo.so
312 </PRE>
313 </TD></TR></TABLE>
314 </UL>
315
316 </OL>
317
318 <H3>Advantages & Disadvantages</H3>
319
320 <P>The above DSO based features of Apache 1.3 have the following advantages:
321
322 <UL>
323 <LI> The server package is more flexible at run-time because the actual server
324      process can be assembled at run-time via <A
325      HREF="mod/mod_so.html#loadmodule"><CODE>LoadModule</CODE></A>
326      <CODE>httpd.conf</CODE> configuration commands instead of
327      <CODE>Configuration</CODE> <CODE>AddModule</CODE> commands at build-time.
328      For instance this way one is able to run different server instances
329      (standard &amp; SSL version, minimalistic &amp; powered up version
330      [mod_perl, PHP3], etc.) with only one Apache installation.
331 <P>
332 <LI> The server package can be easily extended with third-party modules even
333      after installation. This is at least a great benefit for vendor package
334      maintainers who can create a Apache core package and additional packages
335      containing extensions like PHP3, mod_perl, mod_fastcgi, etc.
336 <P>
337 <LI> Easier Apache module prototyping because with the DSO/<CODE>apxs</CODE>
338      pair you can both work outside the Apache source tree and only need an
339      <CODE>apxs -i</CODE> command followed by an <CODE>apachectl
340      restart</CODE> to bring a new version of your currently developed module
341      into the running Apache server.
342 </UL>
343
344 <P>DSO has the following disadvantages:
345
346 <UL>
347 <LI> The DSO mechanism cannot be used on every platform because not all
348      operating systems support dynamic loading of code into the address space
349      of a program.
350 <P>
351 <LI> The server is approximately 20% slower at startup time because of the
352      symbol resolving overhead the Unix loader now has to do.
353 <P>
354 <LI> The server is approximately 5% slower at execution time under some
355      platforms because position independent code (PIC) sometimes needs
356      complicated assembler tricks for relative addressing which are not
357      necessarily as fast as absolute addressing.
358 <P>
359 <LI> Because DSO modules cannot be linked against other DSO-based libraries
360      (<CODE>ld -lfoo</CODE>) on all platforms (for instance a.out-based
361      platforms usually don't provide this functionality while ELF-based
362      platforms do) you cannot use the DSO mechanism for all types of modules.
363      Or in other words, modules compiled as DSO files are restricted to only
364      use symbols from the Apache core, from the C library (<CODE>libc</CODE>)
365      and all other dynamic or static libraries used by the Apache core, or
366      from static library archives (<CODE>libfoo.a</CODE>) containing position
367      independent code. The only chances to use other code is to either make
368      sure the Apache core itself already contains a reference to it, loading
369      the code yourself via <CODE>dlopen()</CODE> or enabling the
370      <CODE>SHARED_CHAIN</CODE> rule while building Apache when your platform
371      supports linking DSO files against DSO libraries.
372 <P>
373 <LI> Under some platforms (many SVR4 systems) there is no way to force the
374      linker to export all global symbols for use in DSO's when linking the
375      Apache httpd executable program. But without the visibility of the Apache
376      core symbols no standard Apache module could be used as a DSO. The only
377      chance here is to use the <CODE>SHARED_CORE</CODE> feature because this
378      way the global symbols are forced to be exported. As a consequence the
379      Apache <CODE>src/Configure</CODE> script automatically enforces
380      <CODE>SHARED_CORE</CODE> on these platforms when DSO features are used in
381      the <CODE>Configuration</CODE> file or on the configure command line.
382 </UL>
383
384 <!--#include virtual="footer.html" -->
385 </BLOCKQUOTE>
386 </BODY>
387 </HTML>