1 /* Storage management for the chain of loaded shared objects.
2 Copyright (C) 1995-2002,2004,2006,2007,2008 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
29 /* Allocate a `struct link_map' for a new object being loaded,
30 and enter it into the _dl_loaded list. */
34 _dl_new_object (char *realname
, const char *libname
, int type
,
35 struct link_map
*loader
, int mode
, Lmid_t nsid
)
39 size_t libname_len
= strlen (libname
) + 1;
41 struct libname_list
*newname
;
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
)
47 size_t audit_space
= naudit
* sizeof (new->l_audit
[0]);
49 # define audit_space 0
52 new = (struct link_map
*) calloc (sizeof (*new) + audit_space
53 + sizeof (struct link_map
*)
54 + sizeof (*newname
) + libname_len
, 1);
59 new->l_symbolic_searchlist
.r_list
= (struct link_map
**) ((char *) (new + 1)
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
;
70 new->l_loader
= loader
;
71 #if NO_TLS_OFFSET != 0
72 new->l_tls_offset
= NO_TLS_OFFSET
;
77 for (unsigned int cnt
= 0; cnt
< naudit
; ++cnt
)
79 new->l_audit
[cnt
].cookie
= (uintptr_t) new;
80 /* new->l_audit[cnt].bindflags = 0; */
84 /* new->l_global = 0; We use calloc therefore not necessary. */
86 /* Use the 'l_scope_mem' array by default for the the 'l_scope'
87 information. If we need more entries we will allocate a large
89 new->l_scope
= new->l_scope_mem
;
90 new->l_scope_max
= sizeof (new->l_scope_mem
) / sizeof (new->l_scope_mem
[0]);
92 /* Counter for the scopes we have to handle. */
95 if (GL(dl_ns
)[nsid
]._ns_loaded
!= NULL
)
97 l
= GL(dl_ns
)[nsid
]._ns_loaded
;
98 while (l
->l_next
!= NULL
)
101 /* new->l_next = NULL; Would be necessary but we use calloc. */
104 /* Add the global scope. */
105 new->l_scope
[idx
++] = &GL(dl_ns
)[nsid
]._ns_loaded
->l_searchlist
;
108 GL(dl_ns
)[nsid
]._ns_loaded
= new;
109 ++GL(dl_ns
)[nsid
]._ns_nloaded
;
110 new->l_serial
= GL(dl_load_adds
);
113 /* If we have no loader the new object acts as it. */
117 /* Determine the local scope. */
118 while (loader
->l_loader
!= NULL
)
119 loader
= loader
->l_loader
;
121 /* Insert the scope if it isn't the global scope we already added. */
122 if (idx
== 0 || &loader
->l_searchlist
!= new->l_scope
[0])
124 if ((mode
& RTLD_DEEPBIND
) != 0 && idx
!= 0)
126 new->l_scope
[1] = new->l_scope
[0];
130 new->l_scope
[idx
] = &loader
->l_searchlist
;
133 new->l_local_scope
[0] = &new->l_searchlist
;
135 /* Don't try to find the origin for the main map which has the name "". */
136 if (realname
[0] != '\0')
138 size_t realname_len
= strlen (realname
) + 1;
142 if (realname
[0] == '/')
144 /* It is an absolute path. Use it. But we have to make a
145 copy since we strip out the trailing slash. */
146 cp
= origin
= (char *) malloc (realname_len
);
149 origin
= (char *) -1;
155 size_t len
= realname_len
;
158 /* Get the current directory name. */
165 new_origin
= (char *) realloc (origin
, len
);
166 if (new_origin
== NULL
)
167 /* We exit the loop. Note that result == NULL. */
171 while ((result
= __getcwd (origin
, len
- realname_len
)) == NULL
176 /* We were not able to determine the current directory.
177 Note that free(origin) is OK if origin == NULL. */
179 origin
= (char *) -1;
183 /* Find the end of the path and see whether we have to add a
184 slash. We could use rawmemchr but this need not be
186 cp
= (strchr
) (origin
, '\0');
191 /* Add the real file name. */
192 cp
= __mempcpy (cp
, realname
, realname_len
);
194 /* Now remove the filename and the slash. Leave the slash if
195 the name is something like "/foo". */
201 /* Keep the only slash which is the first character. */
206 new->l_origin
= origin
;