1 /* Load a shared object at runtime, relocate it, and run its initializer.
2 Copyright (C) 1996-2001, 2002, 2003 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>
38 /* Giving this initialized value preallocates some surplus bytes in the
39 static TLS area, see __libc_setup_tls (libc-tls.c). */
40 size_t _dl_tls_static_size
= 2048;
43 extern ElfW(Addr
) _dl_sysdep_start (void **start_argptr
,
44 void (*dl_main
) (const ElfW(Phdr
) *phdr
,
46 ElfW(Addr
) *user_entry
));
47 weak_extern (BP_SYM (_dl_sysdep_start
))
49 extern int __libc_multiple_libcs
; /* Defined in init-first.c. */
51 extern int __libc_argc attribute_hidden
;
52 extern char **__libc_argv attribute_hidden
;
54 extern char **__environ
;
56 /* Undefine the following for debugging. */
57 /* #define SCOPE_DEBUG 1 */
59 static void show_scope (struct link_map
*new);
62 /* We must be carefull not to leave us in an inconsistent state. Thus we
63 catch any error and re-raise it after cleaning up. */
75 add_to_global (struct link_map
*new)
77 struct link_map
**new_global
;
78 unsigned int to_add
= 0;
81 /* Count the objects we have to put in the global scope. */
82 for (cnt
= 0; cnt
< new->l_searchlist
.r_nlist
; ++cnt
)
83 if (new->l_searchlist
.r_list
[cnt
]->l_global
== 0)
86 /* The symbols of the new objects and its dependencies are to be
87 introduced into the global scope that will be used to resolve
88 references from other dynamically-loaded objects.
90 The global scope is the searchlist in the main link map. We
91 extend this list if necessary. There is one problem though:
92 since this structure was allocated very early (before the libc
93 is loaded) the memory it uses is allocated by the malloc()-stub
94 in the ld.so. When we come here these functions are not used
95 anymore. Instead the malloc() implementation of the libc is
96 used. But this means the block from the main map cannot be used
97 in an realloc() call. Therefore we allocate a completely new
98 array the first time we have to add something to the locale scope. */
100 if (GL(dl_global_scope_alloc
) == 0)
102 /* This is the first dynamic object given global scope. */
103 GL(dl_global_scope_alloc
) = GL(dl_main_searchlist
)->r_nlist
+ to_add
+ 8;
104 new_global
= (struct link_map
**)
105 malloc (GL(dl_global_scope_alloc
) * sizeof (struct link_map
*));
106 if (new_global
== NULL
)
108 GL(dl_global_scope_alloc
) = 0;
110 _dl_signal_error (ENOMEM
, new->l_libname
->name
, NULL
,
111 N_("cannot extend global scope"));
115 /* Copy over the old entries. */
116 memcpy (new_global
, GL(dl_main_searchlist
)->r_list
,
117 (GL(dl_main_searchlist
)->r_nlist
* sizeof (struct link_map
*)));
119 GL(dl_main_searchlist
)->r_list
= new_global
;
121 else if (GL(dl_main_searchlist
)->r_nlist
+ to_add
122 > GL(dl_global_scope_alloc
))
124 /* We have to extend the existing array of link maps in the
126 new_global
= (struct link_map
**)
127 realloc (GL(dl_main_searchlist
)->r_list
,
128 ((GL(dl_global_scope_alloc
) + to_add
+ 8)
129 * sizeof (struct link_map
*)));
130 if (new_global
== NULL
)
133 GL(dl_global_scope_alloc
) += to_add
+ 8;
134 GL(dl_main_searchlist
)->r_list
= new_global
;
137 /* Now add the new entries. */
138 for (cnt
= 0; cnt
< new->l_searchlist
.r_nlist
; ++cnt
)
140 struct link_map
*map
= new->l_searchlist
.r_list
[cnt
];
142 if (map
->l_global
== 0)
145 GL(dl_main_searchlist
)->r_list
[GL(dl_main_searchlist
)->r_nlist
]
147 ++GL(dl_main_searchlist
)->r_nlist
;
156 dl_open_worker (void *a
)
158 struct dl_open_args
*args
= a
;
159 const char *file
= args
->file
;
160 int mode
= args
->mode
;
161 struct link_map
*new, *l
;
169 /* Maybe we have to expand a DST. */
170 dst
= strchr (file
, '$');
171 if (__builtin_expect (dst
!= NULL
, 0))
173 const void *caller
= args
->caller
;
174 size_t len
= strlen (file
);
176 struct link_map
*call_map
;
179 /* DSTs must not appear in SUID/SGID programs. */
180 if (__libc_enable_secure
)
181 /* This is an error. */
182 _dl_signal_error (0, "dlopen", NULL
,
183 N_("DST not allowed in SUID/SGID programs"));
185 /* We have to find out from which object the caller is calling. */
187 for (l
= GL(dl_loaded
); l
; l
= l
->l_next
)
188 if (caller
>= (const void *) l
->l_map_start
189 && caller
< (const void *) l
->l_map_end
)
191 /* There must be exactly one DSO for the range of the virtual
192 memory. Otherwise something is really broken. */
197 if (call_map
== NULL
)
198 /* In this case we assume this is the main application. */
199 call_map
= GL(dl_loaded
);
201 /* Determine how much space we need. We have to allocate the
203 required
= DL_DST_REQUIRED (call_map
, file
, len
, _dl_dst_count (dst
, 0));
205 /* Get space for the new file name. */
206 new_file
= (char *) alloca (required
+ 1);
208 /* Generate the new file name. */
209 _dl_dst_substitute (call_map
, file
, new_file
, 0);
211 /* If the substitution failed don't try to load. */
212 if (*new_file
== '\0')
213 _dl_signal_error (0, "dlopen", NULL
,
214 N_("empty dynamic string token substitution"));
216 /* Now we have a new file name. */
220 /* Load the named object. */
221 args
->map
= new = _dl_map_object (NULL
, file
, 0, lt_loaded
, 0, mode
);
223 /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is
224 set and the object is not already loaded. */
227 assert (mode
& RTLD_NOLOAD
);
231 if (__builtin_expect (mode
& __RTLD_SPROF
, 0))
232 /* This happens only if we load a DSO for 'sprof'. */
235 /* It was already open. */
236 if (new->l_searchlist
.r_list
!= NULL
)
238 /* Let the user know about the opencount. */
239 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_FILES
, 0))
240 _dl_debug_printf ("opening file=%s; opencount == %u\n\n",
241 new->l_name
, new->l_opencount
);
243 /* If the user requested the object to be in the global namespace
244 but it is not so far, add it now. */
245 if ((mode
& RTLD_GLOBAL
) && new->l_global
== 0)
246 (void) add_to_global (new);
248 /* Increment just the reference counter of the object. */
254 /* Load that object's dependencies. */
255 _dl_map_object_deps (new, NULL
, 0, 0, mode
& __RTLD_DLOPEN
);
257 /* So far, so good. Now check the versions. */
258 for (i
= 0; i
< new->l_searchlist
.r_nlist
; ++i
)
259 if (new->l_searchlist
.r_list
[i
]->l_versions
== NULL
)
260 (void) _dl_check_map_versions (new->l_searchlist
.r_list
[i
], 0, 0);
266 /* Only do lazy relocation if `LD_BIND_NOW' is not set. */
267 lazy
= (mode
& RTLD_BINDING_MASK
) == RTLD_LAZY
&& GL(dl_lazy
);
269 /* Relocate the objects loaded. We do this in reverse order so that copy
270 relocs of earlier objects overwrite the data written by later objects. */
277 if (! l
->l_relocated
)
280 if (GL(dl_profile
) != NULL
)
282 /* If this here is the shared object which we want to profile
283 make sure the profile is started. We can find out whether
284 this is necessary or not by observing the `_dl_profile_map'
285 variable. If was NULL but is not NULL afterwars we must
286 start the profiling. */
287 struct link_map
*old_profile_map
= GL(dl_profile_map
);
289 _dl_relocate_object (l
, l
->l_scope
, 1, 1);
291 if (old_profile_map
== NULL
&& GL(dl_profile_map
) != NULL
)
292 /* We must prepare the profiling. */
293 _dl_start_profile (GL(dl_profile_map
), GL(dl_profile_output
));
297 _dl_relocate_object (l
, l
->l_scope
, lazy
, 0);
306 /* We normally don't bump the TLS generation counter. There must be
307 actually a need to do this. */
311 /* Increment the open count for all dependencies. If the file is
312 not loaded as a dependency here add the search list of the newly
313 loaded object to the scope. */
314 for (i
= 0; i
< new->l_searchlist
.r_nlist
; ++i
)
315 if (++new->l_searchlist
.r_list
[i
]->l_opencount
> 1
316 && new->l_searchlist
.r_list
[i
]->l_type
== lt_loaded
)
318 struct link_map
*imap
= new->l_searchlist
.r_list
[i
];
319 struct r_scope_elem
**runp
= imap
->l_scope
;
322 while (*runp
!= NULL
)
324 /* This can happen if imap was just loaded, but during
325 relocation had l_opencount bumped because of relocation
326 dependency. Avoid duplicates in l_scope. */
327 if (__builtin_expect (*runp
== &new->l_searchlist
, 0))
335 /* Avoid duplicates. */
338 if (__builtin_expect (cnt
+ 1 >= imap
->l_scope_max
, 0))
340 /* The 'r_scope' array is too small. Allocate a new one
342 struct r_scope_elem
**newp
;
343 size_t new_size
= imap
->l_scope_max
* 2;
345 if (imap
->l_scope
== imap
->l_scope_mem
)
347 newp
= (struct r_scope_elem
**)
348 malloc (new_size
* sizeof (struct r_scope_elem
*));
350 _dl_signal_error (ENOMEM
, "dlopen", NULL
,
351 N_("cannot create scope list"));
352 imap
->l_scope
= memcpy (newp
, imap
->l_scope
,
353 cnt
* sizeof (imap
->l_scope
[0]));
357 newp
= (struct r_scope_elem
**)
358 realloc (imap
->l_scope
,
359 new_size
* sizeof (struct r_scope_elem
*));
361 _dl_signal_error (ENOMEM
, "dlopen", NULL
,
362 N_("cannot create scope list"));
363 imap
->l_scope
= newp
;
366 imap
->l_scope_max
= new_size
;
369 imap
->l_scope
[cnt
++] = &new->l_searchlist
;
370 imap
->l_scope
[cnt
] = NULL
;
373 else if (new->l_searchlist
.r_list
[i
]->l_opencount
== 1
374 /* Only if the module defines thread local data. */
375 && __builtin_expect (new->l_searchlist
.r_list
[i
]->l_tls_blocksize
378 /* Now that we know the object is loaded successfully add
379 modules containing TLS data to the dtv info table. We
380 might have to increase its size. */
381 struct dtv_slotinfo_list
*listp
;
382 struct dtv_slotinfo_list
*prevp
;
383 size_t idx
= new->l_searchlist
.r_list
[i
]->l_tls_modid
;
385 assert (new->l_searchlist
.r_list
[i
]->l_type
== lt_loaded
);
387 /* Find the place in the dtv slotinfo list. */
388 listp
= GL(dl_tls_dtv_slotinfo_list
);
389 prevp
= NULL
; /* Needed to shut up gcc. */
392 /* Does it fit in the array of this list element? */
393 if (idx
< listp
->len
)
399 while (listp
!= NULL
);
403 /* When we come here it means we have to add a new element
404 to the slotinfo list. And the new module must be in
408 listp
= prevp
->next
= (struct dtv_slotinfo_list
*)
409 malloc (sizeof (struct dtv_slotinfo_list
)
410 + TLS_SLOTINFO_SURPLUS
* sizeof (struct dtv_slotinfo
));
413 /* We ran out of memory. We will simply fail this
414 call but don't undo anything we did so far. The
415 application will crash or be terminated anyway very
418 /* We have to do this since some entries in the dtv
419 slotinfo array might already point to this
421 ++GL(dl_tls_generation
);
423 _dl_signal_error (ENOMEM
, "dlopen", NULL
,
424 N_("cannot create TLS data structures"));
427 listp
->len
= TLS_SLOTINFO_SURPLUS
;
429 memset (listp
->slotinfo
, '\0',
430 TLS_SLOTINFO_SURPLUS
* sizeof (struct dtv_slotinfo
));
433 /* Add the information into the slotinfo data structure. */
434 listp
->slotinfo
[idx
].map
= new->l_searchlist
.r_list
[i
];
435 listp
->slotinfo
[idx
].gen
= GL(dl_tls_generation
) + 1;
437 /* We have to bump the generation counter. */
441 /* Bump the generation number if necessary. */
443 if (__builtin_expect (++GL(dl_tls_generation
) == 0, 0))
444 __libc_fatal (_("TLS generation counter wrapped! Please send report with the 'glibcbug' script."));
447 /* Run the initializer functions of new objects. */
448 _dl_init (new, __libc_argc
, __libc_argv
, __environ
);
450 /* Now we can make the new map available in the global scope. */
451 if (mode
& RTLD_GLOBAL
)
452 /* Move the object in the global namespace. */
453 if (add_to_global (new) != 0)
457 /* Mark the object as not deletable if the RTLD_NODELETE flags was
459 if (__builtin_expect (mode
& RTLD_NODELETE
, 0))
460 new->l_flags_1
|= DF_1_NODELETE
;
463 /* We must be the static _dl_open in libc.a. A static program that
464 has loaded a dynamic object now has competition. */
465 __libc_multiple_libcs
= 1;
468 /* Let the user know about the opencount. */
469 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_FILES
, 0))
470 _dl_debug_printf ("opening file=%s; opencount == %u\n\n",
471 new->l_name
, new->l_opencount
);
477 _dl_open (const char *file
, int mode
, const void *caller
)
479 struct dl_open_args args
;
481 const char *errstring
;
484 if ((mode
& RTLD_BINDING_MASK
) == 0)
485 /* One of the flags must be set. */
486 _dl_signal_error (EINVAL
, file
, NULL
, N_("invalid mode for dlopen()"));
488 /* Make sure we are alone. */
489 __rtld_lock_lock_recursive (GL(dl_load_lock
));
493 args
.caller
= caller
;
495 errcode
= _dl_catch_error (&objname
, &errstring
, dl_open_worker
, &args
);
498 /* We must munmap() the cache file. */
502 /* Release the lock. */
503 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
505 if (__builtin_expect (errstring
!= NULL
, 0))
507 /* Some error occurred during loading. */
508 char *local_errstring
;
509 size_t len_errstring
;
511 /* Remove the object from memory. It may be in an inconsistent
512 state if relocation failed, for example. */
517 /* Increment open counters for all objects since this
518 sometimes has not happened yet. */
519 if (args
.map
->l_searchlist
.r_list
[0]->l_opencount
== 0)
520 for (i
= 0; i
< args
.map
->l_searchlist
.r_nlist
; ++i
)
521 ++args
.map
->l_searchlist
.r_list
[i
]->l_opencount
;
524 /* Maybe some of the modules which were loaded uses TLS.
525 Since it will be removed in the following _dl_close call
526 we have to mark the dtv array as having gaps to fill
527 the holes. This is a pessimistic assumption which won't
529 GL(dl_tls_dtv_gaps
) = true;
532 _dl_close (args
.map
);
535 /* Make a local copy of the error string so that we can release the
536 memory allocated for it. */
537 len_errstring
= strlen (errstring
) + 1;
538 if (objname
== errstring
+ len_errstring
)
540 size_t total_len
= len_errstring
+ strlen (objname
) + 1;
541 local_errstring
= alloca (total_len
);
542 memcpy (local_errstring
, errstring
, total_len
);
543 objname
= local_errstring
+ len_errstring
;
547 local_errstring
= alloca (len_errstring
);
548 memcpy (local_errstring
, errstring
, len_errstring
);
551 if (errstring
!= _dl_out_of_memory
)
552 free ((char *) errstring
);
554 /* Reraise the error. */
555 _dl_signal_error (errcode
, objname
, NULL
, local_errstring
);
559 DL_STATIC_INIT (args
.map
);
564 libc_hidden_def (_dl_open
)
571 show_scope (struct link_map
*new)
575 for (scope_cnt
= 0; new->l_scope
[scope_cnt
] != NULL
; ++scope_cnt
)
580 numbuf
[0] = '0' + scope_cnt
;
582 _dl_printf ("scope %s:", numbuf
);
584 for (cnt
= 0; cnt
< new->l_scope
[scope_cnt
]->r_nlist
; ++cnt
)
585 if (*new->l_scope
[scope_cnt
]->r_list
[cnt
]->l_name
)
586 _dl_printf (" %s", new->l_scope
[scope_cnt
]->r_list
[cnt
]->l_name
);
588 _dl_printf (" <main>");