1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
|
.\" -*- nroff -*-
.\" Copyright 1995 Yggdrasil Computing, Incorporated.
.\" written by Adam J. Richter (adam@yggdrasil.com),
.\" with typesetting help from Daniel Quinlan (quinlan@yggdrasil.com).
.\" and Copyright 2003 Michael Kerrisk (mtk.manpages@gmail.com).
.\"
.\" This is free documentation; you can redistribute it and/or
.\" modify it under the terms of the GNU General Public License as
.\" published by the Free Software Foundation; either version 2 of
.\" the License, or (at your option) any later version.
.\"
.\" The GNU General Public License's references to "object code"
.\" and "executables" are to be interpreted as the output of any
.\" document formatting or typesetting system, including
.\" intermediate and printed output.
.\"
.\" This manual is distributed in the hope that it will be useful,
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
.\" GNU General Public License for more details.
.\"
.\" You should have received a copy of the GNU General Public
.\" License along with this manual; if not, write to the Free
.\" Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
.\" USA.
.\"
.\" Modified by David A. Wheeler <dwheeler@dwheeler.com> 2000-11-28.
.\" Applied patch by Terran Melconian, aeb, 2001-12-14.
.\" Modified by Hacksaw <hacksaw@hacksaw.org> 2003-03-13.
.\" Modified by Matt Domsch, 2003-04-09: _init and _fini obsolete
.\" Modified by Michael Kerrisk <mtk.manpages@gmail.com> 2003-05-16.
.\" Modified by Walter Harms: dladdr, dlvsym
.\"
.\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved.
.\" Translated Sat May 23 1998 by NAKANO Takeo <nakano@apm.seikei.ac.jp>
.\" Updated & Modified 1999-09-14, NAKANO Takeo
.\" Modified 2000-03-19, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
.\" Updated 2001-02-16, Kentaro Shirakata <argrath@ub32.org>
.\" Updated 2001-12-21, Kentaro Shirakata <argrath@ub32.org>
.\" Updated 2002-10-21, Kentaro Shirakata <argrath@ub32.org>
.\" Updated 2003-09-01, Kentaro Shirakata <argrath@ub32.org>
.\" Updated 2005-03-15, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
.\" Updated 2006-01-20, Akihiro MOTOKI
.\"
.\"WORD: linker
.\"WORD: dynamic linking ưŪ
.\"WORD: dynamic library ưŪ饤֥
.\"
.TH DLOPEN 3 2007-06-27 "Linux" "Linux Programmer's Manual"
.SH ̾
dlclose, dlerror, dlopen, dlsym \- ưŪԤؤ
ץߥե
.SH
.B #include <dlfcn.h>
.sp
.BI "void *dlopen(const char *" filename ", int " flag );
.sp
.B "char *dlerror(void);"
.sp
.BI "void *dlsym(void *" handle ", const char *" symbol );
.sp
.BI "int dlclose(void *" handle );
.sp
\fI\-ldl\fP ǥ롣
.SH
.BR dlopen (),
.BR dlsym (),
.BR dlclose (),
.BR dlerror ()
4ĤδؿϡưŪ (dynamic linking) Ԥؤ
եΤǤ롣
.SS "dlerror()"
ؿ
.BR dlerror ()
ϡ
.BR dlerror ()
ƤӽФ줿ˡ
.BR dlopen (),
.BR dlsym (),
.BR dlclose ()
Τ줫ǺǸȯ顼ˤĤƤå֤
ޤƤӽФ줿ǡ顼ȯƤʤ NULL ֤
.SS "dlopen()"
ؿ
.BR dlopen ()
ϡNULL ü줿ʸ
.I filename
ǻꤵ줿ե̾ưŪ饤֥ (dynamic library) ɤ
ưŪ饤֥ؤ֥ϥɥפ֤
.I filename
NULL ξ硢ᥤץؤΥϥɥ뤬֤롣
.I filename
å ("/") ޤ硢(ФФ)ѥ̾ȤƲᤵ롣
ʳξ硢ưŪϰʲμǥ饤֥
(ܺ٤
.BR ld.so (8)
):
.IP o 4
(ELF Τ) ƤӽФץμ¹ԥե DT_RPATH ޤޤƤꡢ
DT_RUNPATH ޤޤƤʤ硢DT_RPATH ˽Ƥ
ǥ쥯ȥꡦꥹȤ롣
.IP o
Ķѿ
.B LD_LIBRARY_PATH
ǻꤵ줿ڤΥǥ쥯ȥꡦꥹȤ롣
(ƥͳǡѿ set-UID set-GID 줿
ץξ̵뤵)
.IP o
(ELF Τ) ƤӽФץμ¹ԥե DT_RUNPATH ޤޤ
硢Υ˽Ƥǥ쥯ȥꡦꥹȤ롣
.IP o
åե
.I /etc/ld.so.cache
.I filename
Υȥ꤬äƤ뤫å
.RB ( /etc/ld.so.cache
.BR ldconfig (8)
ˤäƴƤ)
.IP o
ǥ쥯ȥ
.I /lib
.I /usr/lib
ν֤Ǹ롣
.PP
Υ饤֥꤬¾ζͭ饤֥˰¸Ƥϡ
¸Ƥ饤֥ưŪƱ롼˴Ť
ưŪ˥ɤ (Υ饤֥ˤ˰¸طʤɤ
νϺƵŪ˹Ԥ)
.PP
.I flag
ˤϰʲ 2 ĤͤΤ줫ޤʤФʤʤ:
.TP
.B RTLD_LAZY
lazy binding (ȴʥܥηդ) Ԥ
ܥβϤΥܥȤ륳ɤ¹ԤȤˤΤ
Ԥ롣ܥ뤬٤⻲ȤʤäˤϡΥܥ
褵ʤޤޤȤʤ롣
(lazy binding ϴؿȤˤĤƤΤ»ܤ; ѿؤλȤϾ
饤֥꤬ɤ줿ľ˲褵롣)
.TP
.B RTLD_NOW
ͤꤵ뤫Ķѿ
.B LD_BIND_NOW
˶Ǥʤʸꤵ줿硢
饤֥̤ΥܥƲ褷Ƥ
.BR dlopen ()
롣ǤʤäȤˤϥ顼֤롣
.PP
ʲͤΤ 0 İʾ (OR) η
.I flag
ɲä뤳ȤǤ:
.TP
.B RTLD_GLOBAL
Υ饤֥Ƥ륷ܥ뤬ǥɤ
饤֥ΥܥѤǤ褦ˤʤ롣
.TP
.B RTLD_LOCAL
Υե饰
.B RTLD_GLOBAL
ȿФΰ̣ǤꡢɤΥե饰ꤵʤä
餬ǥեȤȤʤ롣
Υ饤֥Ƥ륷ܥϡǥɤ
饤֥ǤΥܥ뻲ȤѤǤʤ
.TP
.BR RTLD_NODELETE " (glibc 2.2 ʹ)"
.BR dlclose ()
ˤΥ饤֥ɤʤ
ΤᡢƱ饤֥ʹߤ
.BR dlopen ()
Ǻ٥ɤˡ饤֥ŪѿϺƽʤ
Υե饰 POSIX.1-2001 ǤϵꤵƤʤ
.\" ( Solaris ¸ߤ)
.TP
.BR RTLD_NOLOAD " (glibc 2.2 ʹ)"
Υ饤֥ɤʤ
Υե饰ϤΥ饤֥꤬ǤȤ߹ޤƤ뤫Τ
ѤǤ
.RB ( dlopen ()
ϡ饤֥꤬Ȥ߹ޤƤʤ NULL ֤
ǤȤ߹ޤƤФΥ饤֥Υϥɥ֤)
ޤǤ˥ɤƤ饤֥Υե饰ʤΤˤ
ѤǤ롣㤨С
.B RTLD_LOCAL
ǥɤ饤֥
.BR RTLD_NOLOAD\ |\ RTLD_GLOBAL
Ǻƥץ뤳ȤǤ롣
Υե饰 POSIX.1-2001 ǤϵꤵƤʤ
.\" ( Solaris ¸ߤ)
.TP
.BR RTLD_DEEPBIND " (glibc 2.3.4 ʹ)"
.\" Inimitably described by UD in
.\" http://sources.redhat.com/ml/libc-hacker/2004-09/msg00083.html.
Υ饤֥ΥܥλΰХΰ֤롣
Ĥޤꡢ¢Υ饤֥ǤϡǤ˥ɤ줿饤֥˴ޤޤ
Ʊ̾ΥХʥܥ⼫饤֥Υܥ뤬ͥ褷
Ȥ롣
Υե饰 POSIX.1-2001 ǤϵꤵƤʤ
.PP
.I filename
NULL ݥǤϡ
֤ϥɥϥᥤץΤΤˤʤ롣
Υϥɥ뤬
.BR dlsym ()
Ϥȡܥθϡᥤץ⡢
ץεư˥ɤƤζͭ饤֥ꡢ
.BR dlopen ()
ˤä
.B RTLD_GLOBAL
ե饰դǥɤ줿Ƥζͭ饤֥ꡢνǹԤ롣
.PP
ץ줿饤֥ǤγȤϡ
Υ饤֥ΰ¸ꥹȤˤ饤֥꤫
.B RTLD_GLOBAL
ե饰դǴ˥ץƤ饤֥ȤäƲ褵롣
¹ԥե뤬 "\-rdynamic" ե饰 ("\-\-export\-dynamic" Ʊ)
դǥƤϡ¹ԥեΥХ륷ܥ⡢
ưŪ˥ɤ饤֥λȲѤ롣
.PP
Ʊ饤֥꤬
.BR dlopen ()
ˤäƺ٥ɤ줿ˤϡƱեϥɥ뤬֤롣
dl 饤֥ϥ饤֥ϥɥΥƤ롣
äưŪ饤֥
.BR dlclose ()
.BR dlopen ()
ƱƤӽФʤ¤ꥢɤʤ
.BR _init ()
롼ϰ٤ƤӽФ
.RB ( _init ()
¸ߤΤ)
.B RTLD_NOW
ꤵ
.BR dlopen ()
ƤӽФ줿硢
.B RTLD_LAZY
ǰ˥ɤ줿饤֥Υܥ褬¹Ԥ뤳Ȥ롣
.PP
.BR dlopen ()
ϡ餫ͳǼԤ NULL ֤
.SS "dlsym()"
ؿ
.BR dlsym ()
ϡ
.BR dlopen ()
֤ưŪ饤֥Ρ֥ϥɥפȡ
NULL ü줿ܥ̾ʸ˼ꡢ
Υܥ뤬ɤ줿Υɥ쥹֤
ܥ뤬ꤵ줿饤֥ȡꤵ줿饤֥꤬ɤݤ
.BR dlopen ()
ưŪ˥ɤƥ饤֥Τˤ⸫Ĥʤˤϡ
.BR dlsym ()
NULL ֤
.RB ( dlsym ()
ˤ븡ϡΥ饤֥ΰ¸طΥĥƬ
éäƹԤ)
ºݤˤϥܥͼΤ NULL ˤʤ뤳Ȥ⤢ (Τᡢ
.BR dlsym ()
֤ͤ NULL ǤäȤƤɬ⥨顼ȤǤϤʤ)
顼ɤǧˡϰʲ̤Ǥ:
.BR dlerror ()
ƤӽФưΥ顼֤ꥢƤ顢
.BR dlsym ()
ƤӽФθǤ⤦
.BR dlerror ()
ƤӽФơ
.BR dlerror ()
֤ͤѿ¸¸ͤ NULL Ǥ뤫Ƚꤹ롣
.PP
.B RTLD_DEFAULT
.B RTLD_NEXT
ȤĤ̤ʵϥɥ뤬롣
.B RTLD_DEFAULT
ϡǥեȤΥ饤֥긡ˤäơ
оݤΥܥ뤬ǽ˸Ȥõ
.B RTLD_NEXT
ϡ饤֥긡ǸߤΥ饤֥ʹߤǺǽ
ؿȤõεǽȤȤǡ̤ζͭ饤֥
ؿؤΥåѡ뤳ȤǤ롣
.SS "dlclose()"
ؿ
.BR dlclose ()
ưŪ饤֥Υϥɥ
.I handle
λȥȤ 1 餹ȥȤ 0 ˤʤꡢɤƤ
¾Υ饤֥꤫餽Υ饤֥Υܥ뤬ȤƤʤС
ưŪ饤֥ɤ롣
.LP
ؿ
.BR dlclose ()
ϡ 0 ֤顼ξ 0 ʳ֤
.SS "ѻߤ줿ܥ _init() _fini()"
.B _init
.B _fini
̤ʥܥȲ᤹롣
ưŪ饤֥
.BR _init ()
Ȥ̾Υ롼ݡȤƤС
Υɤϡ饤֥Υɸ塢
.BR dlopen ()
˼¹Ԥ롣
ưŪ饤֥
.BR _fini ()
Ȥ̾Υ롼ݡȤƤС
饤֥꤬ɤľˤΥ롼ƤӽФ롣
ƥεưեФɬפ硢
.BR gcc (1)
Υޥɥ饤
.I \-nostartfiles
ץꤹФ褤
.LP
Υ롼䡢gcc Υץ
.B \-nostartfiles
.B \-nostdlib
ϻѤʤȤ侩롣
Ȥȡ˾ޤʤư뤳Ȥ롣
ʤʤ顢(̤֤Ԥʤ¤) constructor/destructor
롼ϼ¹ԤʤǤ롣
.\" void _init(void) __attribute__((constructor));
.\" void _fini(void) __attribute__((destructor));
.LP
ˡ饤֥
.B __attribute__((constructor))
.B __attribute__((destructor))
δؿ°Ȥäɬפʥ롼ݡȤΤ褤
ˤĤƤ gcc info ڡȤΤȡ
constructor 롼
.BR dlopen ()
˼¹Ԥ졢
destructor 롼
.BR dlclose ()
˼¹Ԥ롣
.SS GNU Ǥγĥ: dladdr() dlvsym()
glibc Ǥ POSIX ˤϵܤƤʤؿ 2ɲäƤ롣
ץȥפϰʲ̤Ǥ롣
.sp
.nf
.B #define _GNU_SOURCE
.B #include <dlfcn.h>
.sp
.BI "int dladdr(void *" addr ", Dl_info *" info );
.sp
.BI "void *dlvsym(void *" handle ", char *" symbol ", char *" version );
.fi
.PP
ؿ
.BR dladdr ()
ϡؿΥݥˤȤꡢؿ̾ȴؿƤ
եβߤ롣
.I Dl_info
¤Τ˳Ǽ롣
.sp
.in +4n
.nf
typedef struct {
const char *dli_fname; /* Filename of defining object */
void *dli_fbase; /* Load address of that object */
const char *dli_sname; /* Name of nearest lower symbol */
void *dli_saddr; /* Exact value of nearest symbol */
} Dl_info;
.fi
.in
.sp
.BR dladdr ()
ϡ顼ˤ 0 ֤ 0 ʳ֤
.PP
ؿ
.BR dlvsym ()
.BR dlsym ()
Ʊư뤬СʸϤ
ɲäƤۤʤ롣
.SH
POSIX.1-2003 ˤ
.BR dlclose (),
.BR dlerror (),
.BR dlopen (),
.BR dlsym ().
εܤ롣
.SH
ܥ
.B RTLD_DEFAULT
.B RTLD_NEXT
.I <dlfcn.h>
Ƥꡢ
.I <dlfcn.h>
Υ롼
.B _GNU_SOURCE
ƤΤͭȤʤ롣
.\" .LP
.\" .BR dlerror ()
.\" ֤ʸѹ٤ǤϤʤƥˤäƤϡ
.\" ʲΤ褦ʥץȥפˤʤäƤ롣
.\" .sp
.\" .in +5
.\" .B "const char *dlerror(void);"
.\" .in
glibc 2.2.3 ʹߤǤϡ
.BR atexit (3)
Ȥäơ饤֥꤬ɤݤ˼ưŪ˸ƤӽФ
λϥɥ (exit handler) Ͽ뤳ȤǤ롣
.SS
dlopen եɸ SunOS ȤˤƤ롣
SunOS ˤ
.BR dladdr ()
⤢ä
.BR dlvsym ()
Ϥʤä
.SH
math 饤֥ɤ2.0 ;ɽ
.nf
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int
main(int argc, char **argv)
{
void *handle;
double (*cosine)(double);
char *error;
handle = dlopen("libm.so", RTLD_LAZY);
if (!handle) {
fprintf(stderr, "%s\en", dlerror());
exit(EXIT_FAILURE);
}
dlerror(); /* Clear any existing error */
/* Writing: cosine = (double (*)(double)) dlsym(handle, "cos");
would seem more natural, but the C99 standard leaves
casting from "void *" to a function pointer undefined.
The assignment used below is the POSIX.1\-2003 (Technical
Corrigendum 1) workaround; see the Rationale for the
POSIX specification of dlsym(). */
*(void **) (&cosine) = dlsym(handle, "cos");
.\" But in fact "gcc -O2 -Wall" will complain about the preceding cast.
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\en", error);
exit(EXIT_FAILURE);
}
printf("%f\en", (*cosine)(2.0));
dlclose(handle);
exit(EXIT_SUCCESS);
}
.fi
.PP
Υץ "foo.c" ˽ȤȡʲΥޥɤǥץ
ӥɤǤ롣
.in +4n
.LP
gcc \-rdynamic \-o foo foo.c \-ldl
.in
.PP
.BR _init ()
.BR _fini ()
ݡȤ饤֥ξ
ʲΤ褦ˤƥѥ뤹ɬפ롣
Ȥ \fIbar.c\fP ѥ뤹:
.in +4n
.LP
gcc \-shared \-nostartfiles \-o bar bar.c
.in
.SH Ϣ
.BR ld (1),
.BR ldd (1),
.BR dl_iterate_phdr (3),
.BR feature_test_macros (7),
.BR ld.so (8),
.BR ldconfig (8),
ld.so info pages, gcc info pages, ld info pages
|