1 /* Storage management for the chain of loaded shared objects.
2 Copyright (C) 1995-2015 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, see
17 <http://www.gnu.org/licenses/>. */
28 /* Add the new link_map NEW to the end of the namespace list. */
31 _dl_add_to_namespace_list (struct link_map
*new, Lmid_t nsid
)
33 /* We modify the list of loaded objects. */
34 __rtld_lock_lock_recursive (GL(dl_load_write_lock
));
36 if (GL(dl_ns
)[nsid
]._ns_loaded
!= NULL
)
38 struct link_map
*l
= GL(dl_ns
)[nsid
]._ns_loaded
;
39 while (l
->l_next
!= NULL
)
42 /* new->l_next = NULL; Would be necessary but we use calloc. */
46 GL(dl_ns
)[nsid
]._ns_loaded
= new;
47 ++GL(dl_ns
)[nsid
]._ns_nloaded
;
48 new->l_serial
= GL(dl_load_adds
);
51 __rtld_lock_unlock_recursive (GL(dl_load_write_lock
));
55 /* Allocate a `struct link_map' for a new object being loaded,
56 and enter it into the _dl_loaded list. */
59 _dl_new_object (char *realname
, const char *libname
, int type
,
60 struct link_map
*loader
, int mode
, Lmid_t nsid
)
62 size_t libname_len
= strlen (libname
) + 1;
64 struct libname_list
*newname
;
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
)
70 size_t audit_space
= naudit
* sizeof (new->l_audit
[0]);
72 # define audit_space 0
75 new = (struct link_map
*) calloc (sizeof (*new) + audit_space
76 + sizeof (struct link_map
*)
77 + sizeof (*newname
) + libname_len
, 1);
82 new->l_symbolic_searchlist
.r_list
= (struct link_map
**) ((char *) (new + 1)
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;
91 /* When we create the executable link map, or a VDSO link map, we start
92 with "" for the l_name. In these cases "" points to ld.so rodata
93 and won't get dumped during core file generation. Therefore to assist
94 gdb and to create more self-contained core files we adjust l_name to
95 point at the newly allocated copy (which will get dumped) instead of
96 the ld.so rodata copy. */
97 new->l_name
= *realname
? realname
: (char *) newname
->name
+ libname_len
- 1;
99 /* If we set the bit now since we know it is never used we avoid
100 dirtying the cache line later. */
101 if ((GLRO(dl_debug_mask
) & DL_DEBUG_UNUSED
) == 0)
103 new->l_loader
= loader
;
104 #if NO_TLS_OFFSET != 0
105 new->l_tls_offset
= NO_TLS_OFFSET
;
110 for (unsigned int cnt
= 0; cnt
< naudit
; ++cnt
)
112 new->l_audit
[cnt
].cookie
= (uintptr_t) new;
113 /* new->l_audit[cnt].bindflags = 0; */
117 /* new->l_global = 0; We use calloc therefore not necessary. */
119 /* Use the 'l_scope_mem' array by default for the 'l_scope'
120 information. If we need more entries we will allocate a large
121 array dynamically. */
122 new->l_scope
= new->l_scope_mem
;
123 new->l_scope_max
= sizeof (new->l_scope_mem
) / sizeof (new->l_scope_mem
[0]);
125 /* Counter for the scopes we have to handle. */
128 if (GL(dl_ns
)[nsid
]._ns_loaded
!= NULL
)
129 /* Add the global scope. */
130 new->l_scope
[idx
++] = &GL(dl_ns
)[nsid
]._ns_loaded
->l_searchlist
;
132 /* If we have no loader the new object acts as it. */
136 /* Determine the local scope. */
137 while (loader
->l_loader
!= NULL
)
138 loader
= loader
->l_loader
;
140 /* Insert the scope if it isn't the global scope we already added. */
141 if (idx
== 0 || &loader
->l_searchlist
!= new->l_scope
[0])
143 if ((mode
& RTLD_DEEPBIND
) != 0 && idx
!= 0)
145 new->l_scope
[1] = new->l_scope
[0];
149 new->l_scope
[idx
] = &loader
->l_searchlist
;
152 new->l_local_scope
[0] = &new->l_searchlist
;
154 /* Don't try to find the origin for the main map which has the name "". */
155 if (realname
[0] != '\0')
157 size_t realname_len
= strlen (realname
) + 1;
161 if (realname
[0] == '/')
163 /* It is an absolute path. Use it. But we have to make a
164 copy since we strip out the trailing slash. */
165 cp
= origin
= (char *) malloc (realname_len
);
168 origin
= (char *) -1;
174 size_t len
= realname_len
;
177 /* Get the current directory name. */
184 new_origin
= (char *) realloc (origin
, len
);
185 if (new_origin
== NULL
)
186 /* We exit the loop. Note that result == NULL. */
190 while ((result
= __getcwd (origin
, len
- realname_len
)) == NULL
195 /* We were not able to determine the current directory.
196 Note that free(origin) is OK if origin == NULL. */
198 origin
= (char *) -1;
202 /* Find the end of the path and see whether we have to add a
203 slash. We could use rawmemchr but this need not be
205 cp
= (strchr
) (origin
, '\0');
210 /* Add the real file name. */
211 cp
= __mempcpy (cp
, realname
, realname_len
);
213 /* Now remove the filename and the slash. Leave the slash if
214 the name is something like "/foo". */
220 /* Keep the only slash which is the first character. */
225 new->l_origin
= origin
;