United States-English |
|
|
HP-UX Reference > Ddlopen_ia(3C)Integrity Systems OnlyHP-UX 11i Version 3: February 2007 |
|
NAMEdlopen_ia: dlopen(), dlopene() — open a shared library on Integrity systems SYNOPSISCommand: cc [flag]... cfile... -ldl [library]... #include <dlfcn.h> void *dlopen(const char *file, int mode); void *dlopene(const char *file, int mode, struct dlopen_opts *opts); RemarksThis manpage describes dlopen() on Integrity systems. For dlopen() on HP 9000 systems, see dlopen_pa(3C). DESCRIPTIONdlopen() and dlopene() are members of a family of routines that give the user direct access to the dynamic linking facilities (using the -ldl option on the compiler or ld command line). dlopen() makes a shared object specified by a 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(). dlopene() is an extension to dlopen() which allows the caller to specify explicitly the placement of a shared library's text and data segment when the library is dynamically loaded. A successful dlopen() or dlopene() call returns to the process a handle which 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 path name to the object file. If file contains a slash character, the file argument itself is used as the path name. Otherwise dlopen() searches a series of directories, in the following order, for file:
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 path names 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:
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:
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(3C)). To determine the scope of symbols that are made available for relocation processing of objects loaded in a dlopen() invocation, the mode parameter can be bitwise or'ed with one of the following values:
The default modes for dlopen() are RTLD_WORLD|RTLD_GROUP. These flags are OR'ed together when the same object is loaded with different modes. The following flags do not affect relocation processing but provide other features:
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 a 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 a value 0, dependency ordering is used by the dlsym() function. Load ordering is used in dlsym() operations on 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 objects are added, relocations are performed using load order. Note that if an object and its dependencies have been loaded by a previous dlopen() invocation or on startup, the load and dependency order 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 are typically those that were specified in (for example) C source code as having extern linkage. For a.out's, 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(1)). The dlopen_opts structure has the following members: struct dlopen_opts { long flags; char* text_addr; char* data_addr; }; flags contains the load option, defined by logical OR of the following values:
The dynamic loader only accesses the address fields that are specified by the flags fields. text_addr contains the explicit base address for the shared library's text segment. data_addr contains the explicit base address for the shared library's data segment. Both the text_addr and data_addr must be aligned at a 16-byte boundary. The caller can invoke dlgetfileinfo() to obtain the information needed to allocate memory for the load segments. The caller of dlopene() is responsible for allocating memory with the appropriate permission:
RETURN VALUEIf 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() or dlerrno(). ERRORSIf dlopen() or dlopene() fails, a subsequent call to dlerrno() returns one of the following values:
EXAMPLESThe following example illustrates the use of dlopene() to load a shared library with an explicit data segment address. For simplicity, error checking has been omitted. #include <dlfcn.h> #include <sys/mman.h> int main() { struct dlfileinfo info; void *handle; struct dlopen_opts opts; int status; memset(&info, 0, sizeof(info)); memset(&opts, 0, sizeof(opts)); /* Get file info */ status = dlgetfileinfo("libfoo.so", sizeof(info), &info); opts.flags = RTLD_EXT_DATA_ADDR; /* allocate memory for the data segment */ opts.data_addr = (char*) mmap(0, info.data_size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0); /* call dlopene */ handle = dlopene("libfoo.so", RTLD_NOW|RTLD_GLOBAL, &opts); /* Insert user code to use library */ /* close library */ status = dlclose(handle); /* free memory */ munmap(opts.data_addr, info.data_size); } WARNINGSThe environment variable LD_LIBRARY_PATH and SHLIB_PATH should contain a colon-separated list of directories, in the same format as the PATH variable (see sh(1)). LD_LIBRARY_PATH and SHLIB_PATH are 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(2)). With the +compat option specified, LD_LIBRARY_PATH and +b embedded path are ignored while searching for dependent libraries. Use caution when building shared libraries with external library dependencies. Any library that contains Thread Local Storage (TLS) and uses static TLS model should not be used as a dependency. See Thread Local Storage in dld.so(5) for more information. If a dependent library contains TLS, was built with static TLS model, and was not loaded during program startup (that is, not linked against the executable), the dynamic loader fails to perform the operation. You can use +tls=dynamic compiler option to re-compile the library to avoid errors. SEE ALSOcc(1), ld(1), sh(1), exec(2), dlclose(3C), dlerrno(3C), dlerror(3C), dlgetfileinfo(3C), dlsetlibpath(3C), dlsym(3C). |
Printable version | ||
|