Mention IFUNC enhancements to testsuite in NEWS.
[platform/upstream/glibc.git] / elf / dl-object.c
1 /* Storage management for the chain of loaded shared objects.
2    Copyright (C) 1995-2002,2004,2006,2007,2008,2009,2010,2011
3         Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include <errno.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <ldsodefs.h>
25
26 #include <assert.h>
27
28
29 /* Add the new link_map NEW to the end of the namespace list.  */
30 void
31 internal_function
32 _dl_add_to_namespace_list (struct link_map *new, Lmid_t nsid)
33 {
34   /* We modify the list of loaded objects.  */
35   __rtld_lock_lock_recursive (GL(dl_load_write_lock));
36
37   if (GL(dl_ns)[nsid]._ns_loaded != NULL)
38     {
39       struct link_map *l = GL(dl_ns)[nsid]._ns_loaded;
40       while (l->l_next != NULL)
41         l = l->l_next;
42       new->l_prev = l;
43       /* new->l_next = NULL;   Would be necessary but we use calloc.  */
44       l->l_next = new;
45     }
46   else
47     GL(dl_ns)[nsid]._ns_loaded = new;
48   ++GL(dl_ns)[nsid]._ns_nloaded;
49   new->l_serial = GL(dl_load_adds);
50   ++GL(dl_load_adds);
51
52   __rtld_lock_unlock_recursive (GL(dl_load_write_lock));
53 }
54
55
56 /* Allocate a `struct link_map' for a new object being loaded,
57    and enter it into the _dl_loaded list.  */
58 struct link_map *
59 internal_function
60 _dl_new_object (char *realname, const char *libname, int type,
61                 struct link_map *loader, int mode, Lmid_t nsid)
62 {
63   size_t libname_len = strlen (libname) + 1;
64   struct link_map *new;
65   struct libname_list *newname;
66 #ifdef SHARED
67   /* We create the map for the executable before we know whether we have
68      auditing libraries and if yes, how many.  Assume the worst.  */
69   unsigned int naudit = GLRO(dl_naudit) ?: ((mode & __RTLD_OPENEXEC)
70                                             ? DL_NNS : 0);
71   size_t audit_space = naudit * sizeof (new->l_audit[0]);
72 #else
73 # define audit_space 0
74 #endif
75
76   new = (struct link_map *) calloc (sizeof (*new) + audit_space
77                                     + sizeof (struct link_map *)
78                                     + sizeof (*newname) + libname_len, 1);
79   if (new == NULL)
80     return NULL;
81
82   new->l_real = new;
83   new->l_symbolic_searchlist.r_list = (struct link_map **) ((char *) (new + 1)
84                                                             + audit_space);
85
86   new->l_libname = newname
87     = (struct libname_list *) (new->l_symbolic_searchlist.r_list + 1);
88   newname->name = (char *) memcpy (newname + 1, libname, libname_len);
89   /* newname->next = NULL;      We use calloc therefore not necessary.  */
90   newname->dont_free = 1;
91
92   new->l_name = realname;
93   new->l_type = type;
94   /* If we set the bit now since we know it is never used we avoid
95      dirtying the cache line later.  */
96   if ((GLRO(dl_debug_mask) & DL_DEBUG_UNUSED) == 0)
97     new->l_used = 1;
98   new->l_loader = loader;
99 #if NO_TLS_OFFSET != 0
100   new->l_tls_offset = NO_TLS_OFFSET;
101 #endif
102   new->l_ns = nsid;
103
104 #ifdef SHARED
105   for (unsigned int cnt = 0; cnt < naudit; ++cnt)
106     {
107       new->l_audit[cnt].cookie = (uintptr_t) new;
108       /* new->l_audit[cnt].bindflags = 0; */
109     }
110 #endif
111
112   /* new->l_global = 0; We use calloc therefore not necessary.  */
113
114   /* Use the 'l_scope_mem' array by default for the 'l_scope'
115      information.  If we need more entries we will allocate a large
116      array dynamically.  */
117   new->l_scope = new->l_scope_mem;
118   new->l_scope_max = sizeof (new->l_scope_mem) / sizeof (new->l_scope_mem[0]);
119
120   /* Counter for the scopes we have to handle.  */
121   int idx = 0;
122
123   if (GL(dl_ns)[nsid]._ns_loaded != NULL)
124     /* Add the global scope.  */
125     new->l_scope[idx++] = &GL(dl_ns)[nsid]._ns_loaded->l_searchlist;
126
127   /* If we have no loader the new object acts as it.  */
128   if (loader == NULL)
129     loader = new;
130   else
131     /* Determine the local scope.  */
132     while (loader->l_loader != NULL)
133       loader = loader->l_loader;
134
135   /* Insert the scope if it isn't the global scope we already added.  */
136   if (idx == 0 || &loader->l_searchlist != new->l_scope[0])
137     {
138       if ((mode & RTLD_DEEPBIND) != 0 && idx != 0)
139         {
140           new->l_scope[1] = new->l_scope[0];
141           idx = 0;
142         }
143
144       new->l_scope[idx] = &loader->l_searchlist;
145     }
146
147   new->l_local_scope[0] = &new->l_searchlist;
148
149   /* Don't try to find the origin for the main map which has the name "".  */
150   if (realname[0] != '\0')
151     {
152       size_t realname_len = strlen (realname) + 1;
153       char *origin;
154       char *cp;
155
156       if (realname[0] == '/')
157         {
158           /* It is an absolute path.  Use it.  But we have to make a
159              copy since we strip out the trailing slash.  */
160           cp = origin = (char *) malloc (realname_len);
161           if (origin == NULL)
162             {
163               origin = (char *) -1;
164               goto out;
165             }
166         }
167       else
168         {
169           size_t len = realname_len;
170           char *result = NULL;
171
172           /* Get the current directory name.  */
173           origin = NULL;
174           do
175             {
176               char *new_origin;
177
178               len += 128;
179               new_origin = (char *) realloc (origin, len);
180               if (new_origin == NULL)
181                 /* We exit the loop.  Note that result == NULL.  */
182                 break;
183               origin = new_origin;
184             }
185           while ((result = __getcwd (origin, len - realname_len)) == NULL
186                  && errno == ERANGE);
187
188           if (result == NULL)
189             {
190               /* We were not able to determine the current directory.
191                  Note that free(origin) is OK if origin == NULL.  */
192               free (origin);
193               origin = (char *) -1;
194               goto out;
195             }
196
197           /* Find the end of the path and see whether we have to add a
198              slash.  We could use rawmemchr but this need not be
199              fast.  */
200           cp = (strchr) (origin, '\0');
201           if (cp[-1] != '/')
202             *cp++ = '/';
203         }
204
205       /* Add the real file name.  */
206       cp = __mempcpy (cp, realname, realname_len);
207
208       /* Now remove the filename and the slash.  Leave the slash if
209          the name is something like "/foo".  */
210       do
211         --cp;
212       while (*cp != '/');
213
214       if (cp == origin)
215         /* Keep the only slash which is the first character.  */
216         ++cp;
217       *cp = '\0';
218
219     out:
220       new->l_origin = origin;
221     }
222
223   return new;
224 }