LINUX TIPS AND TRICKS --- May 04, 2001

Published by ITworld.com -- changing the way you view IT
http://www.itworld.com/newsletters
________________________________________________________________

Dynamic Loading
By Danny Kalev

This week, I will present the dl library and discuss dynamic 
loading concepts. In essence, dynamic loading consists of 
opening a library, searching for one or more symbols (which can 
refer to functions, variables, arrays, etc...), using the 
retrieved symbols, and closing the library. All of the 
functions and data types pertaining to dynamic loading are 
declared in the header <dlfcn.h>.

The dl Functions
The dlerror() function reports the most recent error that 
occurred with any of the remaining dl functions. I will 
discuss the other dl functions shortly. The dlerror() 
function's prototype is as follows:

    const char * dlerror(void);

dlerror() returns a string describing the error that occurred. 
If no error occurred, then it returns NULL. Remember that this 
function clears the error after it returns, so you must store 
the result if you intend to use it later.

The dlopen() function opens a library and returns a matching 
handle. A library is a file of compiled code modules. Here is 
an example of the dlopen() function:

    void * dlopen(const char * filename, int flags)

Typically, you pass an absolute pathname (i.e., a file 
beginning with a slash) as the filename but in this case, 
dlopen() doesn't need to search for the library. However, if 
you pass a simple filename, then the function looks for the 
library in the following places until it locates the 
specified library file:

    1) A set of directories specified in the LD_ELF_LIBRARY_PATH 
       environment variable. If this variable isn't defined, the 
       directories listed in LD_LIBRARY_PATH are searched.
    2) The libraries listed in the /etc/ld.so.cache file.
    3) /usr/lib
    4) /lib

dlopen() returns NULL if it fails to open the specified library.

Two types of symbol resolution exist: lazy and immediate. A 
lazy resolution means that symbol resolution is performed on 
demand. Lazy resolution is useful when you need to load a small 
portion of a huge library efficiently. By contrast, immediate 
resolution resolves all the library's unresolved symbols at 
once. Immediate resolution is useful when the library is small 
and for debugging purposes.

dlopen()'s second argument indicates the resolution type: 
RTLD_LAZY and RTLD_NOW, for lazy and immediate resolution, 
respectively. You may combine either of these constants with 
the RTLD_GLOBAL to export symbols to other modules.

For the symbol lookup process, use the dlsym() function:

    void* dlsym(void * libhandle, char * symbol);

libhandle is a value returned from a previous dlopen() call. 
The second argument represents the function or variable's name 
that you want to retrieve. If the symbol was found, then 
dlsym() returns its address (you have to explicitly cast the 
return value to the desired type). Otherwise, dlsym() returns 
NULL. Note, however, that a NULL value can also be a valid 
address of a symbol, an initialized pointer for example. 
Therefore, if the sought-after symbol may have a NULL value, 
you have to call dlerror() after calling dlsym() to check 
whether the retrieval was successful.

Calling dlclose() closes a library. It has the 
following prototype:

    void * dlclose(void * libhandle);

Next week, I will continue this discussion and provide code 
examples showing how to load libraries dynamically.

About the author(s)
-------------------
Danny Kalev is a system analyst and software engineer with more 
than 10 years of experience, specializing in C++ and 
object-oriented analysis and design on various platforms 
including VMS, DOS, Windows, Unix, and Linux. His technical 
interests involve code optimization, networking, and 
distributed computing. He is also a member of the ANSI C++ 
standardization committee and the author of ANSI/ISO C++ 
Professional Programmer's Handbook (Que, 1999). 
Contact him at linuxnl@excite.com.
________________________________________________________________

ADDITIONAL RESOURCES

Regular Expressions: Manage CORBA with scripting 
All the leading open source-scripting languages have 
serviceable CORBA modules

http://www.itworld.com/jump/lintps_nl/www.itworld.com/AppDev/
4061/UIR010502regex1/

Building library interposers for fun and profit 
Create and run your own 

http://www.itworld.com/jump/lintps_nl/www.itworld.com/AppDev/
1006/UIR000929interposers/

Speed and portability 
How to use native methods without restricting class usability

http://www.itworld.com/jjw/lintps_nl/javatips/jw-javatip13.html
________________________________________________________________

COMMUNITY DISCUSSIONS

Linux Software Development
Hone your Linux development skills, share your expertise, and 
put out the occasional call for help in this discussion for 
programmers of all levels. Moderated by Danny Kalev.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee6b652/332!skip=257

Ask the Geek
Got a question about Linux setup, tuning, security, or 
maintenance? Hackers, newbies, and all the geeks in between 
knock heads and devise solutions in this discussion.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee6c981/537!skip=480
http://ad.doubleclick.net/clk;2760307;5704255;e
________________________________________________________________

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate 
  Editor, Newsletters at: andrew_santosusso@itworld.com

* For advertising information, write Dan Chupka, Account 
  Executive at: dan_chupka@itworld.com

* For recruitment advertising information, write Jamie Swartz,
  Eastern Regional Sales Manager at: jamie_swartz@itworld.com 
  or Paul Duthie, Western Regional Sales Manager
  at: paul_duthie@itworld.com

* For all other inquiries, write Jodie Naze, Product Manager, 
  Newsletters at: jodie_naze@itworld.com
________________________________________________________________

PRIVACY POLICY

http://www.itworld.com/Privacy/

Copyright 2001 ITworld.com, Inc., All Rights Reserved.
http://www.itworld.com
