Update.
[glibc.git] / elf / dl-object.c
blob297f30eceed366f50a974bfa6cf76e48784418d8
1 /* Storage management for the chain of loaded shared objects.
2 Copyright (C) 1995, 1996, 1997, 1998 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <elf/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)
37 struct link_map *l;
38 int idx;
39 size_t libname_len = strlen (libname) + 1;
40 struct link_map *new = calloc (sizeof *new, 1);
41 struct libname_list *newname = malloc (sizeof *newname + libname_len);
42 if (! new || ! newname)
43 return NULL;
45 new->l_name = realname;
46 newname->name = memcpy (newname + 1, libname, libname_len);
47 newname->next = NULL;
48 new->l_libname = newname;
49 new->l_type = type;
50 new->l_loader = loader;
52 /* Counter for the scopes we have to handle. */
53 idx = 0;
55 if (_dl_loaded != NULL)
57 l = _dl_loaded;
58 while (l->l_next)
59 l = l->l_next;
60 new->l_prev = l;
61 /* new->l_next = NULL; Would be necesary but we use calloc. */
62 l->l_next = new;
64 /* Add the global scope. */
65 new->l_scope[idx++] = &_dl_loaded->l_searchlist;
67 else
68 _dl_loaded = new;
69 /* This is our local scope. */
70 if (loader != NULL)
72 while (loader->l_loader != NULL)
73 loader = loader->l_loader;
74 new->l_scope[idx] = &loader->l_searchlist;
76 else
77 new->l_scope[idx] = &new->l_searchlist;
79 new->l_local_scope[0] = new->l_scope[idx];
81 /* Don't try to find the origin for the main map which has the name "". */
82 if (realname[0] != '\0')
84 char *origin;
86 if (realname[0] == '/')
88 /* It an absolute path. Use it. But we have to make a copy since
89 we strip out the trailing slash. */
90 size_t len = strlen (realname) + 1;
91 origin = malloc (len);
92 if (origin == NULL)
93 origin = (char *) -1;
94 else
95 memcpy (origin, realname, len);
97 else
99 size_t realname_len = strlen (realname) + 1;
100 size_t len = 128 + realname_len;
101 char *result = NULL;
103 /* Get the current directory name. */
104 origin = malloc (len);
106 while (origin != NULL
107 && (result = __getcwd (origin, len - realname_len)) == NULL
108 && errno == ERANGE)
110 len += 128;
111 origin = (char *) realloc (origin, len);
114 if (result == NULL)
116 /* We were not able to determine the current directory. */
117 if (origin != NULL)
118 free (origin);
119 origin = (char *) -1;
121 else
123 /* Now append the filename. */
124 char *cp = strchr (origin, '\0');
126 if (cp [-1] != '/')
127 *cp++ = '/';
129 memcpy (cp, realname, realname_len);
133 if (origin != (char *) -1)
135 /* Now remove the filename and the slash. Do this even if the
136 string is something like "/foo" which leaves an empty string. */
137 char *last = strrchr (origin, '/');
139 if (last == origin)
140 origin[1] = '\0';
141 else
142 *last = '\0';
145 new->l_origin = origin;
148 return new;