Fix alignment of AVX safe area on x86-64.
[glibc.git] / elf / dl-object.c
blob22a163560b48b3fb6b3a12112f470e550497908f
1 /* Storage management for the chain of loaded shared objects.
2 Copyright (C) 1995-2002,2004,2006-2008,2009 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 /* Allocate a `struct link_map' for a new object being loaded,
30 and enter it into the _dl_loaded list. */
32 struct link_map *
33 internal_function
34 _dl_new_object (char *realname, const char *libname, int type,
35 struct link_map *loader, int mode, Lmid_t nsid)
37 struct link_map *l;
38 int idx;
39 size_t libname_len = strlen (libname) + 1;
40 struct link_map *new;
41 struct libname_list *newname;
42 #ifdef SHARED
43 /* We create the map for the executable before we know whether we have
44 auditing libraries and if yes, how many. Assume the worst. */
45 unsigned int naudit = GLRO(dl_naudit) ?: ((mode & __RTLD_OPENEXEC)
46 ? DL_NNS : 0);
47 size_t audit_space = naudit * sizeof (new->l_audit[0]);
48 #else
49 # define audit_space 0
50 #endif
52 new = (struct link_map *) calloc (sizeof (*new) + audit_space
53 + sizeof (struct link_map *)
54 + sizeof (*newname) + libname_len, 1);
55 if (new == NULL)
56 return NULL;
58 new->l_real = new;
59 new->l_symbolic_searchlist.r_list = (struct link_map **) ((char *) (new + 1)
60 + audit_space);
62 new->l_libname = newname
63 = (struct libname_list *) (new->l_symbolic_searchlist.r_list + 1);
64 newname->name = (char *) memcpy (newname + 1, libname, libname_len);
65 /* newname->next = NULL; We use calloc therefore not necessary. */
66 newname->dont_free = 1;
68 new->l_name = realname;
69 new->l_type = type;
70 /* If we set the bit now since we know it is never used we avoid
71 dirtying the cache line later. */
72 if ((GLRO(dl_debug_mask) & DL_DEBUG_UNUSED) == 0)
73 new->l_used = 1;
74 new->l_loader = loader;
75 #if NO_TLS_OFFSET != 0
76 new->l_tls_offset = NO_TLS_OFFSET;
77 #endif
78 new->l_ns = nsid;
80 #ifdef SHARED
81 for (unsigned int cnt = 0; cnt < naudit; ++cnt)
83 new->l_audit[cnt].cookie = (uintptr_t) new;
84 /* new->l_audit[cnt].bindflags = 0; */
86 #endif
88 /* new->l_global = 0; We use calloc therefore not necessary. */
90 /* Use the 'l_scope_mem' array by default for the the 'l_scope'
91 information. If we need more entries we will allocate a large
92 array dynamically. */
93 new->l_scope = new->l_scope_mem;
94 new->l_scope_max = sizeof (new->l_scope_mem) / sizeof (new->l_scope_mem[0]);
96 /* We modify the list of loaded objects. */
97 __rtld_lock_lock_recursive (GL(dl_load_write_lock));
99 /* Counter for the scopes we have to handle. */
100 idx = 0;
102 if (GL(dl_ns)[nsid]._ns_loaded != NULL)
104 l = GL(dl_ns)[nsid]._ns_loaded;
105 while (l->l_next != NULL)
106 l = l->l_next;
107 new->l_prev = l;
108 /* new->l_next = NULL; Would be necessary but we use calloc. */
109 l->l_next = new;
111 /* Add the global scope. */
112 new->l_scope[idx++] = &GL(dl_ns)[nsid]._ns_loaded->l_searchlist;
114 else
115 GL(dl_ns)[nsid]._ns_loaded = new;
116 ++GL(dl_ns)[nsid]._ns_nloaded;
117 new->l_serial = GL(dl_load_adds);
118 ++GL(dl_load_adds);
120 __rtld_lock_unlock_recursive (GL(dl_load_write_lock));
122 /* If we have no loader the new object acts as it. */
123 if (loader == NULL)
124 loader = new;
125 else
126 /* Determine the local scope. */
127 while (loader->l_loader != NULL)
128 loader = loader->l_loader;
130 /* Insert the scope if it isn't the global scope we already added. */
131 if (idx == 0 || &loader->l_searchlist != new->l_scope[0])
133 if ((mode & RTLD_DEEPBIND) != 0 && idx != 0)
135 new->l_scope[1] = new->l_scope[0];
136 idx = 0;
139 new->l_scope[idx] = &loader->l_searchlist;
142 new->l_local_scope[0] = &new->l_searchlist;
144 /* Don't try to find the origin for the main map which has the name "". */
145 if (realname[0] != '\0')
147 size_t realname_len = strlen (realname) + 1;
148 char *origin;
149 char *cp;
151 if (realname[0] == '/')
153 /* It is an absolute path. Use it. But we have to make a
154 copy since we strip out the trailing slash. */
155 cp = origin = (char *) malloc (realname_len);
156 if (origin == NULL)
158 origin = (char *) -1;
159 goto out;
162 else
164 size_t len = realname_len;
165 char *result = NULL;
167 /* Get the current directory name. */
168 origin = NULL;
171 char *new_origin;
173 len += 128;
174 new_origin = (char *) realloc (origin, len);
175 if (new_origin == NULL)
176 /* We exit the loop. Note that result == NULL. */
177 break;
178 origin = new_origin;
180 while ((result = __getcwd (origin, len - realname_len)) == NULL
181 && errno == ERANGE);
183 if (result == NULL)
185 /* We were not able to determine the current directory.
186 Note that free(origin) is OK if origin == NULL. */
187 free (origin);
188 origin = (char *) -1;
189 goto out;
192 /* Find the end of the path and see whether we have to add a
193 slash. We could use rawmemchr but this need not be
194 fast. */
195 cp = (strchr) (origin, '\0');
196 if (cp[-1] != '/')
197 *cp++ = '/';
200 /* Add the real file name. */
201 cp = __mempcpy (cp, realname, realname_len);
203 /* Now remove the filename and the slash. Leave the slash if
204 the name is something like "/foo". */
206 --cp;
207 while (*cp != '/');
209 if (cp == origin)
210 /* Keep the only slash which is the first character. */
211 ++cp;
212 *cp = '\0';
214 out:
215 new->l_origin = origin;
218 return new;