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