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.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30 /* Add the new link_map NEW to the end of the namespace list. */
33 _dl_add_to_namespace_list (struct link_map
*new, Lmid_t nsid
)
35 /* We modify the list of loaded objects. */
36 __rtld_lock_lock_recursive (GL(dl_load_write_lock
));
38 if (GL(dl_ns
)[nsid
]._ns_loaded
!= NULL
)
40 struct link_map
*l
= GL(dl_ns
)[nsid
]._ns_loaded
;
41 while (l
->l_next
!= NULL
)
44 /* new->l_next = NULL; Would be necessary but we use calloc. */
48 GL(dl_ns
)[nsid
]._ns_loaded
= new;
49 ++GL(dl_ns
)[nsid
]._ns_nloaded
;
50 new->l_serial
= GL(dl_load_adds
);
53 __rtld_lock_unlock_recursive (GL(dl_load_write_lock
));
57 /* Allocate a `struct link_map' for a new object being loaded,
58 and enter it into the _dl_loaded list. */
61 _dl_new_object (char *realname
, const char *libname
, int type
,
62 struct link_map
*loader
, int mode
, Lmid_t nsid
)
64 size_t libname_len
= strlen (libname
) + 1;
66 struct libname_list
*newname
;
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
)
72 size_t audit_space
= naudit
* sizeof (new->l_audit
[0]);
74 # define audit_space 0
77 new = (struct link_map
*) calloc (sizeof (*new) + audit_space
78 + sizeof (struct link_map
*)
79 + sizeof (*newname
) + libname_len
, 1);
84 new->l_symbolic_searchlist
.r_list
= (struct link_map
**) ((char *) (new + 1)
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
;
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)
99 new->l_loader
= loader
;
100 #if NO_TLS_OFFSET != 0
101 new->l_tls_offset
= NO_TLS_OFFSET
;
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; */
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. */
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. */
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];
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;
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
);
164 origin
= (char *) -1;
170 size_t len
= realname_len
;
173 /* Get the current directory name. */
180 new_origin
= (char *) realloc (origin
, len
);
181 if (new_origin
== NULL
)
182 /* We exit the loop. Note that result == NULL. */
186 while ((result
= __getcwd (origin
, len
- realname_len
)) == NULL
191 /* We were not able to determine the current directory.
192 Note that free(origin) is OK if origin == NULL. */
194 origin
= (char *) -1;
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
201 cp
= (strchr
) (origin
, '\0');
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". */
216 /* Keep the only slash which is the first character. */
221 new->l_origin
= origin
;