Use IFUNC on x86-64 memset
[glibc.git] / elf / dl-object.c
blob5d15ce1869b6305f828b409e9edeebf448b50641
1 /* Storage management for the chain of loaded shared objects.
2 Copyright (C) 1995-2002,2004,2006-2009,2010 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
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <ldsodefs.h>
26 #include <assert.h>
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)
34 /* We modify the list of loaded objects. */
35 __rtld_lock_lock_recursive (GL(dl_load_write_lock));
37 if (GL(dl_ns)[nsid]._ns_loaded != NULL)
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;
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);
52 __rtld_lock_unlock_recursive (GL(dl_load_write_lock));
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)
63 struct link_map *l;
64 size_t libname_len = strlen (libname) + 1;
65 struct link_map *new;
66 struct libname_list *newname;
67 #ifdef SHARED
68 /* We create the map for the executable before we know whether we have
69 auditing libraries and if yes, how many. Assume the worst. */
70 unsigned int naudit = GLRO(dl_naudit) ?: ((mode & __RTLD_OPENEXEC)
71 ? DL_NNS : 0);
72 size_t audit_space = naudit * sizeof (new->l_audit[0]);
73 #else
74 # define audit_space 0
75 #endif
77 new = (struct link_map *) calloc (sizeof (*new) + audit_space
78 + sizeof (struct link_map *)
79 + sizeof (*newname) + libname_len, 1);
80 if (new == NULL)
81 return NULL;
83 new->l_real = new;
84 new->l_symbolic_searchlist.r_list = (struct link_map **) ((char *) (new + 1)
85 + audit_space);
87 new->l_libname = newname
88 = (struct libname_list *) (new->l_symbolic_searchlist.r_list + 1);
89 newname->name = (char *) memcpy (newname + 1, libname, libname_len);
90 /* newname->next = NULL; We use calloc therefore not necessary. */
91 newname->dont_free = 1;
93 new->l_name = realname;
94 new->l_type = type;
95 /* If we set the bit now since we know it is never used we avoid
96 dirtying the cache line later. */
97 if ((GLRO(dl_debug_mask) & DL_DEBUG_UNUSED) == 0)
98 new->l_used = 1;
99 new->l_loader = loader;
100 #if NO_TLS_OFFSET != 0
101 new->l_tls_offset = NO_TLS_OFFSET;
102 #endif
103 new->l_ns = nsid;
105 #ifdef SHARED
106 for (unsigned int cnt = 0; cnt < naudit; ++cnt)
108 new->l_audit[cnt].cookie = (uintptr_t) new;
109 /* new->l_audit[cnt].bindflags = 0; */
111 #endif
113 /* new->l_global = 0; We use calloc therefore not necessary. */
115 /* Use the 'l_scope_mem' array by default for the the 'l_scope'
116 information. If we need more entries we will allocate a large
117 array dynamically. */
118 new->l_scope = new->l_scope_mem;
119 new->l_scope_max = sizeof (new->l_scope_mem) / sizeof (new->l_scope_mem[0]);
121 /* Counter for the scopes we have to handle. */
122 int idx = 0;
124 if (GL(dl_ns)[nsid]._ns_loaded != NULL)
125 /* Add the global scope. */
126 new->l_scope[idx++] = &GL(dl_ns)[nsid]._ns_loaded->l_searchlist;
128 /* If we have no loader the new object acts as it. */
129 if (loader == NULL)
130 loader = new;
131 else
132 /* Determine the local scope. */
133 while (loader->l_loader != NULL)
134 loader = loader->l_loader;
136 /* Insert the scope if it isn't the global scope we already added. */
137 if (idx == 0 || &loader->l_searchlist != new->l_scope[0])
139 if ((mode & RTLD_DEEPBIND) != 0 && idx != 0)
141 new->l_scope[1] = new->l_scope[0];
142 idx = 0;
145 new->l_scope[idx] = &loader->l_searchlist;
148 new->l_local_scope[0] = &new->l_searchlist;
150 /* Don't try to find the origin for the main map which has the name "". */
151 if (realname[0] != '\0')
153 size_t realname_len = strlen (realname) + 1;
154 char *origin;
155 char *cp;
157 if (realname[0] == '/')
159 /* It is an absolute path. Use it. But we have to make a
160 copy since we strip out the trailing slash. */
161 cp = origin = (char *) malloc (realname_len);
162 if (origin == NULL)
164 origin = (char *) -1;
165 goto out;
168 else
170 size_t len = realname_len;
171 char *result = NULL;
173 /* Get the current directory name. */
174 origin = NULL;
177 char *new_origin;
179 len += 128;
180 new_origin = (char *) realloc (origin, len);
181 if (new_origin == NULL)
182 /* We exit the loop. Note that result == NULL. */
183 break;
184 origin = new_origin;
186 while ((result = __getcwd (origin, len - realname_len)) == NULL
187 && errno == ERANGE);
189 if (result == NULL)
191 /* We were not able to determine the current directory.
192 Note that free(origin) is OK if origin == NULL. */
193 free (origin);
194 origin = (char *) -1;
195 goto out;
198 /* Find the end of the path and see whether we have to add a
199 slash. We could use rawmemchr but this need not be
200 fast. */
201 cp = (strchr) (origin, '\0');
202 if (cp[-1] != '/')
203 *cp++ = '/';
206 /* Add the real file name. */
207 cp = __mempcpy (cp, realname, realname_len);
209 /* Now remove the filename and the slash. Leave the slash if
210 the name is something like "/foo". */
212 --cp;
213 while (*cp != '/');
215 if (cp == origin)
216 /* Keep the only slash which is the first character. */
217 ++cp;
218 *cp = '\0';
220 out:
221 new->l_origin = origin;
224 return new;