1 /* Storage management for the chain of loaded shared objects.
2 Copyright (C) 1995-2002, 2004 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 new = (struct link_map
*) calloc (sizeof (*new) + sizeof (*newname
)
49 new->l_libname
= newname
= (struct libname_list
*) (new + 1);
50 newname
->name
= (char *) memcpy (newname
+ 1, libname
, libname_len
);
51 /* newname->next = NULL; We use calloc therefore not necessary. */
52 newname
->dont_free
= 1;
54 new->l_name
= realname
;
56 new->l_loader
= loader
;
57 #if defined USE_TLS && NO_TLS_OFFSET != 0
58 new->l_tls_offset
= NO_TLS_OFFSET
;
62 /* new->l_global = 0; We use calloc therefore not necessary. */
64 /* Use the 'l_scope_mem' array by default for the the 'l_scope'
65 information. If we need more entries we will allocate a large
67 new->l_scope
= new->l_scope_mem
;
68 new->l_scope_max
= sizeof (new->l_scope_mem
) / sizeof (new->l_scope_mem
[0]);
70 /* Counter for the scopes we have to handle. */
73 if (GL(dl_ns
)[nsid
]._ns_loaded
!= NULL
)
75 l
= GL(dl_ns
)[nsid
]._ns_loaded
;
76 while (l
->l_next
!= NULL
)
79 /* new->l_next = NULL; Would be necessary but we use calloc. */
82 /* Add the global scope. */
83 new->l_scope
[idx
++] = &GL(dl_ns
)[nsid
]._ns_loaded
->l_searchlist
;
86 GL(dl_ns
)[nsid
]._ns_loaded
= new;
87 ++GL(dl_ns
)[nsid
]._ns_nloaded
;
90 /* If we have no loader the new object acts as it. */
94 /* Determine the local scope. */
95 while (loader
->l_loader
!= NULL
)
96 loader
= loader
->l_loader
;
98 /* Insert the scope if it isn't the global scope we already added. */
99 if (idx
== 0 || &loader
->l_searchlist
!= new->l_scope
[0])
101 if ((mode
& RTLD_DEEPBIND
) != 0 && idx
!= 0)
103 new->l_scope
[1] = new->l_scope
[0];
107 new->l_scope
[idx
] = &loader
->l_searchlist
;
110 new->l_local_scope
[0] = &new->l_searchlist
;
112 /* Don't try to find the origin for the main map which has the name "". */
113 if (realname
[0] != '\0')
115 size_t realname_len
= strlen (realname
) + 1;
119 if (realname
[0] == '/')
121 /* It is an absolute path. Use it. But we have to make a
122 copy since we strip out the trailing slash. */
123 cp
= origin
= (char *) malloc (realname_len
);
126 origin
= (char *) -1;
132 size_t len
= realname_len
;
135 /* Get the current directory name. */
142 new_origin
= (char *) realloc (origin
, len
);
143 if (new_origin
== NULL
)
144 /* We exit the loop. Note that result == NULL. */
148 while ((result
= __getcwd (origin
, len
- realname_len
)) == NULL
153 /* We were not able to determine the current directory.
154 Note that free(origin) is OK if origin == NULL. */
156 origin
= (char *) -1;
160 /* Find the end of the path and see whether we have to add a
161 slash. We could use rawmemchr but this need not be
163 cp
= (strchr
) (origin
, '\0');
168 /* Add the real file name. */
169 cp
= __mempcpy (cp
, realname
, realname_len
);
171 /* Now remove the filename and the slash. Leave the slash if
172 the name is something like "/foo". */
178 /* Keep the only slash which is the first character. */
183 new->l_origin
= origin
;