[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]

Technical Reference: Base Operating System and Extensions, Volume 1


dlopen Subroutine

The dlopen subroutine includes information for dlopen subroutine on a POWER-based platform and on dlopen subroutine on an Itanium-based platform.

dlopen Subroutine on POWER-based Platform

Purpose

Dynamically load a module into the calling process.

Syntax

#include <dlfcn.h>

void *dlopen (FilePath, Flags);
const char *FilePath;
int Flags;

Description

The dlopen subroutine loads the module specified by FilePath into the executing process's address space. Dependents of the module are automatically loaded as well. If the module is already loaded, i t is not loaded again, but a new, unique value will be returned by the dlopen subroutine.

The value returned by dlopen may be used in subsequent calls to dlsym and dlclose. If an error occurs during the operation, dlopen returns NULL.

If the main application was linked with the -brtl option, then the runtime linker is invoked by dlopen. If the module being loaded was linked with runtime linking enabled, both intra-module and inter-module references are overridden by any symbols available in the main application. If runtime linking was enabled, but the module was not built enabled, then all inter-module references will be overridden, but some intra-module references will not be overridden.

If the module being opened with dlopen or any of its dependents is being loaded for the first time, initialization routines for these newly-loaded routines are called (after runtime linking, if applicable) before dlopen returns. Initialization routines are the functions specified with the -binitfini: linker option when the module was built. (Refer to the ld command for more information about this option.)

Notes:
  1. The initialization functions need not have any special names, and multiple functions per module are allowed.
  2. If the module being loaded has read-other permission, the module is loaded into the global shared library segment. Modules loaded into the global shared library segment are not unloaded even if they are no longer being used. Use the slibclean command to remove unused modules from the global shared library segment.

Use the environment variable LIBPATH to specify a list of directories in which dlopen search es for the named module. The running application also contains a set of library search paths that were specified when the application was linked; these paths are searched after any paths found in LIBPATH. Also, the setenv subroutine

FilePath Specifies the name of a file containing the loadable module. This parameter can be contain an absolute path, a relative path, or no path component. If FilePath contains a slash character, FilePath is used directly, and no directories are searched.

If the FilePath parameter is /unix, dlopen returns a value that can be used to look up symbols in the current kernel image, including those symbols found in any kernel extension that was available at the time the process began execution.

If the value of FilePath is NULL, a value for the main application is returned. This allows dynamically loaded objects to look up symbols in the main executable, or for an application to examine symbols available within itself.

Flags

Specifies variations of the behavior of dlopen. Either RTLD_NOW or RTLD_LAZY must always be specified. Other flags may be OR'ed with RTLD_NOW or RTLD_LAZY.

RTLD_NOW Load all dependents of the module being loaded and resolve all symbols.
RTLD_LAZY Specifies the same behavior as RTLD_NOW. In a future release of the operating system, the behavior of the RTLD_LAZY may change so that loading of dependent modules is deferred of resolution of some symbols is deferred.
RTLD_GLOBAL Allows symbols in the module being loaded to be visible when resolving symbols used by other dlopen calls. These symbols will also be visible when the main application is opened with dlopen(NULL, mode).
RTLD_LOCAL Prevent symbols in the module being loaded from being used when resolving symbols used by other dlopen calls. Symbols in the module being loaded can only be accessed by calling dlsym subroutine. If neither RTLD_GLOBAL nor RTLD_LOCAL is specified, the default is RTLD_LOCAL. If both flags are specified, RTLD_LOCAL is ignored.
RTLD_MEMBER The dlopen subroutine can be used to load a module that is a member of an archive. The L_LOADMEMBER flag is used when the load subroutine is called. The module name FilePath names the archive and archive member according to the rules outlined in the load subroutine.
RTLD_NOAUTODEFER Prevents deferred imports in the module being loaded from being automatically resolved by subsequent loads. The L_NOAUTODEFER flag is used when the load subroutine is called.

Ordinarily, modules built for use by the dlopen and dlsym sub routines will not contain deferred imports. However, deferred imports can be still used. A module opened with dlopen may provide definitions for deferred imports in the main application, for modules loaded with the load subroutine (if the L_NOAUTODEFER flag was not used), and for other modules loaded with the dlopen subroutine (if the RTLD_NOAUTODEFER flag was not used).

Return Values

Upon successful completion, dlopen returns a value that can be used in calls to the dlsym and dlclose subroutines. The value is not valid for use with the loadbind and unload subroutines.

If the dlopen call fails, NULL (a value of 0) is returned and the global variable errno is set. If errno contains the value ENOEXEC, further information is available via the dlerror function.

Error Codes

See the load subroutine for a list of possible errno values and their meanings.

Implementation Specifics

This subroutine is part of Base Operating System (BOS) Runtime.

Related Information

The dlclose (dlclose Subroutine) subroutine, dlerror (dlerror Subroutine) subroutine, dlsym (dladdr Subroutine) subroutine, load (load Subroutine) subroutine, loadbind (loadbind Subroutine) subroutine, loadquery (loadquery Subroutine)subroutine, unload subroutine.

The ld command.

Subroutines Overview in AIX 5L Version 5.1 General Programming Concepts: Writing and Debugging Programs.

dlopen Subroutine on Itanium-based Platform

Purpose

Opens a shared object.

Syntax

#include <dlfcn.h> 
    
void *dlopen(const char *file, int mode); 

Description

The dlopen subroutine is one of a family of routines that give the user direct access to the dynamic linking facilities. dlopen makes a shared object specified by file available to a running process. A shared object may specify other objects that it "needs" in order to execute properly. These dependencies are specified by DT_NEEDED entries in the .dynamic section of the original object. Each needed object may, in turn, specify other needed objects. All such objects are loaded along with the original object as a result of the call to dlopen.

A successful dlopen call returns to the process a handle the process may use on subsequent calls to dlsym and dlclose. This value should not be interpreted in any way by the process.

file is used to construct a pathname to the object file. If file contains a slash character, the file argument, itself, is used as the pathname. Otherwise, a series of directories is searched for file. First, any directories specified by a DT_RPATH entry in the .dynamic section of the original program object are searched. Then, any directories specified by the environment variable LD_LIBRARY_PATH are searched. Finally, the directory /usr/lib is searched.

If the value of file is 0, dlopen provides a handle on a global symbol object. This object provides access to the symbols from an ordered set of objects consisting of the original a.out, all of the objects that were loaded at program startup along with the a.out, and all objects loaded using a dlopen operation along with the RTLD_GLOBAL flag. As the latter set of objects can change during execution, the set identified by handle can also change dynamically.

Only a single copy of an object file is brought into the address space, even if dlopen is invoked multiple times in reference to the file, and even if different pathnames are used to reference the file.

When a shared object is brought into the address space of a process, it may contain references to symbols whose addresses are not known until the object is loaded. These references must be relocated before the symbols can be accessed. The mode parameter governs when these relocations take place and may have the following values:

RTLD_LAZY
Under this mode, only references to data symbols are relocated when the object is loaded. References to functions are not relocated until a given function is invoked for the first time. This mode should result in better performance, since a process may not reference all of the functions in any given shared object.

RTLD_NOW
Under this mode, all necessary relocations are performed when the object is first loaded. This may result in some wasted effort, if relocations are performed for functions that are never referenced, but is useful for applications that need to know as soon as an object is loaded that all symbols referenced during execution will be available.

Any object loaded by dlopen that requires relocations against global symbols can reference the symbols in the original a.out, any objects loaded at program startup, from the object itself as well as any other object included in the same dlopen invocation, and any objects that were loaded in any dlopen invocation that specified the RTLD_GLOBAL flag. To determine the scope of visibility for the symbols loaded with a dlopen invocation, the mode parameter should be bitwise or'ed with one of the following values:

RLTD_GLOBAL
The object's symbols are made available for the relocation processing of any other object. In addition, symbol lookup using dlopen(0, mode) and an associated dlsym() allows objects loaded with RTLD_GLOBAL to be searched.

RTLD_LOCAL
The object's symbols are made available for relocation processing only to objects loaded in the same dlopen invocation.

If neither RTLD_GLOBAL nor RTLD_LOCAL are specified, the default is RTLD_LOCAL.

If a file is specified in multiple dlopen invocations, mode is interpreted at each invocation. Note, however, that once RTLD_NOW has been specified, all relocations will have been completed, rendering any further RTLD_NOW operations redundant and any further RTLD_LAZY operations irrelevant. Similarly note that once RTLD_GLOBAL has been specified, the object will maintain the RTLD_GLOBAL status regardless of any previous or future specification of RTLD_LOCAL, so long as the object remains in the address space [see dlclose (dlclose Subroutine)].

If a file is specified in multiple dlopen invocations, mode is interpreted at each invocation. Note, however, that once RTLD_NOW has been specified, all relocations will have been completed, rendering any further RTLD_NOW operations redundant and any further RTLD_LAZY operations irrelevant. Similarly note that once RTLD_GLOBAL has been specified, the object will maintain the RTLD_GLOBAL status regardless of any previous or future specification of RTLD_LOCAL, so long as the object remains in the address space [see >dlclose (dlclose Subroutine)].

Symbols introduced into a program through calls to dlopen may be used in relocation activities. Symbols so introduced may duplicate symbols already defined by the program or previous dlopen operations. To resolve the ambiguities such a situation might present, the resolution of a symbol reference to symbol definition is based on a symbol resolution order. Two such resolution orders are defined: load and dependency ordering. Load order establishes an ordering among symbol definitions using the temporal order in which the objects containing the definitions were loaded, such that the definition first loaded has priority over definitions added later. Load ordering is used in relocation processing. Dependency ordering uses a "breadth-first" order starting with a given object, then all of its dependencies, then any dependents of those, iterating until all dependencies are satisfied. With the exception of the global symbol object obtained via a dlopen operation on a file with value 0, dependency ordering is used by the dlsym function. Load ordering is used in dlsym operations upon the global symbol object.

When an object is first made accessible via dlopen, it and its dependent objects are added in dependency order. Once all the objects are added, relocations are performed using load order. Note that if an object or its dependencies had been loaded by a previous dlopen invocation or on startup, the load and dependency orders may yield different resolutions.

The symbols introduced by dlopen operations and available through dlsym are those which are "exported" as symbols of global scope by the object. For shared objects, such symbols will typically be those that were specified in (for example) C source code as having extern linkage. For a.outs, only a subset of externally visible symbols are typically exported: specifically those referenced by the shared objects with which the a.out is linked. The exact set of exported symbols for any shared object or the a.out can be controlled using the linker [see ld].

The environment variable LD_LIBRARY_PATH should contain a colon-separated list of directories in the same format as the PATH variable [see sh]. LD_LIBRARY_PATH is ignored if the process' real user id is different from its effective user id or its real group id is different from its effective group id [see exec (exec: execl, execle, execlp, execv, execve, execvp, or exect Subroutine)] or if the process has acquired any privileges

Return values

If file cannot be found, cannot be opened for reading, is not a shared object, or if an error occurs during the process of loading file or relocating its symbolic references, dlopen returns NULL. More detailed diagnostic information is available through dlerror.

Related Information

The cc command, ld command, and sh command.

The dladdr (dladdr Subroutine) subroutine, dlclose (dlclose Subroutine) subroutine, dlerror (dlerror Subroutine) subroutine, dlsym (dladdr Subroutine) subroutine, and exec (exec: execl, execle, execlp, execv, execve, execvp, or exect Subroutine) subroutine.


[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]