Update.
[platform/upstream/glibc.git] / elf / dl-support.c
1 /* Support for dynamic linking code in static libc.
2    Copyright (C) 1996, 97, 98, 99, 2000, 2001 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
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    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 /* This file defines some things that for the dynamic linker are defined in
21    rtld.c and dl-sysdep.c in ways appropriate to bootstrap dynamic linking.  */
22
23 #include <errno.h>
24 #include <libintl.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <ldsodefs.h>
28 #include <dl-machine.h>
29 #include <bits/libc-lock.h>
30 #include <dl-librecon.h>
31 #include <unsecvars.h>
32 #include <cpuclock-init.h>
33
34 extern char *__progname;
35 char **_dl_argv = &__progname;  /* This is checked for some error messages.  */
36
37 /* Name of the architecture.  */
38 const char *_dl_platform;
39 size_t _dl_platformlen;
40
41 int _dl_debug_mask;
42 int _dl_lazy;
43 /* XXX I know about at least one case where we depend on the old weak
44    behavior (it has to do with librt).  Until we get DSO groups implemented
45    we have to make this the default.  Bummer. --drepper  */
46 #if 0
47 int _dl_dynamic_weak;
48 #else
49 int _dl_dynamic_weak = 1;
50 #endif
51
52 /* If nonzero print warnings about problematic situations.  */
53 int _dl_verbose;
54
55 /* Structure to store information about search paths.  */
56 struct r_search_path *_dl_search_paths;
57
58 /* We never do profiling.  */
59 const char *_dl_profile;
60
61 /* Names of shared object for which the RUNPATHs and RPATHs should be
62    ignored.  */
63 const char *_dl_inhibit_rpath;
64
65 /* The map for the object we will profile.  */
66 struct link_map *_dl_profile_map;
67
68 /* This is the address of the last stack address ever used.  */
69 void *__libc_stack_end;
70
71 /* Path where the binary is found.  */
72 const char *_dl_origin_path;
73
74 /* Nonzero if runtime lookup should not update the .got/.plt.  */
75 int _dl_bind_not;
76
77 /* Initially empty list of loaded objects.  */
78 struct link_map *_dl_loaded;
79 /* Number of object in the _dl_loaded list.  */
80 unsigned int _dl_nloaded;
81
82 /* Fake scope.  In dynamically linked binaries this is the scope of the
83    main application but here we don't have something like this.  So
84    create a fake scope containing nothing.  */
85 struct r_scope_elem _dl_initial_searchlist;
86 /* Variable which can be used in lookup to process the global scope.  */
87 struct r_scope_elem *_dl_global_scope[2] = { &_dl_initial_searchlist, NULL };
88 /* This is a global pointer to this structure which is public.  It is
89    used by dlopen/dlclose to add and remove objects from what is regarded
90    to be the global scope.  */
91 struct r_scope_elem *_dl_main_searchlist = &_dl_initial_searchlist;
92
93 /* Nonzero during startup.  */
94 int _dl_starting_up = 1;
95
96 /* Initial value of the CPU clock.  */
97 #ifdef CPUCLOCK_VARDEF
98 CPUCLOCK_VARDEF (_dl_cpuclock_offset);
99 #endif
100
101 /* During the program run we must not modify the global data of
102    loaded shared object simultanously in two threads.  Therefore we
103    protect `_dl_open' and `_dl_close' in dl-close.c.
104
105    This must be a recursive lock since the initializer function of
106    the loaded object might as well require a call to this function.
107    At this time it is not anymore a problem to modify the tables.  */
108 __libc_lock_define_initialized_recursive (, _dl_load_lock)
109
110
111 #ifdef HAVE_AUX_VECTOR
112 extern int _dl_clktck;
113
114 void
115 internal_function
116 _dl_aux_init (ElfW(auxv_t) *av)
117 {
118   for (; av->a_type != AT_NULL; ++av)
119     switch (av->a_type)
120       {
121       case AT_PAGESZ:
122         _dl_pagesize = av->a_un.a_val;
123         break;
124       case AT_CLKTCK:
125         _dl_clktck = av->a_un.a_val;
126         break;
127       }
128 }
129 #endif
130
131 static void non_dynamic_init (void) __attribute__ ((unused));
132
133 static void
134 non_dynamic_init (void)
135 {
136 #ifdef CPUCLOCK_INIT
137   CPUCLOCK_INIT (_dl_cpuclock_offset);
138 #endif
139
140   if (!_dl_pagesize)
141     _dl_pagesize = __getpagesize ();
142
143   _dl_verbose = *(getenv ("LD_WARN") ?: "") == '\0' ? 0 : 1;
144
145   /* Initialize the data structures for the search paths for shared
146      objects.  */
147   _dl_init_paths (getenv ("LD_LIBRARY_PATH"));
148
149   _dl_lazy = *(getenv ("LD_BIND_NOW") ?: "") == '\0';
150
151   _dl_bind_not = *(getenv ("LD_BIND_NOT") ?: "") != '\0';
152
153   _dl_dynamic_weak = *(getenv ("LD_DYNAMIC_WEAK") ?: "") == '\0';
154
155   if (__libc_enable_secure)
156     {
157       static const char *unsecure_envvars[] =
158       {
159         UNSECURE_ENVVARS,
160 #ifdef EXTRA_UNSECURE_ENVVARS
161         EXTRA_UNSECURE_ENVVARS
162 #endif
163       };
164       size_t cnt;
165
166       for (cnt = 0;
167            cnt < sizeof (unsecure_envvars) / sizeof (unsecure_envvars[0]);
168            ++cnt)
169         unsetenv (unsecure_envvars[cnt]);
170
171       if (__access ("/etc/suid-debug", F_OK) != 0)
172         unsetenv ("MALLOC_CHECK_");
173     }
174
175 #ifdef DL_PLATFORM_INIT
176   DL_PLATFORM_INIT;
177 #endif
178
179   /* Now determine the length of the platform string.  */
180   if (_dl_platform != NULL)
181     _dl_platformlen = strlen (_dl_platform);
182 }
183 text_set_element (__libc_subinit, non_dynamic_init);
184
185 const struct r_strlenpair *
186 internal_function
187 _dl_important_hwcaps (const char *platform, size_t platform_len, size_t *sz,
188                       size_t *max_capstrlen)
189 {
190   struct r_strlenpair *result;
191
192   /* XXX We don't try to find the capabilities in this case.  */
193   result = (struct r_strlenpair *) malloc (sizeof (*result));
194   if (result == NULL)
195     _dl_signal_error (ENOMEM, NULL, N_("cannot create capability list"));
196
197   result[0].str = (char *) result;      /* Does not really matter.  */
198   result[0].len = 0;
199
200   *sz = 1;
201   return result;
202 }