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
)
212 /* In statically linked apps there might be no loaded object. */
213 if (call_map
== NULL
)
214 args
->nsid
= LM_ID_BASE
;
217 args
->nsid
= call_map
->l_ns
;
221 /* Maybe we have to expand a DST. */
222 if (__builtin_expect (dst
!= NULL
, 0))
224 size_t len
= strlen (file
);
228 /* DSTs must not appear in SUID/SGID programs. */
229 if (__libc_enable_secure
)
230 /* This is an error. */
231 GLRO(dl_signal_error
) (0, "dlopen", NULL
,
232 N_("DST not allowed in SUID/SGID programs"));
235 /* Determine how much space we need. We have to allocate the
237 required
= DL_DST_REQUIRED (call_map
, file
, len
, _dl_dst_count (dst
, 0));
239 /* Get space for the new file name. */
240 new_file
= (char *) alloca (required
+ 1);
242 /* Generate the new file name. */
243 _dl_dst_substitute (call_map
, file
, new_file
, 0);
245 /* If the substitution failed don't try to load. */
246 if (*new_file
== '\0')
247 GLRO(dl_signal_error
) (0, "dlopen", NULL
,
248 N_("empty dynamic string token substitution"));
250 /* Now we have a new file name. */
253 /* It does not matter whether call_map is set even if we
254 computed it only because of the DST. Since the path contains
255 a slash the value is not used. See dl-load.c. */
258 /* Load the named object. */
259 args
->map
= new = GLRO(dl_map_object
) (call_map
, file
, 0, lt_loaded
, 0,
260 mode
| __RTLD_CALLMAP
, args
->nsid
);
262 /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is
263 set and the object is not already loaded. */
266 assert (mode
& RTLD_NOLOAD
);
270 if (__builtin_expect (mode
& __RTLD_SPROF
, 0))
271 /* This happens only if we load a DSO for 'sprof'. */
274 /* This object is directly loaded. */
275 ++new->l_direct_opencount
;
277 /* It was already open. */
278 if (__builtin_expect (new->l_searchlist
.r_list
!= NULL
, 0))
280 /* Let the user know about the opencount. */
281 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
, 0))
282 GLRO(dl_debug_printf
) ("opening file=%s [%lu]; direct_opencount=%u\n\n",
283 new->l_name
, new->l_ns
, new->l_direct_opencount
);
285 /* If the user requested the object to be in the global namespace
286 but it is not so far, add it now. */
287 if ((mode
& RTLD_GLOBAL
) && new->l_global
== 0)
288 (void) add_to_global (new);
293 /* Load that object's dependencies. */
294 GLRO(dl_map_object_deps
) (new, NULL
, 0, 0,
295 mode
& (__RTLD_DLOPEN
| RTLD_DEEPBIND
));
297 /* So far, so good. Now check the versions. */
298 for (i
= 0; i
< new->l_searchlist
.r_nlist
; ++i
)
299 if (new->l_searchlist
.r_list
[i
]->l_real
->l_versions
== NULL
)
300 (void) GLRO(dl_check_map_versions
) (new->l_searchlist
.r_list
[i
]->l_real
,
307 /* Only do lazy relocation if `LD_BIND_NOW' is not set. */
308 lazy
= (mode
& RTLD_BINDING_MASK
) == RTLD_LAZY
&& GLRO(dl_lazy
);
310 /* Relocate the objects loaded. We do this in reverse order so that copy
311 relocs of earlier objects overwrite the data written by later objects. */
318 if (! l
->l_real
->l_relocated
)
321 if (GLRO(dl_profile
) != NULL
)
323 /* If this here is the shared object which we want to profile
324 make sure the profile is started. We can find out whether
325 this is necessary or not by observing the `_dl_profile_map'
326 variable. If was NULL but is not NULL afterwars we must
327 start the profiling. */
328 struct link_map
*old_profile_map
= GL(dl_profile_map
);
330 GLRO(dl_relocate_object
) (l
, l
->l_scope
, 1, 1);
332 if (old_profile_map
== NULL
&& GL(dl_profile_map
) != NULL
)
334 /* We must prepare the profiling. */
335 GLRO(dl_start_profile
) ();
337 /* Prevent unloading the object. */
338 GL(dl_profile_map
)->l_flags_1
|= DF_1_NODELETE
;
343 GLRO(dl_relocate_object
) (l
, l
->l_scope
, lazy
, 0);
352 /* Do static TLS initialization now if it has been delayed because
353 the TLS template might not be fully relocated at _dl_allocate_static_tls
355 for (l
= new; l
; l
= l
->l_next
)
356 if (l
->l_need_tls_init
)
358 l
->l_need_tls_init
= 0;
359 GL(dl_init_static_tls
) (l
);
362 /* We normally don't bump the TLS generation counter. There must be
363 actually a need to do this. */
367 /* If the file is not loaded now as a dependency, add the search
368 list of the newly loaded object to the scope. */
369 for (i
= 0; i
< new->l_searchlist
.r_nlist
; ++i
)
371 struct link_map
*imap
= new->l_searchlist
.r_list
[i
];
373 /* If the initializer has been called already, the object has
374 not been loaded here and now. */
375 if (imap
->l_init_called
&& imap
->l_type
== lt_loaded
)
377 struct r_scope_elem
**runp
= imap
->l_scope
;
380 while (*runp
!= NULL
)
387 /* Avoid duplicates. */
390 if (__builtin_expect (cnt
+ 1 >= imap
->l_scope_max
, 0))
392 /* The 'r_scope' array is too small. Allocate a new one
394 struct r_scope_elem
**newp
;
395 size_t new_size
= imap
->l_scope_max
* 2;
397 if (imap
->l_scope
== imap
->l_scope_mem
)
399 newp
= (struct r_scope_elem
**)
400 malloc (new_size
* sizeof (struct r_scope_elem
*));
402 GLRO(dl_signal_error
) (ENOMEM
, "dlopen", NULL
,
403 N_("cannot create scope list"));
404 imap
->l_scope
= memcpy (newp
, imap
->l_scope
,
405 cnt
* sizeof (imap
->l_scope
[0]));
409 newp
= (struct r_scope_elem
**)
410 realloc (imap
->l_scope
,
411 new_size
* sizeof (struct r_scope_elem
*));
413 GLRO(dl_signal_error
) (ENOMEM
, "dlopen", NULL
,
414 N_("cannot create scope list"));
415 imap
->l_scope
= newp
;
418 imap
->l_scope_max
= new_size
;
421 imap
->l_scope
[cnt
++] = &new->l_searchlist
;
422 imap
->l_scope
[cnt
] = NULL
;
425 /* Only add TLS memory if this object is loaded now and
426 therefore is not yet initialized. */
427 else if (! imap
->l_init_called
428 /* Only if the module defines thread local data. */
429 && __builtin_expect (imap
->l_tls_blocksize
> 0, 0))
431 /* Now that we know the object is loaded successfully add
432 modules containing TLS data to the dtv info table. We
433 might have to increase its size. */
434 struct dtv_slotinfo_list
*listp
;
435 struct dtv_slotinfo_list
*prevp
;
436 size_t idx
= imap
->l_tls_modid
;
438 assert (imap
->l_type
== lt_loaded
);
440 /* Find the place in the dtv slotinfo list. */
441 listp
= GL(dl_tls_dtv_slotinfo_list
);
442 prevp
= NULL
; /* Needed to shut up gcc. */
445 /* Does it fit in the array of this list element? */
446 if (idx
< listp
->len
)
452 while (listp
!= NULL
);
456 /* When we come here it means we have to add a new element
457 to the slotinfo list. And the new module must be in
461 listp
= prevp
->next
= (struct dtv_slotinfo_list
*)
462 malloc (sizeof (struct dtv_slotinfo_list
)
463 + TLS_SLOTINFO_SURPLUS
* sizeof (struct dtv_slotinfo
));
466 /* We ran out of memory. We will simply fail this
467 call but don't undo anything we did so far. The
468 application will crash or be terminated anyway very
471 /* We have to do this since some entries in the dtv
472 slotinfo array might already point to this
474 ++GL(dl_tls_generation
);
476 GLRO(dl_signal_error
) (ENOMEM
, "dlopen", NULL
, N_("\
477 cannot create TLS data structures"));
480 listp
->len
= TLS_SLOTINFO_SURPLUS
;
482 memset (listp
->slotinfo
, '\0',
483 TLS_SLOTINFO_SURPLUS
* sizeof (struct dtv_slotinfo
));
486 /* Add the information into the slotinfo data structure. */
487 listp
->slotinfo
[idx
].map
= imap
;
488 listp
->slotinfo
[idx
].gen
= GL(dl_tls_generation
) + 1;
490 /* We have to bump the generation counter. */
497 /* Bump the generation number if necessary. */
499 if (__builtin_expect (++GL(dl_tls_generation
) == 0, 0))
500 __libc_fatal (_("TLS generation counter wrapped! Please report this."));
503 /* Run the initializer functions of new objects. */
504 GLRO(dl_init
) (new, __libc_argc
, __libc_argv
, __environ
);
506 /* Now we can make the new map available in the global scope. */
507 if (mode
& RTLD_GLOBAL
)
508 /* Move the object in the global namespace. */
509 if (add_to_global (new) != 0)
513 /* Mark the object as not deletable if the RTLD_NODELETE flags was
515 if (__builtin_expect (mode
& RTLD_NODELETE
, 0))
516 new->l_flags_1
|= DF_1_NODELETE
;
519 /* We must be the static _dl_open in libc.a. A static program that
520 has loaded a dynamic object now has competition. */
521 __libc_multiple_libcs
= 1;
524 /* Let the user know about the opencount. */
525 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
, 0))
526 GLRO(dl_debug_printf
) ("opening file=%s [%lu]; direct_opencount=%u\n\n",
527 new->l_name
, new->l_ns
, new->l_direct_opencount
);
533 _dl_open (const char *file
, int mode
, const void *caller_dlopen
, Lmid_t nsid
)
535 struct dl_open_args args
;
537 const char *errstring
;
540 if ((mode
& RTLD_BINDING_MASK
) == 0)
541 /* One of the flags must be set. */
542 GLRO(dl_signal_error
) (EINVAL
, file
, NULL
,
543 N_("invalid mode for dlopen()"));
545 /* Make sure we are alone. */
546 __rtld_lock_lock_recursive (GL(dl_load_lock
));
548 if (nsid
== LM_ID_NEWLM
)
550 /* Find a new namespace. */
551 for (nsid
= 1; nsid
< DL_NNS
; ++nsid
)
552 if (GL(dl_ns
)[nsid
]._ns_loaded
== NULL
)
557 /* No more namespace available. */
558 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
560 GLRO(dl_signal_error
) (EINVAL
, file
, NULL
, N_("\
561 no more namespaces available for dlmopen()"));
564 /* Never allow loading a DSO in a namespace which is empty. Such
565 direct placements is only causing problems. */
566 else if (nsid
!= LM_ID_BASE
&& nsid
!= __LM_ID_CALLER
567 && GL(dl_ns
)[nsid
]._ns_nloaded
== 0)
568 GLRO(dl_signal_error
) (EINVAL
, file
, NULL
,
569 N_("invalid target namespace in dlmopen()"));
573 args
.caller_dlopen
= caller_dlopen
;
574 args
.caller_dl_open
= RETURN_ADDRESS (0);
577 errcode
= GLRO(dl_catch_error
) (&objname
, &errstring
, dl_open_worker
, &args
);
580 /* We must munmap() the cache file. */
581 GLRO(dl_unload_cache
) ();
584 /* Release the lock. */
585 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
587 if (__builtin_expect (errstring
!= NULL
, 0))
589 /* Some error occurred during loading. */
590 char *local_errstring
;
591 size_t len_errstring
;
593 /* Remove the object from memory. It may be in an inconsistent
594 state if relocation failed, for example. */
598 /* Maybe some of the modules which were loaded uses TLS.
599 Since it will be removed in the following _dl_close call
600 we have to mark the dtv array as having gaps to fill
601 the holes. This is a pessimistic assumption which won't
603 GL(dl_tls_dtv_gaps
) = true;
606 _dl_close (args
.map
);
609 /* Make a local copy of the error string so that we can release the
610 memory allocated for it. */
611 len_errstring
= strlen (errstring
) + 1;
612 if (objname
== errstring
+ len_errstring
)
614 size_t total_len
= len_errstring
+ strlen (objname
) + 1;
615 local_errstring
= alloca (total_len
);
616 memcpy (local_errstring
, errstring
, total_len
);
617 objname
= local_errstring
+ len_errstring
;
621 local_errstring
= alloca (len_errstring
);
622 memcpy (local_errstring
, errstring
, len_errstring
);
625 if (errstring
!= _dl_out_of_memory
)
626 free ((char *) errstring
);
628 /* Reraise the error. */
629 GLRO(dl_signal_error
) (errcode
, objname
, NULL
, local_errstring
);
633 DL_STATIC_INIT (args
.map
);
638 libc_hidden_def (_dl_open
)
645 show_scope (struct link_map
*new)
649 for (scope_cnt
= 0; new->l_scope
[scope_cnt
] != NULL
; ++scope_cnt
)
654 numbuf
[0] = '0' + scope_cnt
;
656 _dl_printf ("scope %s:", numbuf
);
658 for (cnt
= 0; cnt
< new->l_scope
[scope_cnt
]->r_nlist
; ++cnt
)
659 if (*new->l_scope
[scope_cnt
]->r_list
[cnt
]->l_name
)
660 _dl_printf (" %s", new->l_scope
[scope_cnt
]->r_list
[cnt
]->l_name
);
662 _dl_printf (" <main>");