1 /* Load a shared object at runtime, relocate it, and run its initializer.
2 Copyright (C) 1996-2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include <sys/mman.h> /* Check whether MAP_COPY is defined. */
29 #include <sys/param.h>
30 #include <bits/libc-lock.h>
39 /* Giving this initialized value preallocates some surplus bytes in the
40 static TLS area, see __libc_setup_tls (libc-tls.c). */
41 size_t _dl_tls_static_size
= 2048;
44 extern ElfW(Addr
) _dl_sysdep_start (void **start_argptr
,
45 void (*dl_main
) (const ElfW(Phdr
) *phdr
,
47 ElfW(Addr
) *user_entry
));
48 weak_extern (BP_SYM (_dl_sysdep_start
))
50 extern int __libc_multiple_libcs
; /* Defined in init-first.c. */
52 extern int __libc_argc attribute_hidden
;
53 extern char **__libc_argv attribute_hidden
;
55 extern char **__environ
;
57 /* Undefine the following for debugging. */
58 /* #define SCOPE_DEBUG 1 */
60 static void show_scope (struct link_map
*new);
63 /* We must be carefull not to leave us in an inconsistent state. Thus we
64 catch any error and re-raise it after cleaning up. */
70 /* This is the caller of the dlopen() function. */
71 const void *caller_dlopen
;
72 /* This is the caller if _dl_open(). */
73 const void *caller_dl_open
;
81 add_to_global (struct link_map
*new)
83 struct link_map
**new_global
;
84 unsigned int to_add
= 0;
87 /* Count the objects we have to put in the global scope. */
88 for (cnt
= 0; cnt
< new->l_searchlist
.r_nlist
; ++cnt
)
89 if (new->l_searchlist
.r_list
[cnt
]->l_global
== 0)
92 /* The symbols of the new objects and its dependencies are to be
93 introduced into the global scope that will be used to resolve
94 references from other dynamically-loaded objects.
96 The global scope is the searchlist in the main link map. We
97 extend this list if necessary. There is one problem though:
98 since this structure was allocated very early (before the libc
99 is loaded) the memory it uses is allocated by the malloc()-stub
100 in the ld.so. When we come here these functions are not used
101 anymore. Instead the malloc() implementation of the libc is
102 used. But this means the block from the main map cannot be used
103 in an realloc() call. Therefore we allocate a completely new
104 array the first time we have to add something to the locale scope. */
106 if (GL(dl_ns
)[new->l_ns
]._ns_global_scope_alloc
== 0)
108 /* This is the first dynamic object given global scope. */
109 GL(dl_ns
)[new->l_ns
]._ns_global_scope_alloc
110 = GL(dl_ns
)[new->l_ns
]._ns_main_searchlist
->r_nlist
+ to_add
+ 8;
111 new_global
= (struct link_map
**)
112 malloc (GL(dl_ns
)[new->l_ns
]._ns_global_scope_alloc
113 * sizeof (struct link_map
*));
114 if (new_global
== NULL
)
116 GL(dl_ns
)[new->l_ns
]._ns_global_scope_alloc
= 0;
118 GLRO(dl_signal_error
) (ENOMEM
, new->l_libname
->name
, NULL
,
119 N_("cannot extend global scope"));
123 /* Copy over the old entries. */
124 GL(dl_ns
)[new->l_ns
]._ns_main_searchlist
->r_list
125 = memcpy (new_global
,
126 GL(dl_ns
)[new->l_ns
]._ns_main_searchlist
->r_list
,
127 (GL(dl_ns
)[new->l_ns
]._ns_main_searchlist
->r_nlist
128 * sizeof (struct link_map
*)));
130 else if (GL(dl_ns
)[new->l_ns
]._ns_main_searchlist
->r_nlist
+ to_add
131 > GL(dl_ns
)[new->l_ns
]._ns_global_scope_alloc
)
133 /* We have to extend the existing array of link maps in the
135 new_global
= (struct link_map
**)
136 realloc (GL(dl_ns
)[new->l_ns
]._ns_main_searchlist
->r_list
,
137 ((GL(dl_ns
)[new->l_ns
]._ns_global_scope_alloc
+ to_add
+ 8)
138 * sizeof (struct link_map
*)));
139 if (new_global
== NULL
)
142 GL(dl_ns
)[new->l_ns
]._ns_global_scope_alloc
+= to_add
+ 8;
143 GL(dl_ns
)[new->l_ns
]._ns_main_searchlist
->r_list
= new_global
;
146 /* Now add the new entries. */
147 for (cnt
= 0; cnt
< new->l_searchlist
.r_nlist
; ++cnt
)
149 struct link_map
*map
= new->l_searchlist
.r_list
[cnt
];
151 if (map
->l_global
== 0)
154 GL(dl_ns
)[new->l_ns
]._ns_main_searchlist
->r_list
[GL(dl_ns
)[new->l_ns
]._ns_main_searchlist
->r_nlist
]
156 ++GL(dl_ns
)[new->l_ns
]._ns_main_searchlist
->r_nlist
;
165 dl_open_worker (void *a
)
167 struct dl_open_args
*args
= a
;
168 const char *file
= args
->file
;
169 int mode
= args
->mode
;
170 struct link_map
*new, *l
;
176 struct link_map
*call_map
= NULL
;
178 /* Check whether _dl_open() has been called from a valid DSO. */
179 if (__check_caller (args
->caller_dl_open
, allow_libc
|allow_libdl
) != 0)
180 GLRO(dl_signal_error
) (0, "dlopen", NULL
, N_("invalid caller"));
182 /* Determine the caller's map if necessary. This is needed in case
183 we have a DST, when we don't know the namespace ID we have to put
184 the new object in, or when the file name has no path in which
185 case we need to look along the RUNPATH/RPATH of the caller. */
186 const char *dst
= strchr (file
, '$');
187 if (dst
!= NULL
|| args
->nsid
== __LM_ID_CALLER
188 || strchr (file
, '/') == NULL
)
190 const void *caller_dlopen
= args
->caller_dlopen
;
192 /* We have to find out from which object the caller is calling.
193 By default we assume this is the main application. */
194 call_map
= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
;
196 for (Lmid_t ns
= 0; ns
< DL_NNS
; ++ns
)
197 for (l
= GL(dl_ns
)[ns
]._ns_loaded
; l
!= NULL
; l
= l
->l_next
)
198 if (caller_dlopen
>= (const void *) l
->l_map_start
199 && caller_dlopen
< (const void *) l
->l_map_end
)
201 /* There must be exactly one DSO for the range of the virtual
202 memory. Otherwise something is really broken. */
203 assert (ns
== l
->l_ns
);
209 if (args
->nsid
== __LM_ID_CALLER
)
210 args
->nsid
= call_map
->l_ns
;
213 /* Maybe we have to expand a DST. */
214 if (__builtin_expect (dst
!= NULL
, 0))
216 size_t len
= strlen (file
);
220 /* DSTs must not appear in SUID/SGID programs. */
221 if (__libc_enable_secure
)
222 /* This is an error. */
223 GLRO(dl_signal_error
) (0, "dlopen", NULL
,
224 N_("DST not allowed in SUID/SGID programs"));
227 /* Determine how much space we need. We have to allocate the
229 required
= DL_DST_REQUIRED (call_map
, file
, len
, _dl_dst_count (dst
, 0));
231 /* Get space for the new file name. */
232 new_file
= (char *) alloca (required
+ 1);
234 /* Generate the new file name. */
235 _dl_dst_substitute (call_map
, file
, new_file
, 0);
237 /* If the substitution failed don't try to load. */
238 if (*new_file
== '\0')
239 GLRO(dl_signal_error
) (0, "dlopen", NULL
,
240 N_("empty dynamic string token substitution"));
242 /* Now we have a new file name. */
245 /* It does not matter whether call_map is set even if we
246 computed it only because of the DST. Since the path contains
247 a slash the value is not used. See dl-load.c. */
250 /* Load the named object. */
251 args
->map
= new = GLRO(dl_map_object
) (call_map
, file
, 0, lt_loaded
, 0,
252 mode
| __RTLD_CALLMAP
, args
->nsid
);
254 /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is
255 set and the object is not already loaded. */
258 assert (mode
& RTLD_NOLOAD
);
262 if (__builtin_expect (mode
& __RTLD_SPROF
, 0))
263 /* This happens only if we load a DSO for 'sprof'. */
266 /* This object is directly loaded. */
267 ++new->l_direct_opencount
;
269 /* It was already open. */
270 if (__builtin_expect (new->l_searchlist
.r_list
!= NULL
, 0))
272 /* Let the user know about the opencount. */
273 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
, 0))
274 GLRO(dl_debug_printf
) ("opening file=%s [%lu]; opencount=%u\n\n",
275 new->l_name
, new->l_ns
, new->l_opencount
);
277 /* If the user requested the object to be in the global namespace
278 but it is not so far, add it now. */
279 if ((mode
& RTLD_GLOBAL
) && new->l_global
== 0)
280 (void) add_to_global (new);
282 if (new->l_direct_opencount
== 1)
283 /* This is the only direct reference. Increment all the
284 dependencies' reference counter. */
285 for (i
= 0; i
< new->l_searchlist
.r_nlist
; ++i
)
286 ++new->l_searchlist
.r_list
[i
]->l_opencount
;
288 /* Increment just the reference counter of the object. */
294 /* Load that object's dependencies. */
295 GLRO(dl_map_object_deps
) (new, NULL
, 0, 0,
296 mode
& (__RTLD_DLOPEN
| RTLD_DEEPBIND
));
298 /* So far, so good. Now check the versions. */
299 for (i
= 0; i
< new->l_searchlist
.r_nlist
; ++i
)
300 if (new->l_searchlist
.r_list
[i
]->l_real
->l_versions
== NULL
)
301 (void) GLRO(dl_check_map_versions
) (new->l_searchlist
.r_list
[i
]->l_real
,
308 /* Only do lazy relocation if `LD_BIND_NOW' is not set. */
309 lazy
= (mode
& RTLD_BINDING_MASK
) == RTLD_LAZY
&& GLRO(dl_lazy
);
311 /* Relocate the objects loaded. We do this in reverse order so that copy
312 relocs of earlier objects overwrite the data written by later objects. */
319 if (! l
->l_real
->l_relocated
)
322 if (GLRO(dl_profile
) != NULL
)
324 /* If this here is the shared object which we want to profile
325 make sure the profile is started. We can find out whether
326 this is necessary or not by observing the `_dl_profile_map'
327 variable. If was NULL but is not NULL afterwars we must
328 start the profiling. */
329 struct link_map
*old_profile_map
= GL(dl_profile_map
);
331 GLRO(dl_relocate_object
) (l
, l
->l_scope
, 1, 1);
333 if (old_profile_map
== NULL
&& GL(dl_profile_map
) != NULL
)
335 /* We must prepare the profiling. */
336 GLRO(dl_start_profile
) ();
338 /* Prevent unloading the object. */
339 GL(dl_profile_map
)->l_flags_1
|= DF_1_NODELETE
;
344 GLRO(dl_relocate_object
) (l
, l
->l_scope
, lazy
, 0);
353 /* Do static TLS initialization now if it has been delayed because
354 the TLS template might not be fully relocated at _dl_allocate_static_tls
356 for (l
= new; l
; l
= l
->l_next
)
357 if (l
->l_need_tls_init
)
359 l
->l_need_tls_init
= 0;
360 GL(dl_init_static_tls
) (l
);
363 /* We normally don't bump the TLS generation counter. There must be
364 actually a need to do this. */
368 /* Increment the open count for all dependencies. If the file is
369 not loaded as a dependency here add the search list of the newly
370 loaded object to the scope. */
371 for (i
= 0; i
< new->l_searchlist
.r_nlist
; ++i
)
372 if (++new->l_searchlist
.r_list
[i
]->l_opencount
> 1
373 && new->l_real
->l_searchlist
.r_list
[i
]->l_type
== lt_loaded
)
375 struct link_map
*imap
= new->l_searchlist
.r_list
[i
];
376 struct r_scope_elem
**runp
= imap
->l_scope
;
379 while (*runp
!= NULL
)
381 /* This can happen if imap was just loaded, but during
382 relocation had l_opencount bumped because of relocation
383 dependency. Avoid duplicates in l_scope. */
384 if (__builtin_expect (*runp
== &new->l_searchlist
, 0))
392 /* Avoid duplicates. */
395 if (__builtin_expect (cnt
+ 1 >= imap
->l_scope_max
, 0))
397 /* The 'r_scope' array is too small. Allocate a new one
399 struct r_scope_elem
**newp
;
400 size_t new_size
= imap
->l_scope_max
* 2;
402 if (imap
->l_scope
== imap
->l_scope_mem
)
404 newp
= (struct r_scope_elem
**)
405 malloc (new_size
* sizeof (struct r_scope_elem
*));
407 GLRO(dl_signal_error
) (ENOMEM
, "dlopen", NULL
,
408 N_("cannot create scope list"));
409 imap
->l_scope
= memcpy (newp
, imap
->l_scope
,
410 cnt
* sizeof (imap
->l_scope
[0]));
414 newp
= (struct r_scope_elem
**)
415 realloc (imap
->l_scope
,
416 new_size
* sizeof (struct r_scope_elem
*));
418 GLRO(dl_signal_error
) (ENOMEM
, "dlopen", NULL
,
419 N_("cannot create scope list"));
420 imap
->l_scope
= newp
;
423 imap
->l_scope_max
= new_size
;
426 imap
->l_scope
[cnt
++] = &new->l_searchlist
;
427 imap
->l_scope
[cnt
] = NULL
;
430 else if (new->l_searchlist
.r_list
[i
]->l_opencount
== 1
431 /* Only if the module defines thread local data. */
432 && __builtin_expect (new->l_searchlist
.r_list
[i
]->l_tls_blocksize
435 /* Now that we know the object is loaded successfully add
436 modules containing TLS data to the dtv info table. We
437 might have to increase its size. */
438 struct dtv_slotinfo_list
*listp
;
439 struct dtv_slotinfo_list
*prevp
;
440 size_t idx
= new->l_searchlist
.r_list
[i
]->l_tls_modid
;
442 assert (new->l_searchlist
.r_list
[i
]->l_type
== lt_loaded
);
444 /* Find the place in the dtv slotinfo list. */
445 listp
= GL(dl_tls_dtv_slotinfo_list
);
446 prevp
= NULL
; /* Needed to shut up gcc. */
449 /* Does it fit in the array of this list element? */
450 if (idx
< listp
->len
)
456 while (listp
!= NULL
);
460 /* When we come here it means we have to add a new element
461 to the slotinfo list. And the new module must be in
465 listp
= prevp
->next
= (struct dtv_slotinfo_list
*)
466 malloc (sizeof (struct dtv_slotinfo_list
)
467 + TLS_SLOTINFO_SURPLUS
* sizeof (struct dtv_slotinfo
));
470 /* We ran out of memory. We will simply fail this
471 call but don't undo anything we did so far. The
472 application will crash or be terminated anyway very
475 /* We have to do this since some entries in the dtv
476 slotinfo array might already point to this
478 ++GL(dl_tls_generation
);
480 GLRO(dl_signal_error
) (ENOMEM
, "dlopen", NULL
, N_("\
481 cannot create TLS data structures"));
484 listp
->len
= TLS_SLOTINFO_SURPLUS
;
486 memset (listp
->slotinfo
, '\0',
487 TLS_SLOTINFO_SURPLUS
* sizeof (struct dtv_slotinfo
));
490 /* Add the information into the slotinfo data structure. */
491 listp
->slotinfo
[idx
].map
= new->l_searchlist
.r_list
[i
];
492 listp
->slotinfo
[idx
].gen
= GL(dl_tls_generation
) + 1;
494 /* We have to bump the generation counter. */
498 /* Bump the generation number if necessary. */
500 if (__builtin_expect (++GL(dl_tls_generation
) == 0, 0))
501 __libc_fatal (_("TLS generation counter wrapped! Please send report with the 'glibcbug' script."));
504 /* Run the initializer functions of new objects. */
505 GLRO(dl_init
) (new, __libc_argc
, __libc_argv
, __environ
);
507 /* Now we can make the new map available in the global scope. */
508 if (mode
& RTLD_GLOBAL
)
509 /* Move the object in the global namespace. */
510 if (add_to_global (new) != 0)
514 /* Mark the object as not deletable if the RTLD_NODELETE flags was
516 if (__builtin_expect (mode
& RTLD_NODELETE
, 0))
517 new->l_flags_1
|= DF_1_NODELETE
;
520 /* We must be the static _dl_open in libc.a. A static program that
521 has loaded a dynamic object now has competition. */
522 __libc_multiple_libcs
= 1;
525 /* Let the user know about the opencount. */
526 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
, 0))
527 GLRO(dl_debug_printf
) ("opening file=%s [%lu]; opencount=%u\n\n",
528 new->l_name
, new->l_ns
, new->l_opencount
);
534 _dl_open (const char *file
, int mode
, const void *caller_dlopen
, Lmid_t nsid
)
536 struct dl_open_args args
;
538 const char *errstring
;
541 if ((mode
& RTLD_BINDING_MASK
) == 0)
542 /* One of the flags must be set. */
543 GLRO(dl_signal_error
) (EINVAL
, file
, NULL
,
544 N_("invalid mode for dlopen()"));
546 /* Make sure we are alone. */
547 __rtld_lock_lock_recursive (GL(dl_load_lock
));
549 if (nsid
== LM_ID_NEWLM
)
551 /* Find a new namespace. */
552 for (nsid
= 1; nsid
< DL_NNS
; ++nsid
)
553 if (GL(dl_ns
)[nsid
]._ns_loaded
== NULL
)
558 /* No more namespace available. */
559 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
561 GLRO(dl_signal_error
) (EINVAL
, file
, NULL
, N_("\
562 no more namespaces available for dlmopen()"));
568 args
.caller_dlopen
= caller_dlopen
;
569 args
.caller_dl_open
= RETURN_ADDRESS (0);
572 errcode
= GLRO(dl_catch_error
) (&objname
, &errstring
, dl_open_worker
, &args
);
575 /* We must munmap() the cache file. */
576 GLRO(dl_unload_cache
) ();
579 /* Release the lock. */
580 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
582 if (__builtin_expect (errstring
!= NULL
, 0))
584 /* Some error occurred during loading. */
585 char *local_errstring
;
586 size_t len_errstring
;
588 /* Remove the object from memory. It may be in an inconsistent
589 state if relocation failed, for example. */
594 /* Increment open counters for all objects since this
595 sometimes has not happened yet. */
596 if (args
.map
->l_searchlist
.r_list
[0]->l_opencount
== 0)
597 for (i
= 0; i
< args
.map
->l_searchlist
.r_nlist
; ++i
)
598 ++args
.map
->l_searchlist
.r_list
[i
]->l_opencount
;
601 /* Maybe some of the modules which were loaded uses TLS.
602 Since it will be removed in the following _dl_close call
603 we have to mark the dtv array as having gaps to fill
604 the holes. This is a pessimistic assumption which won't
606 GL(dl_tls_dtv_gaps
) = true;
609 _dl_close (args
.map
);
612 /* Make a local copy of the error string so that we can release the
613 memory allocated for it. */
614 len_errstring
= strlen (errstring
) + 1;
615 if (objname
== errstring
+ len_errstring
)
617 size_t total_len
= len_errstring
+ strlen (objname
) + 1;
618 local_errstring
= alloca (total_len
);
619 memcpy (local_errstring
, errstring
, total_len
);
620 objname
= local_errstring
+ len_errstring
;
624 local_errstring
= alloca (len_errstring
);
625 memcpy (local_errstring
, errstring
, len_errstring
);
628 if (errstring
!= _dl_out_of_memory
)
629 free ((char *) errstring
);
631 /* Reraise the error. */
632 GLRO(dl_signal_error
) (errcode
, objname
, NULL
, local_errstring
);
636 DL_STATIC_INIT (args
.map
);
641 libc_hidden_def (_dl_open
)
648 show_scope (struct link_map
*new)
652 for (scope_cnt
= 0; new->l_scope
[scope_cnt
] != NULL
; ++scope_cnt
)
657 numbuf
[0] = '0' + scope_cnt
;
659 _dl_printf ("scope %s:", numbuf
);
661 for (cnt
= 0; cnt
< new->l_scope
[scope_cnt
]->r_nlist
; ++cnt
)
662 if (*new->l_scope
[scope_cnt
]->r_list
[cnt
]->l_name
)
663 _dl_printf (" %s", new->l_scope
[scope_cnt
]->r_list
[cnt
]->l_name
);
665 _dl_printf (" <main>");