]> git.saurik.com Git - apple/dyld.git/blob - doc/man/man3/dlopen.3
dyld-210.2.3.tar.gz
[apple/dyld.git] / doc / man / man3 / dlopen.3
1 .Dd Nov 7, 2011
2 .Os
3 .Dt DLOPEN 3
4 .Sh NAME
5 .Nm dlopen
6 .Nd load and link a dynamic library or bundle
7 .Sh SYNOPSIS
8 .In dlfcn.h
9 .Ft void*
10 .Fn dlopen "const char* path" "int mode"
11 .Sh DESCRIPTION
12 .Fn dlopen
13 examines the mach-o file specified by
14 .Fa path .
15 If the file is compatible with the current process and has not already been
16 loaded into the current process, it is loaded and linked. After being linked,
17 if it contains any initializer functions, they are called, before
18 .Fn dlopen
19 returns.
20 .Fn dlopen
21 can load dynamic libraries and bundles. It returns a handle that can
22 be used with
23 .Fn dlsym
24 and
25 .Fn dlclose .
26 A second call to
27 .Fn dlopen
28 with the same path will return the same handle, but the internal reference
29 count for the handle will be incremented. Therefore all
30 .Fn dlopen
31 calls should be balanced with a
32 .Fn dlclose
33 call.
34 .Pp
35 If a null pointer is passed in
36 .Fa path ,
37 .Fn dlopen
38 returns a handle equivalent to RTLD_DEFAULT.
39 .Pp
40 .Fa mode
41 contains options to
42 .Fn dlopen .
43 It must contain one or more of the following values, possibly ORed together:
44 .Pp
45 .Bl -tag -width RTLD_LAZYX
46 .It Dv RTLD_LAZY
47 Each external function reference is bound the first time the function is called.
48 .It Dv RTLD_NOW
49 All external function references are bound immediately during the call to
50 .Fn dlopen .
51 .El
52 .Pp
53 .Dv RTLD_LAZY
54 is normally preferred, for reasons of efficiency.
55 However,
56 .Dv RTLD_NOW
57 is useful to ensure that any undefined symbols are discovered during the
58 call to
59 .Fn dlopen .
60 If neither
61 RTLD_LAZY nor RTLD_NOW is specified, the default is RTLD_LAZY.
62 .Pp
63 One of the following flags may be ORed into the
64 .Fa mode
65 argument:
66 .Bl -tag -width RTLD_LOCALX
67 .It Dv RTLD_GLOBAL
68 Symbols exported from this image (dynamic library or bundle) will be available to any
69 images build with -flat_namespace option to
70 .Xr ld 1
71 or to calls to
72 .Fn dlsym
73 when using a special handle.
74 .It Dv RTLD_LOCAL
75 Symbols exported from this image (dynamic library or bundle) are generally hidden
76 and only availble to
77 .Fn dlsym
78 when directly using the handle returned by this call to
79 .Fn dlopen .
80 .Pp
81 .El
82 If neither
83 RTLD_GLOBAL nor RTLD_LOCAL is specified, the default is RTLD_GLOBAL.
84 .Pp
85 One of the following may be ORed into the
86 .Fa mode
87 argument:
88 .Bl -tag -width RTLD_NODELETEX
89 .It Dv RTLD_NOLOAD
90 The specified image is not loaded. However, a valid
91 .Fa handle
92 is returned if the image already exists in the process. This provides a way
93 to query if an image is already loaded. The
94 .Fa handle
95 returned is ref-counted, so you eventually need a corresponding call to
96 .Fn dlclose
97 .It Dv RTLD_NODELETE
98 The specified image is tagged so that will never be removed from the address space,
99 even after all clients have released it via
100 .Fn dlclose
101 .El
102 .Pp
103 Additionally, the following may be ORed into the
104 .Fa mode
105 argument:
106 .Bl -tag -width RTLD_FIRSTX
107 .It Dv RTLD_FIRST
108 The retuned
109 .Fa handle
110 is tagged so that any
111 .Fn dlsym
112 calls on the
113 .Fa handle
114 will only search the image specified, and not subsequent images. If
115 .Fa path
116 is NULL and the option RTLD_FIRST is used, the
117 .Fa handle
118 returned will only search the main executable.
119 .El
120 .Sh SEARCHING
121 .Fn dlopen
122 searches for a compatible Mach-O file in the directories specified by a set of environment variables and
123 the process's current working directory.
124 When set, the environment variables must contain a colon-separated list of directory paths,
125 which can be absolute or relative to the current working directory. The environment variables
126 are LD_LIBRARY_PATH, DYLD_LIBRARY_PATH, and DYLD_FALLBACK_LIBRARY_PATH, but if the path specified
127 is formatted like a framework, then DYLD_FRAMEWORK_PATH and DYLD_FALLBACK_FRAMEWORK_PATH are
128 used instead.
129 The non-fallback variables have no default value. The default value of DYLD_FALLBACK_LIBRARY_PATH
130 is $HOME/lib:/usr/local/lib:/usr/lib, and DYLD_FALLBACK_FRAMEWORK_PATH is
131 $HOME/Library/Frameworks:/Library/Frameworks:/Network/Library/Frameworks:/System/Library/Frameworks.
132 .Fn dlopen
133 searches the directories specified in the environment variables in the order they are listed.
134 .Pp
135 When
136 .Fa path
137 doesn't contain a slash character (i.e. it is just a leaf name and therefore not a framework),
138 .Fn dlopen
139 searches the following the following until it finds a compatible Mach-O file: $LD_LIBRARY_PATH,
140 $DYLD_LIBRARY_PATH, current working directory, $DYLD_FALLBACK_LIBRARY_PATH.
141 .Pp
142 When
143 .Fa path
144 contains a slash (i.e. a full path or a partial path)
145 .Fn dlopen
146 searches the following the following until it finds a compatible Mach-O file:
147 $DYLD_LIBRARY_PATH (with leaf name from
148 .Fa path
149 ), then the supplied
150 .Fa path
151 (using current working directory for partial paths).
152 .Pp
153 Note: There are no configuration files to control dlopen searching.
154 .Pp
155 Note: If the main executable is a set[ug]id binary or codesigned with entitlements,
156 then all environment variables are ignored, and only a full path can be used.
157 .Pp
158 Note: Mac OS X uses "universal" files to combine 32-bit and 64-bit libraries. This means there are no separate 32-bit and 64-bit search paths.
159 .Pp
160 .Sh RETURN VALUES
161 If
162 .Fn dlopen
163 fails, it returns a null pointer, and sets an error condition which may be interrogated with
164 .Fn dlerror .
165 .Sh AUTHORS
166 Mac OS X 10.3 incorporated the dlcompat package written by Jorge Acereda <jacereda@users.sourceforge.net>
167 and Peter O'Gorman <ogorman@users.sourceforge.net>.
168 .Pp
169 In Mac OS X 10.4, dlopen was rewritten to be a native part of dyld.
170 .Pp
171 .Sh SEE ALSO
172 .Xr dlopen_preflight 3
173 .Xr dlclose 3
174 .Xr dlsym 3
175 .Xr dlerror 3
176 .Xr dyld 3
177 .Xr ld 1