dlopen.html

来自「unix 下的C开发手册,还用详细的例程。」· HTML 代码 · 共 258 行

HTML
258
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>dlopen</title></head><body bgcolor=white><center><font size=2>The Single UNIX &reg; Specification, Version 2<br>Copyright &copy; 1997 The Open Group</font></center><hr size=2 noshade><h4><a name = "tag_000_003_142">&nbsp;</a>NAME</h4><blockquote>dlopen - gain access to an executable object file</blockquote><h4><a name = "tag_000_003_143">&nbsp;</a>SYNOPSIS</h4><blockquote><pre><code>#include &lt;<a href="dlfcn.h.html">dlfcn.h</a>&gt;void *dlopen(const char *<i>file</i>, int <i>mode</i>);</code></pre></blockquote><h4><a name = "tag_000_003_144">&nbsp;</a>DESCRIPTION</h4><blockquote><i>dlopen()</i>makes an executable object file specified by <i>file</i>available to the calling program.The class of files eligible for this operation and the manner of theirconstruction are specified by the implementation, though typically such files areexecutable objects such as shared libraries, relocatable files or programs.Note that some implementations permit the construction of dependenciesbetween such objects that are embedded within files. In such cases, a<i>dlopen()</i>operation will load such dependencies in addition to the objectreferenced by <i>file</i>.Implementations may also impose specificconstraints on the construction of programs that can employ <i>dlopen()</i>and its related services. <p>A successful<i>dlopen()</i>returns a <i>handle</i> which the caller may use on subsequent calls to<i><a href="dlsym.html">dlsym()</a></i>and<i><a href="dlclose.html">dlclose()</a></i>.The value of this <i>handle</i>should not be interpreted in any way by the caller.<p><i>file</i>is used to construct a pathname to the object file. If<i>file</i>contains a slash character, the<i>file</i>argument is used as the pathname for the file. Otherwise, <i>file</i>is used in animplementation-dependent manner to yield a pathname. <p>If the value of <i>file</i> is 0, <i>dlopen()</i>provides a <i>handle</i> on a global symbol object.This object provides access to the symbolsfrom an ordered set of objects consisting ofthe original program image file, together withany objects loaded at program startup as specified by that process image file(for example, shared libraries), and the set of objects loaded using a <i>dlopen()</i>operation together with theRTLD_GLOBALflag. As the latter set of objects can change during execution, the setidentified by <i>handle</i> can also change dynamically. <p>Only a single copy of an object file is brought into theaddress space, even if <i>dlopen()</i>is invoked multiple times in reference to the file, and even if differentpathnames are used to reference the file.<p>The <i>mode</i> parameter describes how<i>dlopen()</i>will operate upon <i>file</i>with respect to the processing of relocations and the scope of visibility ofthe symbols provided within <i>file</i>.When an object is brought into the address space of a process,it may contain references to symbols whose addresses are not knownuntil the object is loaded. These references must be relocatedbefore the symbols can be accessed. The <i>mode</i>parameter governs when these relocations take place and mayhave the following values:<dl compact><dt>RTLD_LAZY<dd>Relocations are performed at an implementation-dependent time,ranging from the time of the<i>dlopen()</i>call until the first reference to a given symbol occurs. SpecifyingRTLD_LAZYshould improve performance on implementations supporting dynamic symbolbinding as a process may not reference all of the functions in any givenobject. And, for systems supporting dynamic symbol resolution fornormal process execution, this behaviour mimics the normal handling of processexecution.<dt>RTLD_NOW<dd>All necessary relocations are performed when the object is first loaded.This may waste some processing if relocations are performedfor functions that are never referenced. This behaviour may be useful forapplications that need to know as soon as an object is loadedthat all symbols referenced during execution will be available.</dl><p>Any object loaded by <i>dlopen()</i>that requires relocationsagainst global symbols can reference the symbols in the originalprocess image file, any objects loaded at program startup, from theobject itself as well as any other object included in the same<i>dlopen()</i>invocation, and any objects that were loaded in any <i>dlopen()</i>invocation and which specified the RTLD_GLOBAL flag.To determine the scope of visibility for the symbols loaded with a<i>dlopen()</i>invocation, the<i>mode</i>parameter should be bitwise or'ed with one of the following values:<dl compact><dt>RTLD_GLOBAL<dd>The object's symbols are made available for the relocation processingof any other object. In addition, symbol lookup using<i>dlopen</i>(<i>0, mode</i>)and an associated<i><a href="dlsym.html">dlsym()</a></i>allows objects loaded with this<i>mode</i>to be searched.<dt>RTLD_LOCAL<dd>The object's symbols are not made available for the relocation processingof any other object.</dl><p>If neither RTLD_GLOBAL nor RTLD_LOCALare specified, then an implementation-specified default behaviour will beapplied.<p>If a <i>file</i> is specified in multiple<i>dlopen()</i>invocations, <i>mode</i>is interpreted at each invocation. Note, however, that onceRTLD_NOWhas been specified all relocations will have been completed renderingfurtherRTLD_NOWoperations redundant and any furtherRTLD_LAZYoperations irrelevant. Similarly note that onceRTLD_GLOBALhas been specified the object will maintain theRTLD_GLOBALstatus regardless of any previous or future specification ofRTLD_LOCAL,so long as the object remains in the address space (see <i><a href="dlclose.html">dlclose()</a></i>).<p>Symbols introduced into a program through calls to<i>dlopen()</i>may be used in relocation activities. Symbols so introduced may duplicatesymbols already defined by the program or previous<i>dlopen()</i>operations. To resolve the ambiguities such a situation might present, theresolution of a symbol reference to symbol definition is based on a symbolresolution order.Two such resolution orders are defined: <i>load</i>or <i>dependency</i>ordering.<i>Load</i>order establishes an ordering among symbol definitions, such that thedefinition first loaded (including definitions from the image file and anydependent objects loaded with it) has priority over objects added later (via <i>dlopen()</i>).<i>Load</i> ordering is used in relocation processing.<i>Dependency</i>ordering uses a breadth-first order starting with a given object, then allof its dependencies, then any dependents of those, iterating until alldependencies are satisfied.With the exception of the global symbol object obtained via a<i>dlopen()</i>operation on a <i>file</i> of 0, <i>dependency</i> ordering is used by the <i><a href="dlsym.html">dlsym()</a></i>function.<i>Load</i> ordering is used in <i><a href="dlsym.html">dlsym()</a></i>operations upon the global symbol object.<p>When an object is first made accessible via<i>dlopen()</i>it and its dependent objects are added in<i>dependency</i>order. Once all the objects are added, relocations are performed using<i>load</i>order. Note that if an object or its dependencies had been previouslyloaded, the <i>load</i> and <i>dependency</i>orders may yield different resolutions.<p>The symbols introduced by<i>dlopen()</i>operations, and available through<i><a href="dlsym.html">dlsym()</a></i>are at a minimum those which are exported as symbols of global scope by the object. Typically such symbols will be those that were specified in(for example) C source code as having <i>extern</i> linkage. The precise manner in which an implementation constructs the set of exported symbols for a<i>dlopen()</i>object is specified by that implementation.</blockquote><h4><a name = "tag_000_003_145">&nbsp;</a>RETURN VALUE</h4><blockquote>If<i>file</i>cannot be found, cannot be opened for reading, is not of an appropriateobject format for processing by<i>dlopen()</i>,or if an error occurs during the process of loading<i>file</i>or relocating its symbolic references, <i>dlopen()</i>will return NULL.More detailed diagnostic information will beavailable through<i><a href="dlerror.html">dlerror()</a></i>.</blockquote><h4><a name = "tag_000_003_146">&nbsp;</a>ERRORS</h4><blockquote>No errors are defined.</blockquote><h4><a name = "tag_000_003_147">&nbsp;</a>EXAMPLES</h4><blockquote>None.</blockquote><h4><a name = "tag_000_003_148">&nbsp;</a>APPLICATION USAGE</h4><blockquote>None.</blockquote><h4><a name = "tag_000_003_149">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>None.</blockquote><h4><a name = "tag_000_003_150">&nbsp;</a>SEE ALSO</h4><blockquote><i><a href="dlclose.html">dlclose()</a></i>,<i><a href="dlerror.html">dlerror()</a></i>,<i><a href="dlsym.html">dlsym()</a></i>.</blockquote><hr size=2 noshade><center><font size=2>UNIX &reg; is a registered Trademark of The Open Group.<br>Copyright &copy; 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?