OSDN Git Service

(split) LDP_man-pages: release ioctl.2 [JM:00378]
[linuxjm/LDP_man-pages.git] / draft / man3 / dlopen.3
1 .\" -*- nroff -*-
2 .\" Copyright 1995 Yggdrasil Computing, Incorporated.
3 .\" written by Adam J. Richter (adam@yggdrasil.com),
4 .\" with typesetting help from Daniel Quinlan (quinlan@yggdrasil.com).
5 .\" and Copyright 2003 Michael Kerrisk (mtk.manpages@gmail.com).
6 .\"
7 .\" This is free documentation; you can redistribute it and/or
8 .\" modify it under the terms of the GNU General Public License as
9 .\" published by the Free Software Foundation; either version 2 of
10 .\" the License, or (at your option) any later version.
11 .\"
12 .\" The GNU General Public License's references to "object code"
13 .\" and "executables" are to be interpreted as the output of any
14 .\" document formatting or typesetting system, including
15 .\" intermediate and printed output.
16 .\"
17 .\" This manual is distributed in the hope that it will be useful,
18 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
19 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 .\" GNU General Public License for more details.
21 .\"
22 .\" You should have received a copy of the GNU General Public
23 .\" License along with this manual; if not, write to the Free
24 .\" Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
25 .\" USA.
26 .\"
27 .\" Modified by David A. Wheeler <dwheeler@dwheeler.com> 2000-11-28.
28 .\" Applied patch by Terran Melconian, aeb, 2001-12-14.
29 .\" Modified by Hacksaw <hacksaw@hacksaw.org> 2003-03-13.
30 .\" Modified by Matt Domsch, 2003-04-09: _init and _fini obsolete
31 .\" Modified by Michael Kerrisk <mtk.manpages@gmail.com> 2003-05-16.
32 .\" Modified by Walter Harms: dladdr, dlvsym
33 .\" Modified by Petr Baudis <pasky@suse.cz>, 2008-12-04: dladdr caveat
34 .\"
35 .\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved.
36 .\" Translated Sat May 23 1998 by NAKANO Takeo <nakano@apm.seikei.ac.jp>
37 .\" Updated & Modified 1999-09-14, NAKANO Takeo
38 .\" Modified 2000-03-19, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
39 .\" Updated 2001-02-16, Kentaro Shirakata <argrath@ub32.org>
40 .\" Updated 2001-12-21, Kentaro Shirakata <argrath@ub32.org>
41 .\" Updated 2002-10-21, Kentaro Shirakata <argrath@ub32.org>
42 .\" Updated 2003-09-01, Kentaro Shirakata <argrath@ub32.org>
43 .\" Updated 2005-03-15, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
44 .\" Updated 2006-01-20, Akihiro MOTOKI
45 .\" Updated 2009-03-02, Akihiro MOTOKI, LDP v3.19
46 .\"
47 .\"WORD:        linker                  ¥ê¥ó¥«
48 .\"WORD:        dynamic linking         Æ°Åª¥ê¥ó¥¯
49 .\"WORD:        dynamic library         Æ°Åª¥é¥¤¥Ö¥é¥ê
50 .\"
51 .TH DLOPEN 3 2008-12-06 "Linux" "Linux Programmer's Manual"
52 .\"O .SH NAME
53 .\"O dlclose, dlerror, dlopen, dlsym \- Programming interface to dynamic linking loader
54 .SH Ì¾Á°
55 dlclose, dlerror, dlopen, dlsym \- Æ°Åª¥ê¥ó¥¯¤ò¹Ô¤¦¥í¡¼¥À¤Ø¤Î
56 ¥×¥í¥°¥é¥ß¥ó¥°¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹
57 .\"O .SH SYNOPSIS
58 .SH ½ñ¼°
59 .B #include <dlfcn.h>
60 .sp
61 .BI "void *dlopen(const char *" filename ", int " flag );
62 .sp
63 .B "char *dlerror(void);"
64 .sp
65 .BI "void *dlsym(void *" handle ", const char *" symbol );
66 .sp
67 .BI "int dlclose(void *" handle );
68 .sp
69 .\"O Link with \fI\-ldl\fP.
70 \fI\-ldl\fP ¤Ç¥ê¥ó¥¯¤¹¤ë¡£
71 .\"O .SH DESCRIPTION
72 .SH ÀâÌÀ
73 .\"O The four functions
74 .\"O .BR dlopen (),
75 .\"O .BR dlsym (),
76 .\"O .BR dlclose (),
77 .\"O .BR dlerror ()
78 .\"O implement the interface to the dynamic linking loader.
79 .BR dlopen (),
80 .BR dlsym (),
81 .BR dlclose (),
82 .BR dlerror ()
83 ¤Î 4¤Ä¤Î´Ø¿ô¤Ï¡¢Æ°Åª¥ê¥ó¥¯ (dynamic linking) ¤ò¹Ô¤¦¥í¡¼¥À¤Ø¤Î
84 ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò¼ÂÁõ¤·¤¿¤â¤Î¤Ç¤¢¤ë¡£
85 .SS "dlerror()"
86 .\"O The function
87 .\"O .BR dlerror ()
88 .\"O returns a human readable string describing the most recent error
89 .\"O that occurred from
90 .\"O .BR dlopen (),
91 .\"O .BR dlsym ()
92 .\"O or
93 .\"O .BR dlclose ()
94 .\"O since the last call to
95 .\"O .BR dlerror ().
96 .\"O It returns NULL if no errors have occurred since initialization or since
97 .\"O it was last called.
98 ´Ø¿ô
99 .BR dlerror ()
100 ¤Ï¡¢Á°²ó
101 .BR dlerror ()
102 ¤¬¸Æ¤Ó½Ð¤µ¤ì¤¿¸å¤Ë¡¢
103 .BR dlopen (),
104 .BR dlsym (),
105 .BR dlclose ()
106 ¤Î¤¤¤º¤ì¤«¤ÇºÇ¸å¤ËȯÀ¸¤·¤¿¥¨¥é¡¼¤Ë¤Ä¤¤¤Æ¤ÎÀâÌÀ¥á¥Ã¥»¡¼¥¸¤òÊÖ¤¹¡£
107 ½é´ü²½¸å¤Þ¤¿¤ÏÁ°²ó¸Æ¤Ó½Ð¤µ¤ì¤¿¸å¤Ç¡¢¥¨¥é¡¼¤¬È¯À¸¤·¤Æ¤¤¤Ê¤±¤ì¤Ð NULL ¤òÊÖ¤¹¡£
108 .SS "dlopen()"
109 .\"O The function
110 .\"O .BR dlopen ()
111 .\"O loads the dynamic library file named by the null-terminated
112 .\"O string
113 .\"O .I filename
114 .\"O and returns an opaque "handle" for the dynamic library.
115 ´Ø¿ô
116 .BR dlopen ()
117 ¤Ï¡¢NULL ½ªÃ¼¤µ¤ì¤¿Ê¸»úÎó
118 .I filename
119 ¤Ç»ØÄꤵ¤ì¤¿¥Õ¥¡¥¤¥ë̾¤ÎưŪ¥é¥¤¥Ö¥é¥ê (dynamic library) ¤ò¥í¡¼¥É¤·¡¢
120 ¤½¤ÎưŪ¥é¥¤¥Ö¥é¥ê¤Ø¤ÎÆâÉô¡Ö¥Ï¥ó¥É¥ë¡×¤òÊÖ¤¹¡£
121 .\"O If
122 .\"O .I filename
123 .\"O is NULL, then the returned handle is for the main program.
124 .\"O If
125 .\"O .I filename
126 .\"O contains a slash ("/"), then it is interpreted as a (relative
127 .\"O or absolute) pathname.
128 .I filename
129 ¤¬ NULL ¤Î¾ì¹ç¡¢¥á¥¤¥ó¡¦¥×¥í¥°¥é¥à¤Ø¤Î¥Ï¥ó¥É¥ë¤¬ÊÖ¤µ¤ì¤ë¡£
130 .I filename
131 ¤¬¥¹¥é¥Ã¥·¥å ("/") ¤ò´Þ¤à¾ì¹ç¡¢(ÁêÂФ«ÀäÂФ«¤Î)¥Ñ¥¹Ì¾¤È¤·¤Æ²ò¼á¤µ¤ì¤ë¡£
132 .\"O Otherwise, the dynamic linker searches for the library as follows
133 .\"O (see
134 .\"O .BR ld.so (8)
135 .\"O for further details):
136 ¤½¤ì°Ê³°¤Î¾ì¹ç¡¢Æ°Åª¥ê¥ó¥«¤Ï°Ê²¼¤Î¼ê½ç¤Ç¥é¥¤¥Ö¥é¥ê¤ò¸¡º÷¤¹¤ë
137 (¾ÜºÙ¤Ï
138 .BR ld.so (8)
139 ¤ò»²¾È):
140 .IP o 4
141 .\"O (ELF only) If the executable file for the calling program
142 .\"O contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag,
143 .\"O then the directories listed in the DT_RPATH tag are searched.
144 (ELF ¤Î¤ß) ¸Æ¤Ó½Ð¤·¸µ¥×¥í¥°¥é¥à¤Î¼Â¹Ô¥Õ¥¡¥¤¥ë¤Ë DT_RPATH ¥¿¥°¤¬´Þ¤Þ¤ì¤Æ¤ª¤ê¡¢
145 DT_RUNPATH ¥¿¥°¤¬´Þ¤Þ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¡¢DT_RPATH ¥¿¥°¤Ë½ñ¤«¤ì¤Æ¤¤¤ë
146 ¥Ç¥£¥ì¥¯¥È¥ê¡¦¥ê¥¹¥È¤ò¸¡º÷¤¹¤ë¡£
147 .IP o
148 .\"O If, at the time that the program was started, the environment variable
149 .\"O .B LD_LIBRARY_PATH
150 .\"O was defined to contain a colon-separated list of directories,
151 .\"O then these are searched.
152 .\"O (As a security measure this variable is ignored for set-user-ID and
153 .\"O set-group-ID programs.)
154 ¥×¥í¥°¥é¥à¤Î³«»Ï»þ¤Ë´Ä¶­ÊÑ¿ô
155 .B LD_LIBRARY_PATH
156 ¤Ë¥³¥í¥ó¶èÀÚ¤ê¤Î¥Ç¥£¥ì¥¯¥È¥ê¤Î¥ê¥¹¥È¤¬ÄêµÁ¤µ¤ì¤Æ¤¤¤ì¤Ð¡¢
157 ¤³¤Î´Ä¶­ÊÑ¿ô¤ËÄêµÁ¤µ¤ì¤¿¥Ç¥£¥ì¥¯¥È¥ê¤¬¸¡º÷¤µ¤ì¤ë
158 (¥»¥­¥å¥ê¥Æ¥£¾å¤ÎÍýͳ¤Ç¡¢¤³¤ÎÊÑ¿ô¤Ï set-UID ¤ä set-GID ¤µ¤ì¤¿
159 ¥×¥í¥°¥é¥à¤Î¾ì¹ç¤Ï̵»ë¤µ¤ì¤ë)¡£
160 .IP o
161 .\"O (ELF only) If the executable file for the calling program
162 .\"O contains a DT_RUNPATH tag, then the directories listed in that tag
163 .\"O are searched.
164 (ELF ¤Î¤ß) ¸Æ¤Ó½Ð¤·¸µ¥×¥í¥°¥é¥à¤Î¼Â¹Ô¥Õ¥¡¥¤¥ë¤Ë DT_RUNPATH ¥¿¥°¤¬´Þ¤Þ¤ì¤Æ
165 ¤¤¤ë¾ì¹ç¡¢¤½¤Î¥¿¥°¤Ë½ñ¤«¤ì¤Æ¤¤¤ë¥Ç¥£¥ì¥¯¥È¥ê¡¦¥ê¥¹¥È¤ò¸¡º÷¤¹¤ë¡£
166 .IP o
167 .\"O The cache file
168 .\"O .I /etc/ld.so.cache
169 .\"O (maintained by
170 .\"O .BR ldconfig (8))
171 .\"O is checked to see whether it contains an entry for
172 .\"O .IR filename .
173 ¥­¥ã¥Ã¥·¥å¥Õ¥¡¥¤¥ë
174 .I /etc/ld.so.cache
175 ¤ÎÃæ¤Ë
176 .I filename
177 ¤Î¥¨¥ó¥È¥ê¤¬Æþ¤Ã¤Æ¤¤¤ë¤«¤ò¥Á¥§¥Ã¥¯¤¹¤ë
178 .RB ( /etc/ld.so.cache
179 ¤Ï
180 .BR ldconfig (8)
181 ¤Ë¤è¤Ã¤Æ´ÉÍý¤µ¤ì¤Æ¤¤¤ë)¡£
182 .IP o
183 .\"O The directories
184 .\"O .I /lib
185 .\"O and
186 .\"O .I /usr/lib
187 .\"O are searched (in that order).
188 ¥Ç¥£¥ì¥¯¥È¥ê
189 .I /lib
190 ¤È
191 .I /usr/lib
192 ¤ò¤³¤Î½çÈ֤Ǹ¡º÷¤¹¤ë¡£
193 .PP
194 .\"O If the library has dependencies on other shared libraries,
195 .\"O then these are also automatically loaded by the dynamic linker
196 .\"O using the same rules.
197 .\"O (This process may occur recursively,
198 .\"O if those libraries in turn have dependencies, and so on.)
199 ¤½¤Î¥é¥¤¥Ö¥é¥ê¤¬Â¾¤Î¶¦Í­¥é¥¤¥Ö¥é¥ê¤Ë°Í¸¤·¤Æ¤¤¤ë¾ì¹ç¤Ï¡¢
200 °Í¸¤·¤Æ¤¤¤ë¥é¥¤¥Ö¥é¥ê¤âưŪ¥ê¥ó¥«¤¬Æ±¤¸¸¡º÷¥ë¡¼¥ë¤Ë´ð¤Å¤¤¤Æ
201 ¼«Æ°Åª¤Ë¥í¡¼¥É¤¹¤ë (¤½¤ì¤é¤Î¥é¥¤¥Ö¥é¥ê¤Ë¤µ¤é¤Ë°Í¸´Ø·¸¤¬¤¢¤ë¾ì¹ç¤Ê¤É¤Ï
202 ¤³¤Î½èÍý¤ÏºÆµ¢Åª¤Ë¹Ô¤ï¤ì¤ë)¡£
203 .PP
204 .\"O One of the following two values must be included in
205 .\"O .IR flag :
206 .I flag
207 ¤Ë¤Ï°Ê²¼¤Î 2 ¤Ä¤ÎÃͤΤ¤¤º¤ì¤«¤ò´Þ¤á¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤:
208 .TP
209 .B RTLD_LAZY
210 .\"O Perform lazy binding.
211 .\"O Only resolve symbols as the code that references them is executed.
212 .\"O If the symbol is never referenced, then it is never resolved.
213 .\"O (Lazy binding is only performed for function references;
214 .\"O references to variables are always immediately bound when
215 .\"O the library is loaded.)
216 lazy binding (¼êÈ´¤­¤Ê¥·¥ó¥Ü¥ë¤Î·ë¤ÓÉÕ¤±) ¤¬¹Ô¤¦¡£
217 ¥·¥ó¥Ü¥ë¤Î²ò·è¤Ï¤½¤Î¥·¥ó¥Ü¥ë¤ò»²¾È¤¹¤ë¥³¡¼¥É¤¬¼Â¹Ô¤µ¤ì¤ë¤È¤­¤Ë¤Î¤ß
218 ¹Ô¤ï¤ì¤ë¡£¥·¥ó¥Ü¥ë¤¬°ìÅ٤⻲¾È¤µ¤ì¤Ê¤«¤Ã¤¿¾ì¹ç¤Ë¤Ï¡¢¤½¤Î¥·¥ó¥Ü¥ë¤Ï
219 ²ò·è¤µ¤ì¤Ê¤¤¤Þ¤Þ¤È¤Ê¤ë¡£
220 (lazy binding ¤Ï´Ø¿ô»²¾È¤Ë¤Ä¤¤¤Æ¤Î¤ß¼Â»Ü¤µ¤ì¤ë; ÊÑ¿ô¤Ø¤Î»²¾È¤Ï¾ï¤Ë
221 ¥é¥¤¥Ö¥é¥ê¤¬¥í¡¼¥É¤µ¤ì¤¿»þÅÀ¤Çľ¤Á¤Ë²ò·è¤µ¤ì¤ë¡£)
222 .TP
223 .B RTLD_NOW
224 .\"O If this value is specified, or the environment variable
225 .\"O .B LD_BIND_NOW
226 .\"O is set to a nonempty string,
227 .\"O all undefined symbols in the library are resolved before
228 .\"O .BR dlopen ()
229 .\"O returns.
230 .\"O If this cannot be done, an error is returned.
231 ¤³¤ÎÃͤ¬»ØÄꤵ¤ì¤ë¤«¡¢´Ä¶­ÊÑ¿ô
232 .B LD_BIND_NOW
233 ¤Ë¶õ¤Ç¤Ê¤¤Ê¸»úÎó¤¬ÀßÄꤵ¤ì¤¿¾ì¹ç¡¢
234 ¥é¥¤¥Ö¥é¥êÃæ¤Î̤ÄêµÁ¤Î¥·¥ó¥Ü¥ë¤òÁ´¤Æ²ò·è¤·¤Æ¤«¤é
235 .BR dlopen ()
236 ¤ÏÉüµ¢¤¹¤ë¡£²ò·è¤Ç¤­¤Ê¤«¤Ã¤¿¤È¤­¤Ë¤Ï¥¨¥é¡¼¤¬ÊÖ¤µ¤ì¤ë¡£
237 .PP
238 .\"O Zero or more of the following values may also be ORed in
239 .\"O .IR flag :
240 °Ê²¼¤ÎÃͤΤ¦¤Á 0 ¸Ä°Ê¾å¤òÏÀÍýÏ (OR) ¤Î·Á¤Ç
241 .I flag
242 ¤ËÄɲ乤뤳¤È¤â¤Ç¤­¤ë:
243 .TP
244 .B RTLD_GLOBAL
245 .\"O The symbols defined by this library will be
246 .\"O made available for symbol resolution of subsequently loaded libraries.
247 ¤³¤Î¥é¥¤¥Ö¥é¥ê¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë¥·¥ó¥Ü¥ë¤¬¡¢¤³¤ì¤è¤ê¸å¤Ç¥í¡¼¥É¤µ¤ì¤ë
248 ¥é¥¤¥Ö¥é¥ê¤Î¥·¥ó¥Ü¥ë²ò·è¤ÇÍøÍѤǤ­¤ë¤è¤¦¤Ë¤Ê¤ë¡£
249 .TP
250 .B RTLD_LOCAL
251 .\"O This is the converse of
252 .\"O .BR RTLD_GLOBAL ,
253 .\"O and the default if neither flag is specified.
254 .\"O Symbols defined in this library are not made available to resolve
255 .\"O references in subsequently loaded libraries.
256 ¤³¤Î¥Õ¥é¥°¤Ï
257 .B RTLD_GLOBAL
258 ¤ÎÈ¿ÂФΰÕÌ£¤Ç¤¢¤ê¡¢¤É¤Á¤é¤Î¥Õ¥é¥°¤â»ØÄꤵ¤ì¤Ê¤«¤Ã¤¿¾ì¹ç¤Ï
259 ¤³¤Á¤é¤¬¥Ç¥Õ¥©¥ë¥È¤È¤Ê¤ë¡£
260 ¤³¤Î¥é¥¤¥Ö¥é¥ê¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë¥·¥ó¥Ü¥ë¤Ï¡¢¤³¤ì¤è¤ê¸å¤Ç¥í¡¼¥É¤µ¤ì¤ë
261 ¥é¥¤¥Ö¥é¥ê¤Ç¤Î¥·¥ó¥Ü¥ë»²¾È¤ÇÍøÍѤǤ­¤Ê¤¤¡£
262 .TP
263 .\"O .BR RTLD_NODELETE " (since glibc 2.2)"
264 .BR RTLD_NODELETE " (glibc 2.2 °Ê¹ß)"
265 .\"O Do not unload the library during
266 .\"O .BR dlclose ().
267 .\"O Consequently, the library's static variables are not reinitialized
268 .\"O if the library is reloaded with
269 .\"O .BR dlopen ()
270 .\"O at a later time.
271 .\"O This flag is not specified in POSIX.1-2001.
272 .\"O .\" (But it is present on Solaris.)
273 .BR dlclose ()
274 Ãæ¤Ë¤½¤Î¥é¥¤¥Ö¥é¥ê¤ò¥¢¥ó¥í¡¼¥É¤·¤Ê¤¤¡£
275 ¤½¤Î¤¿¤á¡¢Æ±¤¸¥é¥¤¥Ö¥é¥ê¤ò¤³¤ì°Ê¹ß¤Ë
276 .BR dlopen ()
277 ¤ÇºÆÅÙ¥í¡¼¥É¤·¤¿¾ì¹ç¤Ë¡¢¥é¥¤¥Ö¥é¥êÆâ¤ÎÀÅŪÊÑ¿ô¤ÏºÆ½é´ü²½¤µ¤ì¤Ê¤¤¡£
278 ¤³¤Î¥Õ¥é¥°¤Ï POSIX.1-2001 ¤Ç¤Ïµ¬Äꤵ¤ì¤Æ¤¤¤Ê¤¤¡£
279 .\" (¤·¤«¤· Solaris ¤Ë¸ºß¤¹¤ë)
280 .TP
281 .\"O .BR RTLD_NOLOAD " (since glibc 2.2)"
282 .BR RTLD_NOLOAD " (glibc 2.2 °Ê¹ß)"
283 .\"O Don't load the library.
284 .\"O This can be used to test if the library is already resident
285 .\"O .RB ( dlopen ()
286 .\"O returns NULL if it is not, or the library's handle if it is resident).
287 .\"O This flag can also be used to promote the flags on a library
288 .\"O that is already loaded.
289 .\"O For example, a library that was previously loaded with
290 .\"O .B RTLD_LOCAL
291 .\"O can be reopened with
292 .\"O .BR RTLD_NOLOAD\ |\ RTLD_GLOBAL .
293 .\"O This flag is not specified in POSIX.1-2001.
294 .\"O .\" (But it is present on Solaris.)
295 .\"O .\"
296 ¤½¤Î¥é¥¤¥Ö¥é¥ê¤ò¥í¡¼¥É¤·¤Ê¤¤¡£
297 ¤³¤Î¥Õ¥é¥°¤Ï¤½¤Î¥é¥¤¥Ö¥é¥ê¤¬¤¹¤Ç¤ËÁȤ߹þ¤Þ¤ì¤Æ¤¤¤ë¤«¤ò¸¡ºº¤¹¤ë¤Î¤Ë
298 ÍøÍѤǤ­¤ë
299 .RB ( dlopen ()
300 ¤Ï¡¢¥é¥¤¥Ö¥é¥ê¤¬ÁȤ߹þ¤Þ¤ì¤Æ¤¤¤Ê¤±¤ì¤Ð NULL ¤òÊÖ¤·¡¢
301 ¤¹¤Ç¤ËÁȤ߹þ¤Þ¤ì¤Æ¤¤¤ì¤Ð¤½¤Î¥é¥¤¥Ö¥é¥ê¤Î¥Ï¥ó¥É¥ë¤òÊÖ¤¹)¡£
302 ¤Þ¤¿¡¢¤¹¤Ç¤Ë¥í¡¼¥É¤µ¤ì¤Æ¤¤¤ë¥é¥¤¥Ö¥é¥ê¤Î¥Õ¥é¥°¤ò¾º³Ê¤µ¤»¤ë¤Î¤Ë¤â
303 ÍøÍѤǤ­¤ë¡£Î㤨¤Ð¡¢²áµî¤Ë
304 .B RTLD_LOCAL
305 ¤Ç¥í¡¼¥É¤·¤¿¥é¥¤¥Ö¥é¥ê¤ò
306 .BR RTLD_NOLOAD\ |\ RTLD_GLOBAL
307 ¤ÇºÆ¥ª¡¼¥×¥ó¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
308 ¤³¤Î¥Õ¥é¥°¤Ï POSIX.1-2001 ¤Ç¤Ïµ¬Äꤵ¤ì¤Æ¤¤¤Ê¤¤¡£
309 .\" (¤·¤«¤· Solaris ¤Ë¸ºß¤¹¤ë)
310 .TP
311 .\"O .BR RTLD_DEEPBIND " (since glibc 2.3.4)"
312 .BR RTLD_DEEPBIND " (glibc 2.3.4 °Ê¹ß)"
313 .\" Inimitably described by UD in
314 .\" http://sources.redhat.com/ml/libc-hacker/2004-09/msg00083.html.
315 .\"O Place the lookup scope of the symbols in this
316 .\"O library ahead of the global scope.
317 .\"O This means that a self-contained library will use
318 .\"O its own symbols in preference to global symbols with the same name
319 .\"O contained in libraries that have already been loaded.
320 .\"O This flag is not specified in POSIX.1-2001.
321 ¤³¤Î¥é¥¤¥Ö¥é¥êÆâ¤Î¥·¥ó¥Ü¥ë¤Î»²¾ÈÎΰè¤ò¥°¥í¡¼¥Ð¥ëÎΰè¤è¤ê¤âÁ°¤ËÇÛÃÖ¤¹¤ë¡£
322 ¤Ä¤Þ¤ê¡¢Æ⢷¿¤Î¥é¥¤¥Ö¥é¥ê¤Ç¤Ï¡¢¤¹¤Ç¤Ë¥í¡¼¥É¤µ¤ì¤¿¥é¥¤¥Ö¥é¥ê¤Ë´Þ¤Þ¤ì¤ë
323 Ʊ¤¸Ì¾Á°¤Î¥°¥í¡¼¥Ð¥ë¤Ê¥·¥ó¥Ü¥ë¤è¤ê¤â¼«¥é¥¤¥Ö¥é¥êÆâ¤Î¥·¥ó¥Ü¥ë¤¬Í¥À褷¤Æ
324 »È¤ï¤ì¤ë¡£
325 ¤³¤Î¥Õ¥é¥°¤Ï POSIX.1-2001 ¤Ç¤Ïµ¬Äꤵ¤ì¤Æ¤¤¤Ê¤¤¡£
326 .PP
327 .\"O If
328 .\"O .I filename
329 .\"O is a NULL pointer, then the returned handle is for the main program.
330 .I filename
331 ¤¬ NULL ¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¾ì¹ç¤Ï¡¢
332 ÊÖ¤µ¤ì¤ë¥Ï¥ó¥É¥ë¤Ï¥á¥¤¥ó¡¦¥×¥í¥°¥é¥à¤Î¤â¤Î¤Ë¤Ê¤ë¡£
333 .\"O When given to
334 .\"O .BR dlsym (),
335 .\"O this handle causes a search for a symbol in the main program,
336 .\"O followed by all shared libraries loaded at program startup,
337 .\"O and then all shared libraries loaded by
338 .\"O .BR dlopen ()
339 .\"O with the flag
340 .\"O .BR RTLD_GLOBAL .
341 ¤³¤Î¥Ï¥ó¥É¥ë¤¬
342 .BR dlsym ()
343 ¤ËÅϤµ¤ì¤ë¤È¡¢¥·¥ó¥Ü¥ë¤Î¸¡º÷¤Ï¡¢¥á¥¤¥ó¡¦¥×¥í¥°¥é¥àÆâ¡¢
344 ¥×¥í¥°¥é¥à¤Îµ¯Æ°»þ¤Ë¥í¡¼¥É¤µ¤ì¤ëÁ´¤Æ¤Î¶¦Í­¥é¥¤¥Ö¥é¥ê¡¢
345 .BR dlopen ()
346 ¤Ë¤è¤Ã¤Æ
347 .B RTLD_GLOBAL
348 ¥Õ¥é¥°ÉÕ¤­¤Ç¥í¡¼¥É¤µ¤ì¤¿Á´¤Æ¤Î¶¦Í­¥é¥¤¥Ö¥é¥ê¡¢¤Î½ç½ø¤Ç¹Ô¤ï¤ì¤ë¡£
349 .PP
350 .\"O External references in the library are resolved using the libraries
351 .\"O in that library's dependency list and any other libraries previously
352 .\"O opened with the
353 .\"O .B RTLD_GLOBAL
354 .\"O flag.
355 .\"O If the executable was linked with the flag "\-rdynamic"
356 .\"O (or, synonymously, "\-\-export\-dynamic"),
357 .\"O then the global symbols in the executable will also be used
358 .\"O to resolve references in a dynamically loaded library.
359 ¥ª¡¼¥×¥ó¤µ¤ì¤¿¥é¥¤¥Ö¥é¥êÃæ¤Ç¤Î³°Éô»²¾È¤Ï¡¢
360 ¤½¤Î¥é¥¤¥Ö¥é¥ê¤Î°Í¸¥ê¥¹¥È¤Ë¤¢¤ë¥é¥¤¥Ö¥é¥ê¤«¡¢
361 .B RTLD_GLOBAL
362 ¥Õ¥é¥°ÉÕ¤­¤Ç´û¤Ë¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤ë¥é¥¤¥Ö¥é¥ê¤ò»È¤Ã¤Æ²ò·è¤µ¤ì¤ë¡£
363 ¼Â¹Ô¥Õ¥¡¥¤¥ë¤¬ "\-rdynamic" ¥Õ¥é¥° ("\-\-export\-dynamic" ¤âƱµÁ)
364 ÉÕ¤­¤Ç¥ê¥ó¥¯¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï¡¢¼Â¹Ô¥Õ¥¡¥¤¥ëÃæ¤Î¥°¥í¡¼¥Ð¥ë¥·¥ó¥Ü¥ë¤â¡¢
365 ưŪ¤Ë¥í¡¼¥É¤µ¤ì¤ë¥é¥¤¥Ö¥é¥êÆâ¤Î»²¾È²ò·è¤ËÍѤ¤¤é¤ì¤ë¡£
366 .PP
367 .\"O If the same library is loaded again with
368 .\"O .BR dlopen (),
369 .\"O the same file handle is returned.
370 .\"O The dl library maintains reference
371 .\"O counts for library handles, so a dynamic library is not
372 .\"O deallocated until
373 .\"O .BR dlclose ()
374 .\"O has been called on it as many times as
375 .\"O .BR dlopen ()
376 .\"O has succeeded on it.
377 .\"O The
378 .\"O .BR _init ()
379 .\"O routine, if present, is only called once.
380 .\"O But a subsequent call with
381 .\"O .B RTLD_NOW
382 .\"O may force symbol resolution for a library earlier loaded with
383 .\"O .BR RTLD_LAZY .
384 Ʊ¤¸¥é¥¤¥Ö¥é¥ê¤¬
385 .BR dlopen ()
386 ¤Ë¤è¤Ã¤ÆºÆÅÙ¥í¡¼¥É¤µ¤ì¤¿¾ì¹ç¤Ë¤Ï¡¢Æ±¤¸¥Õ¥¡¥¤¥ë¥Ï¥ó¥É¥ë¤¬ÊÖ¤µ¤ì¤ë¡£
387 dl ¥é¥¤¥Ö¥é¥ê¤Ï¥é¥¤¥Ö¥é¥ê¥Ï¥ó¥É¥ë¤Î¥ê¥ó¥¯¿ô¤ò´ÉÍý¤·¤Æ¤¤¤ë¡£
388 ¤·¤¿¤¬¤Ã¤ÆưŪ¥é¥¤¥Ö¥é¥ê¤Ï
389 .BR dlclose ()
390 ¤¬
391 .BR dlopen ()
392 ¤ÈƱ¤¸²ó¿ô¤À¤±¸Æ¤Ó½Ð¤µ¤ì¤Ê¤¤¸Â¤ê¥¢¥ó¥í¡¼¥É¤µ¤ì¤Ê¤¤¡£
393 .BR _init ()
394 ¥ë¡¼¥Á¥ó¤Ï°ìÅÙ¤À¤±¸Æ¤Ó½Ð¤µ¤ì¤ë
395 .RB ( _init ()
396 ¤¬Â¸ºß¤¹¤ë¾ì¹ç¤Î¤ß)¡£
397 .B RTLD_NOW
398 ¤¬»ØÄꤵ¤ì¤Æ
399 .BR dlopen ()
400 ¤¬¸Æ¤Ó½Ð¤µ¤ì¤¿¾ì¹ç¡¢
401 .B RTLD_LAZY
402 ¤Ç°ÊÁ°¤Ë¥í¡¼¥É¤µ¤ì¤¿¥é¥¤¥Ö¥é¥ê¤Î¥·¥ó¥Ü¥ë²ò·è¤¬¼Â¹Ô¤µ¤ì¤ë¤³¤È¤¬¤¢¤ë¡£
403 .PP
404 .\"O If
405 .\"O .BR dlopen ()
406 .\"O fails for any reason, it returns NULL.
407 .BR dlopen ()
408 ¤Ï¡¢²¿¤é¤«¤ÎÍýͳ¤Ç¼ºÇÔ¤¹¤ë¤È NULL ¤òÊÖ¤¹¡£
409
410 .SS "dlsym()"
411 .\"O The function
412 .\"O .BR dlsym ()
413 .\"O takes a "handle" of a dynamic library returned by
414 .\"O .BR dlopen ()
415 .\"O and the
416 .\"O null-terminated symbol name, returning the address where that symbol is
417 .\"O loaded into memory.
418 .\"O If the symbol is not found, in the specified
419 .\"O library or any of the libraries that were automatically loaded by
420 .\"O .BR dlopen ()
421 .\"O when that library was loaded,
422 .\"O .BR dlsym ()
423 .\"O returns NULL.
424 ´Ø¿ô
425 .BR dlsym ()
426 ¤Ï¡¢
427 .BR dlopen ()
428 ¤¬ÊÖ¤·¤¿Æ°Åª¥é¥¤¥Ö¥é¥ê¤Î¡Ö¥Ï¥ó¥É¥ë¡×¤È¡¢
429 NULL ½ªÃ¼¤µ¤ì¤¿¥·¥ó¥Ü¥ë̾¤Îʸ»úÎó¤ò°ú¤­¿ô¤Ë¼è¤ê¡¢
430 ¤½¤Î¥·¥ó¥Ü¥ë¤¬¥í¡¼¥É¤µ¤ì¤¿¥á¥â¥ê¤Î¥¢¥É¥ì¥¹¤òÊÖ¤¹¡£
431 ¥·¥ó¥Ü¥ë¤¬¡¢»ØÄꤵ¤ì¤¿¥é¥¤¥Ö¥é¥ê¤È¡¢»ØÄꤵ¤ì¤¿¥é¥¤¥Ö¥é¥ê¤¬¥í¡¼¥É¤µ¤ì¤ëºÝ¤Ë
432 .BR dlopen ()
433 ¤¬¼«Æ°Åª¤Ë¥í¡¼¥É¤·¤Æ¥é¥¤¥Ö¥é¥ê¤Î¤¤¤º¤ì¤Ë¤â¸«¤Ä¤«¤é¤Ê¤¤¾ì¹ç¤Ë¤Ï¡¢
434 .BR dlsym ()
435 ¤Ï NULL ¤òÊÖ¤¹
436 .\"O (The search performed by
437 .\"O .BR dlsym ()
438 .\"O is breadth first through the dependency tree of these libraries.)
439 .RB ( dlsym ()
440 ¤Ë¤è¤ë¸¡º÷¤Ï¡¢¤³¤ì¤é¤Î¥é¥¤¥Ö¥é¥ê¤Î°Í¸´Ø·¸¤Î¥Ä¥ê¡¼¤òÀèƬ¤«¤é
441 é¤Ã¤Æ¹Ô¤ï¤ì¤ë)¡£
442 .\"O Since the value of the symbol could actually be NULL (so that a
443 .\"O NULL return from
444 .\"O .BR dlsym ()
445 .\"O need not indicate an error), the correct way to test for an error
446 .\"O is to call
447 .\"O .BR dlerror ()
448 .\"O to clear any old error conditions, then call
449 .\"O .BR dlsym (),
450 .\"O and then call
451 .\"O .BR dlerror ()
452 .\"O again, saving its return value into a variable, and check whether
453 .\"O this saved value is not NULL.
454 ¼ÂºÝ¤Ë¤Ï¥·¥ó¥Ü¥ë¤ÎÃͼ«ÂΤ¬ NULL ¤Ë¤Ê¤ë¤³¤È¤â¤¢¤ë (¤½¤Î¤¿¤á¡¢
455 .BR dlsym ()
456 ¤ÎÊÖ¤êÃͤ¬ NULL ¤Ç¤¢¤Ã¤¿¤È¤·¤Æ¤âɬ¤º¤·¤â¥¨¥é¡¼¤È¤¤¤¦Ìõ¤Ç¤Ï¤Ê¤¤)¡£
457 ¥¨¥é¡¼¤«¤É¤¦¤«¤ò³Îǧ¤¹¤ëÀµ¤·¤¤ÊýË¡¤Ï°Ê²¼¤ÎÄ̤ê¤Ç¤¢¤ë:
458 .BR dlerror ()
459 ¤ò¸Æ¤Ó½Ð¤·¤Æ°ÊÁ°¤Î¥¨¥é¡¼¾õÂÖ¤ò¥¯¥ê¥¢¤·¤Æ¤«¤é¡¢
460 .BR dlsym ()
461 ¤ò¸Æ¤Ó½Ð¤¹¡£¤½¤Î¸å¤Ç¤â¤¦°ìÅÙ
462 .BR dlerror ()
463 ¤ò¸Æ¤Ó½Ð¤·¤Æ¡¢
464 .BR dlerror ()
465 ¤ÎÊÖ¤êÃͤòÊÑ¿ô¤ËÊݸ¤·¡¢Êݸ¤·¤¿Ãͤ¬ NULL ¤Ç¤¢¤ë¤«È½Äꤹ¤ë¡£
466 .PP
467 .\"O There are two special pseudo-handles,
468 .\"O .B RTLD_DEFAULT
469 .\"O and
470 .\"O .BR RTLD_NEXT .
471 .B RTLD_DEFAULT
472 ¤È
473 .B RTLD_NEXT
474 ¤È¤¤¤¦Æó¤Ä¤ÎÆÃÊ̤ʵ¼»÷¥Ï¥ó¥É¥ë¤¬¤¢¤ë¡£
475 .\"O The former will find the first occurrence of the desired symbol
476 .\"O using the default library search order.
477 .\"O The latter
478 .\"O will find the next occurrence of a function in the search order
479 .\"O after the current library.
480 .\"O This allows one to provide a wrapper
481 .\"O around a function in another shared library.
482 .B RTLD_DEFAULT
483 ¤Ï¡¢¥Ç¥Õ¥©¥ë¥È¤Î¥é¥¤¥Ö¥é¥ê¸¡º÷½ç½ø¤Ë¤·¤¿¤¬¤Ã¤Æ¡¢
484 ¸¡º÷ÂоݤΥ·¥ó¥Ü¥ë¤¬ºÇ½é¤Ë¸½¤ì¤ë¤È¤³¤í¤òõ¤¹¡£
485 .B RTLD_NEXT
486 ¤Ï¡¢¥é¥¤¥Ö¥é¥ê¸¡º÷½ç½ø¤ÎÃæ¤Ç¸½ºß¤Î¥é¥¤¥Ö¥é¥ê°Ê¹ß¤ÇºÇ½é¤Ë
487 ´Ø¿ô¤¬¸½¤ì¤ë¤È¤³¤í¤òõ¤¹¡£¤³¤Îµ¡Ç½¤ò»È¤¦¤³¤È¤Ç¡¢Ê̤ζ¦Í­¥é¥¤¥Ö¥é¥ê¤Î
488 ´Ø¿ô¤Ø¤Î¥é¥Ã¥Ñ¡¼¤òÄ󶡤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
489 .SS "dlclose()"
490 .\"O The function
491 .\"O .BR dlclose ()
492 .\"O decrements the reference count on the dynamic library handle
493 .\"O .IR handle .
494 .\"O If the reference count drops to zero and no other loaded libraries use
495 .\"O symbols in it, then the dynamic library is unloaded.
496 ´Ø¿ô
497 .BR dlclose ()
498 ¤ÏưŪ¥é¥¤¥Ö¥é¥ê¤Î¥Ï¥ó¥É¥ë
499 .I handle
500 ¤Î»²¾È¥«¥¦¥ó¥È¤ò 1 ¸º¤é¤¹¡£»²¾È¥«¥¦¥ó¥È¤¬ 0 ¤Ë¤Ê¤ê¡¢¥í¡¼¥É¤µ¤ì¤Æ¤¤¤ë
501 ¾¤Î¥é¥¤¥Ö¥é¥ê¤«¤é¤½¤Î¥é¥¤¥Ö¥é¥êÆâ¤Î¥·¥ó¥Ü¥ë¤¬»È¤ï¤ì¤Æ¤¤¤Ê¤±¤ì¤Ð¡¢
502 ¤½¤ÎưŪ¥é¥¤¥Ö¥é¥ê¤ò¥¢¥ó¥í¡¼¥É¤¹¤ë¡£
503 .LP
504 .\"O The function
505 .\"O .BR dlclose ()
506 .\"O returns 0 on success, and nonzero on error.
507 ´Ø¿ô
508 .BR dlclose ()
509 ¤Ï¡¢À®¸ù¤·¤¿¾ì¹ç¤Ï 0 ¤òÊÖ¤·¡¢¥¨¥é¡¼¤Î¾ì¹ç 0 °Ê³°¤òÊÖ¤¹¡£
510 .\"O .SS "The obsolete symbols _init(0 and _fini()"
511 .SS "Çѻߤµ¤ì¤¿¥·¥ó¥Ü¥ë _init() ¤È _fini()"
512 .\"O The linker recognizes special symbols
513 .\"O .B _init
514 .\"O and
515 .\"O .BR _fini .
516 .\"O If a dynamic library exports a routine named
517 .\"O .BR _init (),
518 .\"O then that code is executed after the loading, before
519 .\"O .BR dlopen ()
520 .\"O returns.
521 .\"O If the dynamic library exports a routine named
522 .\"O .BR _fini (),
523 .\"O then that routine is called just before the library is unloaded.
524 ¥ê¥ó¥«¤Ï
525 .B _init
526 ¤È
527 .B _fini
528 ¤òÆÃÊ̤ʥ·¥ó¥Ü¥ë¤È²ò¼á¤¹¤ë¡£
529 ¤¢¤ëưŪ¥é¥¤¥Ö¥é¥ê¤Ç
530 .BR _init ()
531 ¤È¤¤¤¦Ì¾Á°¤Î¥ë¡¼¥Á¥ó¤¬¥¨¥¯¥¹¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ì¤Ð¡¢
532 ¤½¤Î¥³¡¼¥É¤Ï¡¢¥é¥¤¥Ö¥é¥ê¤Î¥í¡¼¥É¸å¡¢¤«¤Ä
533 .BR dlopen ()
534 ¤¬Éüµ¢¤¹¤ëÁ°¤Ë¼Â¹Ô¤µ¤ì¤ë¡£
535 ¤½¤ÎưŪ¥é¥¤¥Ö¥é¥ê¤Ç
536 .BR _fini ()
537 ¤È¤¤¤¦Ì¾Á°¤Î¥ë¡¼¥Á¥ó¤¬¥¨¥¯¥¹¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ì¤Ð¡¢
538 ¥é¥¤¥Ö¥é¥ê¤¬¥¢¥ó¥í¡¼¥É¤µ¤ì¤ëľÁ°¤Ë¤½¤Î¥ë¡¼¥Á¥ó¤¬¸Æ¤Ó½Ð¤µ¤ì¤ë¡£
539 .\"O In case you need to avoid linking against the system startup files,
540 .\"O this can be done by using the
541 .\"O .BR gcc (1)
542 .\"O .I \-nostartfiles
543 .\"O command-line option.
544 ¥·¥¹¥Æ¥à¤Îµ¯Æ°¥Õ¥¡¥¤¥ë¤ËÂФ¹¤ë¥ê¥ó¥¯¤òÈò¤±¤ëɬÍפ¬¤¢¤ë¾ì¹ç¡¢
545 .BR gcc (1)
546 ¤Î¥³¥Þ¥ó¥É¥é¥¤¥ó¤Ë
547 .I \-nostartfiles
548 ¥ª¥×¥·¥ç¥ó¤ò»ØÄꤹ¤ì¤Ð¤è¤¤¡£
549 .LP
550 .\"O Using these routines, or the gcc
551 .\"O .B \-nostartfiles
552 .\"O or
553 .\"O .B \-nostdlib
554 .\"O options, is not recommended.
555 .\"O Their use may result in undesired behavior,
556 .\"O since the constructor/destructor routines will not be executed
557 .\"O (unless special measures are taken).
558 .\"O .\" void _init(void) __attribute__((constructor));
559 .\"O .\" void _fini(void) __attribute__((destructor));
560 ¤³¤Î¥ë¡¼¥Á¥ó¤ä¡¢gcc ¤Î¥ª¥×¥·¥ç¥ó
561 .B \-nostartfiles
562 ¤ä
563 .B \-nostdlib
564 ¤Ï»ÈÍѤ·¤Ê¤¤¤³¤È¤ò¿ä¾©¤¹¤ë¡£
565 ¤³¤ì¤é¤ò»È¤¦¤È¡¢Ë¾¤Þ¤·¤¯¤Ê¤¤Æ°ºî¤ò¤¹¤ë¤³¤È¤¬¤¢¤ë¡£
566 ¤Ê¤¼¤Ê¤é¡¢(ÆÃÊ̤ÊÁ¼ÃÖ¤¬¹Ô¤ï¤ì¤Ê¤¤¸Â¤ê) ¤³¤ì¤é¤Î constructor/destructor
567 ¥ë¡¼¥Á¥ó¤Ï¼Â¹Ô¤µ¤ì¤Ê¤¤¤«¤é¤Ç¤¢¤ë¡£
568 .\" void _init(void) __attribute__((constructor));
569 .\" void _fini(void) __attribute__((destructor));
570 .LP
571 .\"O Instead, libraries should export routines using the
572 .\"O .B __attribute__((constructor))
573 .\"O and
574 .\"O .B __attribute__((destructor))
575 .\"O function attributes.
576 .\"O See the gcc info pages for information on these.
577 .\"O Constructor routines are executed before
578 .\"O .BR dlopen ()
579 .\"O returns, and destructor routines are executed before
580 .\"O .BR dlclose ()
581 .\"O returns.
582 Âå¤ï¤ê¤Ë¡¢¥é¥¤¥Ö¥é¥ê¤Ï
583 .B __attribute__((constructor))
584 ¤ä
585 .B __attribute__((destructor))
586 ¤Î´Ø¿ô°À­¤ò»È¤Ã¤ÆɬÍפʥ롼¥Á¥ó¤ò¥¨¥¯¥¹¥Ý¡¼¥È¤¹¤ë¤Î¤¬¤è¤¤¡£
587 ¤³¤ì¤é¤Ë¤Ä¤¤¤Æ¤Ï gcc ¤Î info ¥Ú¡¼¥¸¤ò»²¾È¤Î¤³¤È¡£
588 constructor ¥ë¡¼¥Á¥ó¤Ï
589 .BR dlopen ()
590 ¤¬Éüµ¢¤¹¤ëÁ°¤Ë¼Â¹Ô¤µ¤ì¡¢
591 destructor ¥ë¡¼¥Á¥ó¤Ï
592 .BR dlclose ()
593 ¤¬Éüµ¢¤¹¤ëÁ°¤Ë¼Â¹Ô¤µ¤ì¤ë¡£
594 .\"O .SS Glibc extensions: dladdr() and dlvsym()
595 .SS GNU ¤Ç¤Î³ÈÄ¥: dladdr() ¤È dlvsym()
596 .\"O Glibc adds two functions not described by POSIX, with prototypes
597 glibc ¤Ç¤Ï POSIX ¤Ë¤Ïµ­ºÜ¤µ¤ì¤Æ¤¤¤Ê¤¤´Ø¿ô¤¬ 2¤ÄÄɲ䵤ì¤Æ¤¤¤ë¡£
598 ¥×¥í¥È¥¿¥¤¥×¤Ï°Ê²¼¤ÎÄ̤ê¤Ç¤¢¤ë¡£
599 .sp
600 .nf
601 .\"O .BR "#define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
602 .BR "#define _GNU_SOURCE" "         /* feature_test_macros(7) »²¾È */"
603 .B #include <dlfcn.h>
604 .sp
605 .BI "int dladdr(void *" addr ", Dl_info *" info );
606 .sp
607 .BI "void *dlvsym(void *" handle ", char *" symbol ", char *" version );
608 .fi
609 .PP
610 .\"O The function
611 .\"O .BR dladdr ()
612 .\"O takes a function pointer and tries to resolve name
613 .\"O and file where it is located.
614 .\"O Information is stored in the
615 .\"O .I Dl_info
616 .\"O structure:
617 ´Ø¿ô
618 .BR dladdr ()
619 ¤Ï¡¢´Ø¿ô¤Î¥Ý¥¤¥ó¥¿¤ò°ú¤­¿ô¤Ë¤È¤ê¡¢´Ø¿ô¤Î̾Á°¤È´Ø¿ô¤¬ÄêµÁ¤µ¤ì¤Æ¤¤¤ë
620 ¥Õ¥¡¥¤¥ë¤Î²ò·è¤ò»î¤ß¤ë¡£¾ðÊó¤Ï
621 .I Dl_info
622 ¹½Â¤ÂΤ˳ÊǼ¤µ¤ì¤ë¡£
623 .sp
624 .in +4n
625 .nf
626 typedef struct {
627     const char *dli_fname;  /* Pathname of shared object that
628                                contains address */
629     void       *dli_fbase;  /* Address at which shared object
630                                is loaded */
631     const char *dli_sname;  /* Name of nearest symbol with address
632                                lower than \fIaddr\fP */
633     void       *dli_saddr;  /* Exact address of symbol named
634                                in \fIdli_sname\fP */
635 } Dl_info;
636 .fi
637 .in
638 .PP
639 .\"O If no symbol matching
640 .\"O .I addr
641 .\"O could be found, then
642 .\"O .I dli_sname
643 .\"O and
644 .\"O .I dli_saddr
645 .\"O are set to NULL.
646 .I addr
647 ¤Ë¥Þ¥Ã¥Á¤¹¤ë¥·¥ó¥Ü¥ë¤¬¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç¡¢
648 .I dli_sname
649 ¤È
650 .I dli_saddr
651 ¤Ï NULL ¤Ë¥»¥Ã¥È¤µ¤ì¤ë¡£
652 .PP
653 .\"O .BR dladdr ()
654 .\"O returns 0 on error, and nonzero on success.
655 .BR dladdr ()
656 ¤Ï¡¢¥¨¥é¡¼»þ¤Ë¤Ï 0 ¤òÊÖ¤·¡¢À®¸ù¤·¤¿¾ì¹ç¤Ï 0 °Ê³°¤òÊÖ¤¹¡£
657 .PP
658 .\"O The function
659 .\"O .BR dlvsym (),
660 .\"O provided by glibc since version 2.1,
661 .\"O does the same as
662 .\"O .BR dlsym ()
663 .\"O but takes a version string as an additional argument.
664 ´Ø¿ô
665 .BR dlvsym ()
666 ¤Ï
667 .BR dlsym ()
668 ¤ÈƱ¤¸Æ°ºî¤ò¤¹¤ë¤¬¡¢¥Ð¡¼¥¸¥ç¥ó¤Îʸ»úÎó¤òÅϤ¹°ú¤­¿ô¤¬
669 Äɲ䵤ì¤Æ¤¤¤ëÅÀ¤¬°Û¤Ê¤ë
670 .RB ( dlvsym ()
671 ¤Ï¥Ð¡¼¥¸¥ç¥ó 2.1 °Ê¹ß¤Î glibc ¤ÇÄ󶡤µ¤ì¤Æ¤¤¤ë)¡£
672 .\"O .SH "CONFORMING TO"
673 .SH ½àµò
674 .\"O POSIX.1-2003 describes
675 .\"O .BR dlclose (),
676 .\"O .BR dlerror (),
677 .\"O .BR dlopen (),
678 .\"O and
679 .\"O .BR dlsym ().
680 POSIX.1-2003 ¤Ë¤Ï
681 .BR dlclose (),
682 .BR dlerror (),
683 .BR dlopen (),
684 .BR dlsym ().
685 ¤Îµ­ºÜ¤¬¤¢¤ë¡£
686 .\"O .SH NOTES
687 .SH Ãí°Õ
688 .\"O The symbols
689 .\"O .B RTLD_DEFAULT
690 .\"O and
691 .\"O .B RTLD_NEXT
692 .\"O are defined by
693 .\"O .I <dlfcn.h>
694 .\"O only when
695 .\"O .B _GNU_SOURCE
696 .\"O was defined before including it.
697 ¥·¥ó¥Ü¥ë
698 .B RTLD_DEFAULT
699 ¤È
700 .B RTLD_NEXT
701 ¤Ï
702 .I <dlfcn.h>
703 ¤ÇÄêµÁ¤µ¤ì¤Æ¤ª¤ê¡¢
704 .I <dlfcn.h>
705 ¤Î¥¤¥ó¥¯¥ë¡¼¥ÉÁ°¤Ë
706 .B _GNU_SOURCE
707 ¤¬ÄêµÁ¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤Î¤ßÍ­¸ú¤È¤Ê¤ë¡£
708 .\" .LP
709 .\"O .\" The string returned by
710 .\"O .\" .BR dlerror ()
711 .\"O .\" should not be modified.
712 .\"O .\" Some systems give the prototype as
713 .\"O .\" .sp
714 .\"O .\" .in +5
715 .\"O .\" .B "const char *dlerror(void);"
716 .\"O .\" .in
717 .\" .BR dlerror ()
718 .\" ¤¬ÊÖ¤¹Ê¸»úÎó¤ÏÊѹ¹¤¹¤Ù¤­¤Ç¤Ï¤Ê¤¤¡£¥·¥¹¥Æ¥à¤Ë¤è¤Ã¤Æ¤Ï¡¢
719 .\" °Ê²¼¤Î¤è¤¦¤Ê¥×¥í¥È¥¿¥¤¥×¤Ë¤Ê¤Ã¤Æ¤¤¤ë¡£
720 .\" .sp
721 .\" .in +5
722 .\" .B "const char *dlerror(void);"
723 .\" .in
724
725 .\"O Since glibc 2.2.3,
726 .\"O .BR atexit (3)
727 .\"O can be used to register an exit handler that is automatically
728 .\"O called when a library is unloaded.
729 glibc 2.2.3 °Ê¹ß¤Ç¤Ï¡¢
730 .BR atexit (3)
731 ¤ò»È¤Ã¤Æ¡¢¥é¥¤¥Ö¥é¥ê¤¬¥¢¥ó¥í¡¼¥É¤µ¤ì¤ëºÝ¤Ë¼«Æ°Åª¤Ë¸Æ¤Ó½Ð¤µ¤ì¤ë
732 ½ªÎ»¥Ï¥ó¥É¥é (exit handler) ¤òÅÐÏ¿¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
733 .\"O .SS History
734 .SS Îò»Ë
735 .\"O The dlopen interface standard comes from SunOS.
736 .\"O That system also has
737 .\"O .BR dladdr (),
738 .\"O but not
739 .\"O .BR dlvsym ().
740 dlopen ¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤Îɸ½à¤Ï SunOS ¤ò¤â¤È¤Ë¤·¤Æ¤¤¤ë¡£
741 SunOS ¤Ë¤Ï
742 .BR dladdr ()
743 ¤â¤¢¤Ã¤¿¤¬¡¢
744 .BR dlvsym ()
745 ¤Ï¤Ê¤«¤Ã¤¿¡£
746 .\"O .SH BUGS
747 .SH ¥Ð¥°
748 .\"O Sometimes, the function pointers you pass to
749 .\"O .BR dladdr ()
750 .\"O may surprise you.
751 .\"O On some architectures (notably i386 and x86_64),
752 .\"O .I dli_fname
753 .\"O and
754 .\"O .I dli_fbase
755 .\"O may end up pointing back at the object from which you called
756 .\"O .BR dladdr (),
757 .\"O even if the function used as an argument should come from
758 .\"O a dynamically linked library.
759 »þ¤È¤·¤Æ¡¢
760 .BR dladdr ()
761 ¤ËÅϤ·¤¿´Ø¿ô¥Ý¥¤¥ó¥¿¤Ï¶Ã¤¯¤è¤¦¤ÊÃͤˤʤ뤳¤È¤¬¤¢¤ë¡£
762 ¤¤¤¯¤Ä¤«¤Î¥¢¡¼¥­¥Æ¥¯¥Á¥ã (Æäˠi386 ¤È x86_64) ¤Ç¤Ï¡¢
763 °ú¤­¿ô¤È¤·¤Æ»ÈÍѤ·¤¿´Ø¿ô¤¬Æ°Åª¥ê¥ó¥¯¥é¥¤¥Ö¥é¥ê¤ÇÄêµÁ¤µ¤ì¤ë¤â¤Î¤Ç
764 ¤¢¤Ã¤¿¤È¤·¤Æ¤â¡¢
765 .I dli_fname
766 ¤È
767 .I dli_fbase
768 ¤¬
769 .BR dladdr ()
770 ¤ò¸Æ¤Ó½Ð¤·¤¿¥ª¥Ö¥¸¥§¥¯¥È¤ò»²¾È¤·¤¿¾õÂ֤ǽª¤ï¤Ã¤Æ¤¤¤ë¤³¤È¤¬¤¢¤ë¡£
771 .PP
772 .\"O The problem is that the function pointer will still be resolved
773 .\"O at compile time, but merely point to the
774 .\"O .I plt
775 .\"O (Procedure Linkage Table)
776 .\"O section of the original object (which dispatches the call after
777 .\"O asking the dynamic linker to resolve the symbol).
778 .\"O To work around this,
779 .\"O you can try to compile the code to be position-independent:
780 .\"O then, the compiler cannot prepare the pointer
781 .\"O at compile time anymore and today's
782 .\"O .BR gcc (1)
783 .\"O will generate code that just loads the final symbol address from the
784 .\"O .I got
785 .\"O (Global Offset Table) at run time before passing it to
786 .\"O .BR dladdr ().
787 .\"O motoki: ³ç¸ÌÆâ (which dispatches ¡Á) ¤ÎÉôʬ¤ÎÌõ¤Ë¼«¿®¤Ê¤·¡£
788 .\"O         which ¤Î¼ç¸ì¤Ï original object ¤Ç¤¤¤¤¤Î¤«?
789 ÌäÂê¤Ï¡¢´Ø¿ô¥Ý¥¤¥ó¥¿¤Î²ò·è¤Ïº£¤Ê¤ª¥³¥ó¥Ñ¥¤¥ë»þ¤Ë¹Ô¤ï¤ì¤ë¤¬¡¢
790 ¤½¤Î¥Ý¥¤¥ó¥¿¤Ï¸µ¤Î¥ª¥Ö¥¸¥§¥¯¥È¤Î
791 .I plt
792 (Procedure Linkage Table) ¥»¥¯¥·¥ç¥ó¤ò»Ø¤·¤Æ¤¤¤ë¤À¤±¤À¤È¤¤¤¦ÅÀ¤Ë¤¢¤ë
793 (¥ª¥Ö¥¸¥§¥¯¥È¼«ÂΤϡ¢¥À¥¤¥Ê¥ß¥Ã¥¯¥ê¥ó¥«¤Ë¤è¤Ã¤Æ¥·¥ó¥Ü¥ë¤Î²ò·è¤¬¹Ô¤ï¤ì¤¿¸å¤Ë¡¢
794 ´Ø¿ô¤Î¸Æ¤Ó½Ð¤·¤ò¹Ô¤¦)¡£
795 ¤³¤ì¤ËÂн褹¤ëÊýË¡¤È¤·¤Æ¤Ï¡¢
796 ¥³¡¼¥É¤ò position-independent ¤Ç¥³¥ó¥Ñ¥¤¥ë¤¹¤ë¤È¤¤¤¦ÊýË¡¤¬¤¢¤ë¡£
797 ¤½¤¦¤¹¤ë¤È¡¢¥³¥ó¥Ñ¥¤¥é¤Ï¥³¥ó¥Ñ¥¤¥ë»þ¤Ë¥Ý¥¤¥ó¥¿¤òÍÑ°Õ¤¹¤ë¤³¤È¤¬¤Ç¤­¤º¡¢
798 º£Æü¤Î
799 .BR gcc (1)
800 ¤Ç¤Ï¡¢¼Â¹Ô»þ¤Ë
801 .BR dladdr ()
802 ¤Ë´Ø¿ô¥Ý¥¤¥ó¥¿¤òÅϤ¹Á°¤Ë¡¢
803 .I got
804 (Global Offset Table) ¤«¤éºÇ½ªÅª¤Ê¥·¥ó¥Ü¥ë¤Î¥¢¥É¥ì¥¹¤ò¥í¡¼¥É¤¹¤ë¤À¤±¤Î
805 ¥³¡¼¥É¤¬À¸À®¤µ¤ì¤ë¡£
806 .\"O .SH EXAMPLE
807 .SH Îã
808 .\"O Load the math library, and print the cosine of 2.0:
809 math ¥é¥¤¥Ö¥é¥ê¤ò¥í¡¼¥É¤·¡¢2.0 ¤Î;¸¹¤òɽ¼¨¤¹¤ë
810 .nf
811
812 #include <stdio.h>
813 #include <stdlib.h>
814 #include <dlfcn.h>
815
816 int
817 main(int argc, char **argv)
818 {
819     void *handle;
820     double (*cosine)(double);
821     char *error;
822
823     handle = dlopen("libm.so", RTLD_LAZY);
824     if (!handle) {
825         fprintf(stderr, "%s\en", dlerror());
826         exit(EXIT_FAILURE);
827     }
828
829     dlerror();    /* Clear any existing error */
830
831     /* Writing: cosine = (double (*)(double)) dlsym(handle, "cos");
832        would seem more natural, but the C99 standard leaves
833        casting from "void *" to a function pointer undefined.
834        The assignment used below is the POSIX.1\-2003 (Technical
835        Corrigendum 1) workaround; see the Rationale for the
836        POSIX specification of dlsym(). */
837
838     *(void **) (&cosine) = dlsym(handle, "cos");
839 .\" But in fact "gcc -O2 -Wall" will complain about the preceding cast.
840
841     if ((error = dlerror()) != NULL)  {
842         fprintf(stderr, "%s\en", error);
843         exit(EXIT_FAILURE);
844     }
845
846     printf("%f\en", (*cosine)(2.0));
847     dlclose(handle);
848     exit(EXIT_SUCCESS);
849 }
850 .fi
851 .PP
852 .\"O If this program were in a file named "foo.c", you would build the program
853 .\"O with the following command:
854 ¤³¤Î¥×¥í¥°¥é¥à¤ò "foo.c" ¤Ë½ñ¤¤¤¿¤È¤¹¤ë¤È¡¢°Ê²¼¤Î¥³¥Þ¥ó¥É¤Ç¥×¥í¥°¥é¥à¤ò
855 ¥Ó¥ë¥É¤Ç¤­¤ë¡£
856 .in +4n
857 .LP
858     gcc \-rdynamic \-o foo foo.c \-ldl
859 .in
860 .PP
861 .\"O Libraries exporting
862 .\"O .BR _init ()
863 .\"O and
864 .\"O .BR _fini ()
865 .\"O will want to be compiled as
866 .\"O follows, using \fIbar.c\fP as the example name:
867 .BR _init ()
868 ¤È
869 .BR _fini ()
870 ¤ò¥¨¥¯¥¹¥Ý¡¼¥È¤¹¤ë¥é¥¤¥Ö¥é¥ê¤Î¾ì¹ç¤Ï
871 °Ê²¼¤Î¤è¤¦¤Ë¤·¤Æ¥³¥ó¥Ñ¥¤¥ë¤¹¤ëɬÍפ¬¤¢¤ë¡£
872 Îã¤È¤·¤Æ \fIbar.c\fP ¤ò¥³¥ó¥Ñ¥¤¥ë¤¹¤ë¾ì¹ç:
873 .in +4n
874 .LP
875     gcc \-shared \-nostartfiles \-o bar bar.c
876 .in
877 .\"O .SH SEE ALSO
878 .SH ´ØÏ¢¹àÌÜ
879 .BR ld (1),
880 .BR ldd (1),
881 .BR dl_iterate_phdr (3),
882 .BR rtld-audit (7),
883 .BR ld.so (8),
884 .BR ldconfig (8),
885 ld.so info pages, gcc info pages, ld info pages