]>
Commit | Line | Data |
---|---|---|
0a54e401 | 1 | /* Map in a shared object's segments from the file. |
880f421f | 2 | Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc. |
afd4eb37 | 3 | This file is part of the GNU C Library. |
d66e34cd | 4 | |
afd4eb37 UD |
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. | |
d66e34cd | 9 | |
afd4eb37 UD |
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. | |
d66e34cd | 14 | |
afd4eb37 UD |
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. */ | |
d66e34cd | 19 | |
0a54e401 UD |
20 | #include <errno.h> |
21 | #include <fcntl.h> | |
d66e34cd | 22 | #include <link.h> |
0a54e401 | 23 | #include <stdlib.h> |
d66e34cd | 24 | #include <string.h> |
d66e34cd | 25 | #include <unistd.h> |
0a54e401 UD |
26 | #include <sys/mman.h> |
27 | #include <sys/stat.h> | |
28 | #include <sys/types.h> | |
d66e34cd RM |
29 | #include "dynamic-link.h" |
30 | ||
31 | ||
9b8a44cd RM |
32 | /* On some systems, no flag bits are given to specify file mapping. */ |
33 | #ifndef MAP_FILE | |
34 | #define MAP_FILE 0 | |
35 | #endif | |
36 | ||
37 | /* The right way to map in the shared library files is MAP_COPY, which | |
38 | makes a virtual copy of the data at the time of the mmap call; this | |
39 | guarantees the mapped pages will be consistent even if the file is | |
40 | overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we | |
41 | get is MAP_PRIVATE, which copies each page when it is modified; this | |
42 | means if the file is overwritten, we may at some point get some pages | |
43 | from the new version after starting with pages from the old version. */ | |
44 | #ifndef MAP_COPY | |
45 | #define MAP_COPY MAP_PRIVATE | |
46 | #endif | |
47 | ||
f21acc89 UD |
48 | /* Some systems link their relocatable objects for another base address |
49 | than 0. We want to know the base address for these such that we can | |
50 | subtract this address from the segment addresses during mapping. | |
51 | This results in a more efficient address space usage. Defaults to | |
52 | zero for almost all systems. */ | |
53 | #ifndef MAP_BASE_ADDR | |
54 | #define MAP_BASE_ADDR(l) 0 | |
55 | #endif | |
56 | ||
9b8a44cd | 57 | |
d66e34cd RM |
58 | #include <endian.h> |
59 | #if BYTE_ORDER == BIG_ENDIAN | |
60 | #define byteorder ELFDATA2MSB | |
61 | #define byteorder_name "big-endian" | |
62 | #elif BYTE_ORDER == LITTLE_ENDIAN | |
63 | #define byteorder ELFDATA2LSB | |
64 | #define byteorder_name "little-endian" | |
65 | #else | |
66 | #error "Unknown BYTE_ORDER " BYTE_ORDER | |
67 | #define byteorder ELFDATANONE | |
68 | #endif | |
69 | ||
70 | #define STRING(x) #x | |
71 | ||
2064087b RM |
72 | #ifdef MAP_ANON |
73 | /* The fd is not examined when using MAP_ANON. */ | |
74 | #define ANONFD -1 | |
75 | #else | |
d66e34cd | 76 | int _dl_zerofd = -1; |
2064087b RM |
77 | #define ANONFD _dl_zerofd |
78 | #endif | |
79 | ||
4cca6b86 UD |
80 | /* Handle situations where we have a preferred location in memory for |
81 | the shared objects. */ | |
82 | #ifdef ELF_PREFERRED_ADDRESS_DATA | |
83 | ELF_PREFERRED_ADDRESS_DATA; | |
84 | #endif | |
85 | #ifndef ELF_PREFERRED_ADDRESS | |
86 | #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref) | |
87 | #endif | |
88 | #ifndef ELF_FIXED_ADDRESS | |
89 | #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0) | |
90 | #endif | |
91 | ||
266180eb | 92 | size_t _dl_pagesize; |
d66e34cd | 93 | |
0a54e401 UD |
94 | extern const char *_dl_platform; |
95 | extern size_t _dl_platformlen; | |
d66e34cd | 96 | |
40a55d20 UD |
97 | /* This is a fake list to store the RPATH information for static |
98 | binaries. */ | |
99 | static struct r_search_path_elem **fake_path_list; | |
100 | ||
101 | ||
706074a5 UD |
102 | /* Local version of `strdup' function. */ |
103 | static inline char * | |
104 | local_strdup (const char *s) | |
105 | { | |
106 | size_t len = strlen (s) + 1; | |
107 | void *new = malloc (len); | |
108 | ||
109 | if (new == NULL) | |
110 | return NULL; | |
111 | ||
112 | return (char *) memcpy (new, s, len); | |
113 | } | |
114 | ||
0413b54c UD |
115 | /* Add `name' to the list of names for a particular shared object. |
116 | `name' is expected to have been allocated with malloc and will | |
117 | be freed if the shared object already has this name. | |
118 | Returns false if the object already had this name. */ | |
119 | static int | |
120 | add_name_to_object (struct link_map *l, char *name) | |
0a54e401 | 121 | { |
0413b54c UD |
122 | struct libname_list *lnp, *lastp; |
123 | struct libname_list *newname; | |
0a54e401 | 124 | |
0413b54c UD |
125 | if (name == NULL) |
126 | { | |
127 | /* No more memory. */ | |
128 | _dl_signal_error (ENOMEM, NULL, _("could not allocate name string")); | |
129 | return 0; | |
130 | } | |
131 | ||
132 | lastp = NULL; | |
133 | for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next) | |
134 | if (strcmp (name, lnp->name) == 0) | |
135 | { | |
136 | free (name); | |
137 | return 0; | |
138 | } | |
139 | ||
140 | newname = malloc (sizeof *newname); | |
141 | if (newname == NULL) | |
142 | { | |
143 | /* No more memory. */ | |
144 | _dl_signal_error (ENOMEM, name, _("cannot allocate name record")); | |
145 | free(name); | |
146 | return 0; | |
147 | } | |
148 | /* The object should have a libname set from _dl_new_object. */ | |
149 | assert (lastp != NULL); | |
150 | ||
151 | newname->name = name; | |
152 | newname->next = NULL; | |
153 | lastp->next = newname; | |
154 | return 1; | |
155 | } | |
156 | ||
157 | ||
158 | /* Implement cache for search path lookup. */ | |
159 | #include "rtldtbl.h" | |
0a54e401 UD |
160 | |
161 | static size_t max_dirnamelen; | |
162 | ||
163 | static inline struct r_search_path_elem ** | |
164 | fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep, | |
165 | const char **trusted) | |
166 | { | |
167 | char *cp; | |
168 | size_t nelems = 0; | |
169 | ||
170 | while ((cp = __strsep (&rpath, sep)) != NULL) | |
171 | { | |
172 | struct r_search_path_elem *dirp; | |
173 | size_t len = strlen (cp); | |
174 | /* Remove trailing slashes. */ | |
175 | while (len > 1 && cp[len - 1] == '/') | |
176 | --len; | |
177 | ||
178 | /* Make sure we don't use untrusted directories if we run SUID. */ | |
179 | if (trusted != NULL) | |
180 | { | |
181 | const char **trun = trusted; | |
182 | ||
183 | /* All trusted directory must be complete name. */ | |
184 | if (cp[0] != '/') | |
185 | continue; | |
186 | ||
187 | while (*trun != NULL | |
188 | && (memcmp (*trun, cp, len) != 0 || (*trun)[len] != '\0')) | |
189 | ++trun; | |
190 | ||
191 | if (*trun == NULL) | |
192 | /* It's no trusted directory, skip it. */ | |
193 | continue; | |
194 | } | |
195 | ||
196 | /* Now add one. */ | |
197 | if (len > 0) | |
198 | cp[len++] = '/'; | |
199 | ||
200 | /* See if this directory is already known. */ | |
201 | for (dirp = all_dirs; dirp != NULL; dirp = dirp->next) | |
202 | if (dirp->dirnamelen == len && strcmp (cp, dirp->dirname) == 0) | |
203 | break; | |
204 | ||
205 | if (dirp != NULL) | |
206 | { | |
207 | /* It is available, see whether it's in our own list. */ | |
208 | size_t cnt; | |
209 | for (cnt = 0; cnt < nelems; ++cnt) | |
210 | if (result[cnt] == dirp) | |
211 | break; | |
212 | ||
213 | if (cnt == nelems) | |
214 | result[nelems++] = dirp; | |
215 | } | |
216 | else | |
217 | { | |
218 | /* It's a new directory. Create an entry and add it. */ | |
219 | dirp = (struct r_search_path_elem *) malloc (sizeof (*dirp)); | |
220 | if (dirp == NULL) | |
221 | _dl_signal_error (ENOMEM, NULL, | |
222 | "cannot create cache for search path"); | |
223 | ||
224 | dirp->dirnamelen = len; | |
3996f34b UD |
225 | /* We have to make sure all the relative directories are never |
226 | ignored. The current directory might change and all our | |
227 | saved information would be void. */ | |
228 | dirp->dirstatus = cp[0] != '/' ? existing : unknown; | |
0a54e401 UD |
229 | |
230 | /* Add the name of the machine dependent directory if a machine | |
231 | is defined. */ | |
232 | if (_dl_platform != NULL) | |
233 | { | |
234 | char *tmp; | |
235 | ||
236 | dirp->machdirnamelen = len + _dl_platformlen + 1; | |
237 | tmp = (char *) malloc (len + _dl_platformlen + 2); | |
238 | if (tmp == NULL) | |
239 | _dl_signal_error (ENOMEM, NULL, | |
240 | "cannot create cache for search path"); | |
0a54e401 | 241 | dirp->dirname = tmp; |
86187531 UD |
242 | tmp = __mempcpy (tmp, cp, len); |
243 | tmp = __mempcpy (tmp, _dl_platform, _dl_platformlen); | |
244 | *tmp++ = '/'; | |
245 | *tmp = '\0'; | |
246 | ||
3996f34b | 247 | dirp->machdirstatus = dirp->dirstatus; |
0a54e401 UD |
248 | |
249 | if (max_dirnamelen < dirp->machdirnamelen) | |
250 | max_dirnamelen = dirp->machdirnamelen; | |
251 | } | |
252 | else | |
253 | { | |
254 | char *tmp; | |
255 | ||
256 | dirp->machdirnamelen = len; | |
257 | dirp->machdirstatus = nonexisting; | |
258 | ||
259 | tmp = (char *) malloc (len + 1); | |
260 | if (tmp == NULL) | |
261 | _dl_signal_error (ENOMEM, NULL, | |
262 | "cannot create cache for search path"); | |
86187531 UD |
263 | dirp->dirname = tmp; |
264 | *((char *) __mempcpy (tmp, cp, len)) = '\0'; | |
0a54e401 UD |
265 | |
266 | if (max_dirnamelen < dirp->dirnamelen) | |
267 | max_dirnamelen = dirp->dirnamelen; | |
0a54e401 UD |
268 | } |
269 | ||
270 | dirp->next = all_dirs; | |
271 | all_dirs = dirp; | |
272 | ||
273 | /* Put it in the result array. */ | |
274 | result[nelems++] = dirp; | |
275 | } | |
276 | } | |
277 | ||
278 | /* Terminate the array. */ | |
279 | result[nelems] = NULL; | |
280 | ||
281 | return result; | |
282 | } | |
283 | ||
284 | ||
285 | static struct r_search_path_elem ** | |
286 | decompose_rpath (const char *rpath, size_t additional_room) | |
287 | { | |
288 | /* Make a copy we can work with. */ | |
289 | char *copy = strdupa (rpath); | |
290 | char *cp; | |
291 | struct r_search_path_elem **result; | |
292 | /* First count the number of necessary elements in the result array. */ | |
293 | size_t nelems = 0; | |
294 | ||
295 | for (cp = copy; *cp != '\0'; ++cp) | |
296 | if (*cp == ':') | |
297 | ++nelems; | |
298 | ||
299 | /* Allocate room for the result. NELEMS + 1 + ADDITIONAL_ROOM is an upper | |
300 | limit for the number of necessary entries. */ | |
301 | result = (struct r_search_path_elem **) malloc ((nelems + 1 | |
302 | + additional_room + 1) | |
303 | * sizeof (*result)); | |
304 | if (result == NULL) | |
305 | _dl_signal_error (ENOMEM, NULL, "cannot create cache for search path"); | |
306 | ||
307 | return fillin_rpath (copy, result, ":", NULL); | |
308 | } | |
309 | ||
310 | ||
311 | void | |
880f421f | 312 | _dl_init_paths (const char *llp) |
0a54e401 | 313 | { |
40a55d20 UD |
314 | static const char *trusted_dirs[] = |
315 | { | |
316 | #include "trusted-dirs.h" | |
317 | NULL | |
318 | }; | |
319 | ||
0a54e401 UD |
320 | struct r_search_path_elem **pelem; |
321 | ||
322 | /* We have in `search_path' the information about the RPATH of the | |
323 | dynamic loader. Now fill in the information about the applications | |
324 | RPATH and the directories addressed by the LD_LIBRARY_PATH environment | |
325 | variable. */ | |
326 | struct link_map *l; | |
327 | ||
880f421f | 328 | /* Number of elements in the library path. */ |
0a54e401 UD |
329 | size_t nllp; |
330 | ||
880f421f UD |
331 | /* If the user has not specified a library path consider the environment |
332 | variable. */ | |
333 | if (llp == NULL) | |
334 | llp = getenv ("LD_LIBRARY_PATH"); | |
335 | ||
336 | /* First determine how many elements the LD_LIBRARY_PATH contents has. */ | |
0a54e401 UD |
337 | if (llp != NULL && *llp != '\0') |
338 | { | |
339 | /* Simply count the number of colons. */ | |
340 | const char *cp = llp; | |
341 | nllp = 1; | |
342 | while (*cp) | |
343 | if (*cp++ == ':') | |
344 | ++nllp; | |
345 | } | |
346 | else | |
347 | nllp = 0; | |
348 | ||
349 | l = _dl_loaded; | |
40a55d20 | 350 | if (l != NULL) |
0a54e401 | 351 | { |
40a55d20 UD |
352 | if (l->l_type != lt_loaded && l->l_info[DT_RPATH]) |
353 | { | |
354 | /* Allocate room for the search path and fill in information | |
355 | from RPATH. */ | |
356 | l->l_rpath_dirs = | |
357 | decompose_rpath ((const char *) | |
358 | (l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr | |
359 | + l->l_info[DT_RPATH]->d_un.d_val), | |
360 | nllp); | |
361 | } | |
362 | else | |
363 | { | |
364 | /* If we have no LD_LIBRARY_PATH and no RPATH we must tell | |
365 | this somehow to prevent we look this up again and again. */ | |
366 | if (nllp == 0) | |
367 | l->l_rpath_dirs = (struct r_search_path_elem **) -1l; | |
368 | else | |
369 | { | |
370 | l->l_rpath_dirs = (struct r_search_path_elem **) | |
371 | malloc ((nllp + 1) * sizeof (*l->l_rpath_dirs)); | |
372 | if (l->l_rpath_dirs == NULL) | |
373 | _dl_signal_error (ENOMEM, NULL, | |
374 | "cannot create cache for search path"); | |
375 | l->l_rpath_dirs[0] = NULL; | |
376 | } | |
377 | } | |
378 | ||
379 | /* We don't need to search the list of fake entries which is searched | |
380 | when no dynamic objects were loaded at this time. */ | |
381 | fake_path_list = NULL; | |
382 | ||
383 | if (nllp > 0) | |
384 | { | |
385 | char *copy = strdupa (llp); | |
386 | ||
387 | /* Decompose the LD_LIBRARY_PATH and fill in the result. | |
388 | First search for the next place to enter elements. */ | |
389 | struct r_search_path_elem **result = l->l_rpath_dirs; | |
390 | while (*result != NULL) | |
391 | ++result; | |
392 | ||
393 | /* We need to take care that the LD_LIBRARY_PATH environment | |
394 | variable can contain a semicolon. */ | |
395 | (void) fillin_rpath (copy, result, ":;", | |
396 | __libc_enable_secure ? trusted_dirs : NULL); | |
397 | } | |
0a54e401 UD |
398 | } |
399 | else | |
400 | { | |
40a55d20 UD |
401 | /* This is a statically linked program but we still have to |
402 | take care for the LD_LIBRARY_PATH environment variable. We | |
403 | use a fake link_map entry. This will only contain the | |
404 | l_rpath_dirs information. */ | |
405 | ||
0a54e401 | 406 | if (nllp == 0) |
40a55d20 | 407 | fake_path_list = NULL; |
0a54e401 UD |
408 | else |
409 | { | |
40a55d20 UD |
410 | fake_path_list = (struct r_search_path_elem **) |
411 | malloc ((nllp + 1) * sizeof (struct r_search_path_elem *)); | |
f41c8091 UD |
412 | if (fake_path_list == NULL) |
413 | _dl_signal_error (ENOMEM, NULL, | |
414 | "cannot create cache for search path"); | |
0a54e401 | 415 | |
40a55d20 UD |
416 | (void) fillin_rpath (local_strdup (llp), fake_path_list, ":;", |
417 | __libc_enable_secure ? trusted_dirs : NULL); | |
418 | } | |
0a54e401 UD |
419 | } |
420 | ||
421 | /* Now set up the rest of the rtld_search_dirs. */ | |
422 | for (pelem = rtld_search_dirs; *pelem != NULL; ++pelem) | |
423 | { | |
424 | struct r_search_path_elem *relem = *pelem; | |
425 | ||
426 | if (_dl_platform != NULL) | |
427 | { | |
f43ce637 | 428 | char *tmp, *orig; |
0a54e401 UD |
429 | |
430 | relem->machdirnamelen = relem->dirnamelen + _dl_platformlen + 1; | |
431 | tmp = (char *) malloc (relem->machdirnamelen + 1); | |
432 | if (tmp == NULL) | |
433 | _dl_signal_error (ENOMEM, NULL, | |
434 | "cannot create cache for search path"); | |
435 | ||
f43ce637 | 436 | orig = tmp; |
86187531 UD |
437 | tmp = __mempcpy (tmp, relem->dirname, relem->dirnamelen); |
438 | tmp = __mempcpy (tmp, _dl_platform, _dl_platformlen); | |
439 | *tmp++ = '/'; | |
440 | *tmp = '\0'; | |
f43ce637 | 441 | relem->dirname = orig; |
0a54e401 UD |
442 | |
443 | relem->machdirstatus = unknown; | |
444 | ||
445 | if (max_dirnamelen < relem->machdirnamelen) | |
446 | max_dirnamelen = relem->machdirnamelen; | |
447 | } | |
448 | else | |
449 | { | |
450 | relem->machdirnamelen = relem->dirnamelen; | |
451 | relem->machdirstatus = nonexisting; | |
452 | ||
453 | if (max_dirnamelen < relem->dirnamelen) | |
454 | max_dirnamelen = relem->dirnamelen; | |
455 | } | |
456 | } | |
457 | } | |
458 | ||
459 | ||
ea03559a RM |
460 | /* Map in the shared object NAME, actually located in REALNAME, and already |
461 | opened on FD. */ | |
462 | ||
463 | struct link_map * | |
706074a5 | 464 | _dl_map_object_from_fd (char *name, int fd, char *realname, |
ba79d61b | 465 | struct link_map *loader, int l_type) |
ea03559a | 466 | { |
622586fb | 467 | struct link_map *l = NULL; |
ea03559a RM |
468 | void *file_mapping = NULL; |
469 | size_t mapping_size = 0; | |
470 | ||
b122c703 | 471 | #define LOSE(s) lose (0, (s)) |
ea03559a RM |
472 | void lose (int code, const char *msg) |
473 | { | |
266180eb | 474 | (void) __close (fd); |
ea03559a | 475 | if (file_mapping) |
266180eb | 476 | __munmap (file_mapping, mapping_size); |
ba79d61b RM |
477 | if (l) |
478 | { | |
479 | /* Remove the stillborn object from the list and free it. */ | |
480 | if (l->l_prev) | |
481 | l->l_prev->l_next = l->l_next; | |
482 | if (l->l_next) | |
483 | l->l_next->l_prev = l->l_prev; | |
484 | free (l); | |
485 | } | |
486 | free (realname); | |
487 | _dl_signal_error (code, name, msg); | |
0413b54c UD |
488 | free (name); /* Hmmm. Can this leak memory? Better |
489 | than a segfault, anyway. */ | |
ea03559a RM |
490 | } |
491 | ||
266180eb | 492 | inline caddr_t map_segment (ElfW(Addr) mapstart, size_t len, |
b122c703 RM |
493 | int prot, int fixed, off_t offset) |
494 | { | |
266180eb RM |
495 | caddr_t mapat = __mmap ((caddr_t) mapstart, len, prot, |
496 | fixed|MAP_COPY|MAP_FILE, | |
497 | fd, offset); | |
0413b54c | 498 | if (mapat == MAP_FAILED) |
b122c703 RM |
499 | lose (errno, "failed to map segment from shared object"); |
500 | return mapat; | |
501 | } | |
502 | ||
ea03559a RM |
503 | /* Make sure LOCATION is mapped in. */ |
504 | void *map (off_t location, size_t size) | |
505 | { | |
506 | if ((off_t) mapping_size <= location + (off_t) size) | |
507 | { | |
508 | void *result; | |
509 | if (file_mapping) | |
266180eb RM |
510 | __munmap (file_mapping, mapping_size); |
511 | mapping_size = (location + size + 1 + _dl_pagesize - 1); | |
512 | mapping_size &= ~(_dl_pagesize - 1); | |
513 | result = __mmap (file_mapping, mapping_size, PROT_READ, | |
514 | MAP_COPY|MAP_FILE, fd, 0); | |
0413b54c | 515 | if (result == MAP_FAILED) |
ea03559a RM |
516 | lose (errno, "cannot map file data"); |
517 | file_mapping = result; | |
518 | } | |
519 | return file_mapping + location; | |
520 | } | |
521 | ||
266180eb RM |
522 | const ElfW(Ehdr) *header; |
523 | const ElfW(Phdr) *phdr; | |
524 | const ElfW(Phdr) *ph; | |
b122c703 | 525 | int type; |
d66e34cd RM |
526 | |
527 | /* Look again to see if the real name matched another already loaded. */ | |
528 | for (l = _dl_loaded; l; l = l->l_next) | |
529 | if (! strcmp (realname, l->l_name)) | |
530 | { | |
531 | /* The object is already loaded. | |
532 | Just bump its reference count and return it. */ | |
266180eb | 533 | __close (fd); |
c84142e8 UD |
534 | |
535 | /* If the name is not in the list of names for this object add | |
536 | it. */ | |
ea03559a | 537 | free (realname); |
0413b54c | 538 | add_name_to_object (l, name); |
d66e34cd RM |
539 | ++l->l_opencount; |
540 | return l; | |
541 | } | |
542 | ||
d66e34cd RM |
543 | /* Map in the first page to read the header. */ |
544 | header = map (0, sizeof *header); | |
d66e34cd | 545 | |
d66e34cd | 546 | /* Check the header for basic validity. */ |
c4b72918 RM |
547 | if (*(Elf32_Word *) &header->e_ident != |
548 | #if BYTE_ORDER == LITTLE_ENDIAN | |
549 | ((ELFMAG0 << (EI_MAG0 * 8)) | | |
550 | (ELFMAG1 << (EI_MAG1 * 8)) | | |
551 | (ELFMAG2 << (EI_MAG2 * 8)) | | |
552 | (ELFMAG3 << (EI_MAG3 * 8))) | |
553 | #else | |
554 | ((ELFMAG0 << (EI_MAG3 * 8)) | | |
555 | (ELFMAG1 << (EI_MAG2 * 8)) | | |
556 | (ELFMAG2 << (EI_MAG1 * 8)) | | |
557 | (ELFMAG3 << (EI_MAG0 * 8))) | |
558 | #endif | |
559 | ) | |
d66e34cd | 560 | LOSE ("invalid ELF header"); |
266180eb RM |
561 | #define ELF32_CLASS ELFCLASS32 |
562 | #define ELF64_CLASS ELFCLASS64 | |
563 | if (header->e_ident[EI_CLASS] != ELFW(CLASS)) | |
564 | LOSE ("ELF file class not " STRING(__ELF_WORDSIZE) "-bit"); | |
d66e34cd RM |
565 | if (header->e_ident[EI_DATA] != byteorder) |
566 | LOSE ("ELF file data encoding not " byteorder_name); | |
567 | if (header->e_ident[EI_VERSION] != EV_CURRENT) | |
568 | LOSE ("ELF file version ident not " STRING(EV_CURRENT)); | |
569 | if (header->e_version != EV_CURRENT) | |
570 | LOSE ("ELF file version not " STRING(EV_CURRENT)); | |
571 | if (! elf_machine_matches_host (header->e_machine)) | |
572 | LOSE ("ELF file machine architecture not " ELF_MACHINE_NAME); | |
266180eb | 573 | if (header->e_phentsize != sizeof (ElfW(Phdr))) |
d66e34cd RM |
574 | LOSE ("ELF file's phentsize not the expected size"); |
575 | ||
2064087b RM |
576 | #ifndef MAP_ANON |
577 | #define MAP_ANON 0 | |
d66e34cd RM |
578 | if (_dl_zerofd == -1) |
579 | { | |
580 | _dl_zerofd = _dl_sysdep_open_zero_fill (); | |
581 | if (_dl_zerofd == -1) | |
ba79d61b RM |
582 | { |
583 | __close (fd); | |
584 | _dl_signal_error (errno, NULL, "cannot open zero fill device"); | |
585 | } | |
d66e34cd | 586 | } |
2064087b | 587 | #endif |
d66e34cd | 588 | |
ba79d61b RM |
589 | /* Enter the new object in the list of loaded objects. */ |
590 | l = _dl_new_object (realname, name, l_type); | |
591 | if (! l) | |
592 | lose (ENOMEM, "cannot create shared object descriptor"); | |
593 | l->l_opencount = 1; | |
594 | l->l_loader = loader; | |
595 | ||
b122c703 RM |
596 | /* Extract the remaining details we need from the ELF header |
597 | and then map in the program header table. */ | |
598 | l->l_entry = header->e_entry; | |
599 | type = header->e_type; | |
600 | l->l_phnum = header->e_phnum; | |
266180eb | 601 | phdr = map (header->e_phoff, l->l_phnum * sizeof (ElfW(Phdr))); |
879bf2e6 | 602 | |
b122c703 RM |
603 | { |
604 | /* Scan the program header table, collecting its load commands. */ | |
605 | struct loadcmd | |
606 | { | |
266180eb | 607 | ElfW(Addr) mapstart, mapend, dataend, allocend; |
b122c703 RM |
608 | off_t mapoff; |
609 | int prot; | |
610 | } loadcmds[l->l_phnum], *c; | |
611 | size_t nloadcmds = 0; | |
d66e34cd | 612 | |
d66e34cd | 613 | l->l_ld = 0; |
b122c703 RM |
614 | l->l_phdr = 0; |
615 | l->l_addr = 0; | |
d66e34cd RM |
616 | for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph) |
617 | switch (ph->p_type) | |
618 | { | |
619 | /* These entries tell us where to find things once the file's | |
620 | segments are mapped in. We record the addresses it says | |
621 | verbatim, and later correct for the run-time load address. */ | |
622 | case PT_DYNAMIC: | |
623 | l->l_ld = (void *) ph->p_vaddr; | |
624 | break; | |
625 | case PT_PHDR: | |
626 | l->l_phdr = (void *) ph->p_vaddr; | |
627 | break; | |
628 | ||
629 | case PT_LOAD: | |
b122c703 RM |
630 | /* A load command tells us to map in part of the file. |
631 | We record the load commands and process them all later. */ | |
266180eb | 632 | if (ph->p_align % _dl_pagesize != 0) |
d66e34cd RM |
633 | LOSE ("ELF load command alignment not page-aligned"); |
634 | if ((ph->p_vaddr - ph->p_offset) % ph->p_align) | |
635 | LOSE ("ELF load command address/offset not properly aligned"); | |
636 | { | |
b122c703 RM |
637 | struct loadcmd *c = &loadcmds[nloadcmds++]; |
638 | c->mapstart = ph->p_vaddr & ~(ph->p_align - 1); | |
266180eb RM |
639 | c->mapend = ((ph->p_vaddr + ph->p_filesz + _dl_pagesize - 1) |
640 | & ~(_dl_pagesize - 1)); | |
b122c703 RM |
641 | c->dataend = ph->p_vaddr + ph->p_filesz; |
642 | c->allocend = ph->p_vaddr + ph->p_memsz; | |
643 | c->mapoff = ph->p_offset & ~(ph->p_align - 1); | |
644 | c->prot = 0; | |
d66e34cd | 645 | if (ph->p_flags & PF_R) |
b122c703 | 646 | c->prot |= PROT_READ; |
d66e34cd | 647 | if (ph->p_flags & PF_W) |
b122c703 | 648 | c->prot |= PROT_WRITE; |
d66e34cd | 649 | if (ph->p_flags & PF_X) |
b122c703 RM |
650 | c->prot |= PROT_EXEC; |
651 | break; | |
652 | } | |
653 | } | |
d66e34cd | 654 | |
b122c703 | 655 | /* We are done reading the file's headers now. Unmap them. */ |
266180eb | 656 | __munmap (file_mapping, mapping_size); |
b122c703 RM |
657 | |
658 | /* Now process the load commands and map segments into memory. */ | |
659 | c = loadcmds; | |
660 | ||
661 | if (type == ET_DYN || type == ET_REL) | |
662 | { | |
663 | /* This is a position-independent shared object. We can let the | |
664 | kernel map it anywhere it likes, but we must have space for all | |
665 | the segments in their specified positions relative to the first. | |
666 | So we map the first segment without MAP_FIXED, but with its | |
22930c9b RM |
667 | extent increased to cover all the segments. Then we remove |
668 | access from excess portion, and there is known sufficient space | |
4cca6b86 UD |
669 | there to remap from the later segments. |
670 | ||
671 | As a refinement, sometimes we have an address that we would | |
672 | prefer to map such objects at; but this is only a preference, | |
673 | the OS can do whatever it likes. */ | |
b122c703 | 674 | caddr_t mapat; |
4cca6b86 UD |
675 | ElfW(Addr) mappref; |
676 | size_t maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart; | |
f21acc89 UD |
677 | mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart) |
678 | - MAP_BASE_ADDR (l)); | |
4cca6b86 | 679 | mapat = map_segment (mappref, maplength, c->prot, 0, c->mapoff); |
266180eb | 680 | l->l_addr = (ElfW(Addr)) mapat - c->mapstart; |
b122c703 | 681 | |
22930c9b RM |
682 | /* Change protection on the excess portion to disallow all access; |
683 | the portions we do not remap later will be inaccessible as if | |
684 | unallocated. Then jump into the normal segment-mapping loop to | |
685 | handle the portion of the segment past the end of the file | |
686 | mapping. */ | |
0d3726c3 | 687 | __mprotect ((caddr_t) (l->l_addr + c->mapend), |
266180eb RM |
688 | loadcmds[nloadcmds - 1].allocend - c->mapend, |
689 | 0); | |
b122c703 RM |
690 | goto postmap; |
691 | } | |
4cca6b86 UD |
692 | else |
693 | { | |
694 | /* Notify ELF_PREFERRED_ADDRESS that we have to load this one | |
695 | fixed. */ | |
696 | ELF_FIXED_ADDRESS (loader, c->mapstart); | |
697 | } | |
b122c703 RM |
698 | |
699 | while (c < &loadcmds[nloadcmds]) | |
700 | { | |
701 | if (c->mapend > c->mapstart) | |
702 | /* Map the segment contents from the file. */ | |
703 | map_segment (l->l_addr + c->mapstart, c->mapend - c->mapstart, | |
704 | c->prot, MAP_FIXED, c->mapoff); | |
705 | ||
706 | postmap: | |
707 | if (c->allocend > c->dataend) | |
708 | { | |
709 | /* Extra zero pages should appear at the end of this segment, | |
710 | after the data mapped from the file. */ | |
266180eb | 711 | ElfW(Addr) zero, zeroend, zeropage; |
b122c703 RM |
712 | |
713 | zero = l->l_addr + c->dataend; | |
714 | zeroend = l->l_addr + c->allocend; | |
266180eb | 715 | zeropage = (zero + _dl_pagesize - 1) & ~(_dl_pagesize - 1); |
d66e34cd | 716 | |
b122c703 RM |
717 | if (zeroend < zeropage) |
718 | /* All the extra data is in the last page of the segment. | |
719 | We can just zero it. */ | |
720 | zeropage = zeroend; | |
721 | ||
722 | if (zeropage > zero) | |
d66e34cd | 723 | { |
b122c703 RM |
724 | /* Zero the final part of the last page of the segment. */ |
725 | if ((c->prot & PROT_WRITE) == 0) | |
d66e34cd | 726 | { |
b122c703 | 727 | /* Dag nab it. */ |
266180eb RM |
728 | if (__mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)), |
729 | _dl_pagesize, c->prot|PROT_WRITE) < 0) | |
b122c703 | 730 | lose (errno, "cannot change memory protections"); |
d66e34cd | 731 | } |
b122c703 RM |
732 | memset ((void *) zero, 0, zeropage - zero); |
733 | if ((c->prot & PROT_WRITE) == 0) | |
266180eb RM |
734 | __mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)), |
735 | _dl_pagesize, c->prot); | |
b122c703 | 736 | } |
d66e34cd | 737 | |
b122c703 RM |
738 | if (zeroend > zeropage) |
739 | { | |
740 | /* Map the remaining zero pages in from the zero fill FD. */ | |
741 | caddr_t mapat; | |
266180eb RM |
742 | mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage, |
743 | c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED, | |
2064087b | 744 | ANONFD, 0); |
0413b54c | 745 | if (mapat == MAP_FAILED) |
9b8a44cd | 746 | lose (errno, "cannot map zero-fill pages"); |
d66e34cd RM |
747 | } |
748 | } | |
d66e34cd | 749 | |
b122c703 | 750 | ++c; |
879bf2e6 | 751 | } |
0d3726c3 RM |
752 | |
753 | if (l->l_phdr == 0) | |
754 | { | |
755 | /* There was no PT_PHDR specified. We need to find the phdr in the | |
756 | load image ourselves. We assume it is in fact in the load image | |
757 | somewhere, and that the first load command starts at the | |
758 | beginning of the file and thus contains the ELF file header. */ | |
759 | ElfW(Addr) bof = l->l_addr + loadcmds[0].mapstart; | |
760 | assert (loadcmds[0].mapoff == 0); | |
761 | l->l_phdr = (void *) (bof + ((const ElfW(Ehdr) *) bof)->e_phoff); | |
762 | } | |
763 | else | |
764 | /* Adjust the PT_PHDR value by the runtime load address. */ | |
765 | (ElfW(Addr)) l->l_phdr += l->l_addr; | |
b122c703 | 766 | } |
d66e34cd | 767 | |
6d9756c9 RM |
768 | /* We are done mapping in the file. We no longer need the descriptor. */ |
769 | __close (fd); | |
770 | ||
ba79d61b RM |
771 | if (l->l_type == lt_library && type == ET_EXEC) |
772 | l->l_type = lt_executable; | |
9b8a44cd | 773 | |
b122c703 RM |
774 | if (l->l_ld == 0) |
775 | { | |
776 | if (type == ET_DYN) | |
777 | LOSE ("object file has no dynamic section"); | |
778 | } | |
779 | else | |
266180eb | 780 | (ElfW(Addr)) l->l_ld += l->l_addr; |
879bf2e6 | 781 | |
463e148b RM |
782 | l->l_entry += l->l_addr; |
783 | ||
d66e34cd RM |
784 | elf_get_dynamic_info (l->l_ld, l->l_info); |
785 | if (l->l_info[DT_HASH]) | |
786 | _dl_setup_hash (l); | |
787 | ||
788 | return l; | |
789 | } | |
ba79d61b | 790 | \f |
0a54e401 | 791 | /* Try to open NAME in one of the directories in DIRS. |
ba79d61b RM |
792 | Return the fd, or -1. If successful, fill in *REALNAME |
793 | with the malloc'd full directory name. */ | |
794 | ||
795 | static int | |
796 | open_path (const char *name, size_t namelen, | |
0a54e401 UD |
797 | struct r_search_path_elem **dirs, |
798 | char **realname) | |
ba79d61b RM |
799 | { |
800 | char *buf; | |
0a54e401 | 801 | int fd = -1; |
ba79d61b | 802 | |
0a54e401 | 803 | if (dirs == NULL || *dirs == NULL) |
ba79d61b | 804 | { |
c4029823 | 805 | __set_errno (ENOENT); |
ba79d61b RM |
806 | return -1; |
807 | } | |
808 | ||
0a54e401 | 809 | buf = __alloca (max_dirnamelen + namelen); |
ba79d61b RM |
810 | do |
811 | { | |
0a54e401 UD |
812 | struct r_search_path_elem *this_dir = *dirs; |
813 | size_t buflen = 0; | |
ba79d61b | 814 | |
0a54e401 | 815 | if (this_dir->machdirstatus != nonexisting) |
fd26970f | 816 | { |
0a54e401 | 817 | /* Construct the pathname to try. */ |
86187531 UD |
818 | buflen = ((char *) __mempcpy (__mempcpy (buf, this_dir->dirname, |
819 | this_dir->machdirnamelen), | |
820 | name, namelen) | |
821 | - buf); | |
0a54e401 UD |
822 | |
823 | fd = __open (buf, O_RDONLY); | |
824 | if (this_dir->machdirstatus == unknown) | |
825 | if (fd != -1) | |
826 | this_dir->machdirstatus = existing; | |
827 | else | |
828 | { | |
829 | /* We failed to open machine dependent library. Let's | |
830 | test whether there is any directory at all. */ | |
831 | struct stat st; | |
fd26970f | 832 | |
0a54e401 | 833 | buf[this_dir->machdirnamelen - 1] = '\0'; |
fd26970f | 834 | |
0a614877 UD |
835 | if (__xstat (_STAT_VER, buf, &st) != 0 |
836 | || ! S_ISDIR (st.st_mode)) | |
0a54e401 UD |
837 | /* The directory does not exist ot it is no directory. */ |
838 | this_dir->machdirstatus = nonexisting; | |
fd26970f | 839 | else |
0a54e401 UD |
840 | this_dir->machdirstatus = existing; |
841 | } | |
fd26970f UD |
842 | } |
843 | ||
0a54e401 | 844 | if (fd == -1 && this_dir->dirstatus != nonexisting) |
ba79d61b RM |
845 | { |
846 | /* Construct the pathname to try. */ | |
86187531 UD |
847 | buflen = ((char *) __mempcpy (__mempcpy (buf, this_dir->dirname, |
848 | this_dir->dirnamelen), | |
849 | name, namelen) | |
850 | - buf); | |
0a54e401 UD |
851 | |
852 | fd = __open (buf, O_RDONLY); | |
853 | if (this_dir->dirstatus == unknown) | |
854 | if (fd != -1) | |
855 | this_dir->dirstatus = existing; | |
856 | else | |
857 | /* We failed to open library. Let's test whether there | |
858 | is any directory at all. */ | |
859 | if (this_dir->dirnamelen <= 1) | |
860 | this_dir->dirstatus = existing; | |
861 | else | |
862 | { | |
863 | struct stat st; | |
864 | ||
865 | buf[this_dir->dirnamelen - 1] = '\0'; | |
866 | ||
0a614877 UD |
867 | if (__xstat (_STAT_VER, buf, &st) != 0 |
868 | || ! S_ISDIR (st.st_mode)) | |
0a54e401 UD |
869 | /* The directory does not exist ot it is no directory. */ |
870 | this_dir->dirstatus = nonexisting; | |
871 | else | |
872 | this_dir->dirstatus = existing; | |
873 | } | |
ba79d61b RM |
874 | } |
875 | ||
ba79d61b RM |
876 | if (fd != -1) |
877 | { | |
878 | *realname = malloc (buflen); | |
879 | if (*realname) | |
880 | { | |
881 | memcpy (*realname, buf, buflen); | |
882 | return fd; | |
883 | } | |
884 | else | |
885 | { | |
886 | /* No memory for the name, we certainly won't be able | |
887 | to load and link it. */ | |
888 | __close (fd); | |
889 | return -1; | |
890 | } | |
891 | } | |
892 | if (errno != ENOENT && errno != EACCES) | |
893 | /* The file exists and is readable, but something went wrong. */ | |
894 | return -1; | |
895 | } | |
0a54e401 | 896 | while (*++dirs != NULL); |
ba79d61b RM |
897 | |
898 | return -1; | |
899 | } | |
900 | ||
901 | /* Map in the shared object file NAME. */ | |
902 | ||
903 | struct link_map * | |
46ec036d UD |
904 | _dl_map_object (struct link_map *loader, const char *name, int type, |
905 | int trace_mode) | |
ba79d61b RM |
906 | { |
907 | int fd; | |
908 | char *realname; | |
14bab8de | 909 | char *name_copy; |
ba79d61b RM |
910 | struct link_map *l; |
911 | ||
912 | /* Look for this name among those already loaded. */ | |
913 | for (l = _dl_loaded; l; l = l->l_next) | |
f41c8091 UD |
914 | { |
915 | /* If the requested name matches the soname of a loaded object, | |
916 | use that object. Elide this check for names that have not | |
917 | yet been opened. */ | |
918 | if (l->l_opencount <= 0) | |
919 | continue; | |
920 | if (!_dl_name_match_p (name, l)) | |
921 | { | |
922 | const char *soname; | |
923 | ||
924 | if (l->l_info[DT_SONAME] == NULL) | |
925 | continue; | |
926 | ||
927 | soname = (const char *) (l->l_addr | |
928 | + l->l_info[DT_STRTAB]->d_un.d_ptr | |
929 | + l->l_info[DT_SONAME]->d_un.d_val); | |
930 | if (strcmp (name, soname) != 0) | |
931 | continue; | |
932 | ||
933 | /* We have a match on a new name -- cache it. */ | |
934 | add_name_to_object (l, local_strdup (soname)); | |
935 | } | |
936 | ||
937 | /* We have a match -- bump the reference count and return it. */ | |
938 | ++l->l_opencount; | |
939 | return l; | |
940 | } | |
ba79d61b RM |
941 | |
942 | if (strchr (name, '/') == NULL) | |
943 | { | |
944 | /* Search for NAME in several places. */ | |
945 | ||
946 | size_t namelen = strlen (name) + 1; | |
947 | ||
ba79d61b | 948 | fd = -1; |
a23db8e4 RM |
949 | |
950 | /* First try the DT_RPATH of the dependent object that caused NAME | |
951 | to be loaded. Then that object's dependent, and on up. */ | |
952 | for (l = loader; fd == -1 && l; l = l->l_loader) | |
ba79d61b | 953 | if (l && l->l_info[DT_RPATH]) |
fd26970f | 954 | { |
0a54e401 UD |
955 | /* Make sure the cache information is available. */ |
956 | if (l->l_rpath_dirs == NULL) | |
957 | { | |
958 | size_t ptrval = (l->l_addr | |
959 | + l->l_info[DT_STRTAB]->d_un.d_ptr | |
960 | + l->l_info[DT_RPATH]->d_un.d_val); | |
961 | l->l_rpath_dirs = | |
962 | decompose_rpath ((const char *) ptrval, 0); | |
963 | } | |
964 | ||
965 | if (l->l_rpath_dirs != (struct r_search_path_elem **) -1l) | |
966 | fd = open_path (name, namelen, l->l_rpath_dirs, &realname); | |
967 | } | |
968 | ||
969 | /* If dynamically linked, try the DT_RPATH of the executable itself | |
970 | and the LD_LIBRARY_PATH environment variable. */ | |
971 | l = _dl_loaded; | |
972 | if (fd == -1 && l && l->l_type != lt_loaded | |
973 | && l->l_rpath_dirs != (struct r_search_path_elem **) -1l) | |
974 | fd = open_path (name, namelen, l->l_rpath_dirs, &realname); | |
fd26970f | 975 | |
40a55d20 UD |
976 | /* This is used if a static binary uses dynamic loading and there |
977 | is a LD_LIBRARY_PATH given. */ | |
978 | if (fd == -1 && fake_path_list != NULL) | |
979 | fd = open_path (name, namelen, fake_path_list, &realname); | |
980 | ||
f18edac3 RM |
981 | if (fd == -1) |
982 | { | |
983 | /* Check the list of libraries in the file /etc/ld.so.cache, | |
984 | for compatibility with Linux's ldconfig program. */ | |
985 | extern const char *_dl_load_cache_lookup (const char *name); | |
986 | const char *cached = _dl_load_cache_lookup (name); | |
987 | if (cached) | |
988 | { | |
989 | fd = __open (cached, O_RDONLY); | |
990 | if (fd != -1) | |
991 | { | |
706074a5 UD |
992 | realname = local_strdup (cached); |
993 | if (realname == NULL) | |
f18edac3 RM |
994 | { |
995 | __close (fd); | |
996 | fd = -1; | |
997 | } | |
998 | } | |
999 | } | |
1000 | } | |
0a54e401 | 1001 | |
a23db8e4 | 1002 | /* Finally, try the default path. */ |
ba79d61b | 1003 | if (fd == -1) |
0a54e401 | 1004 | fd = open_path (name, namelen, rtld_search_dirs, &realname); |
ba79d61b RM |
1005 | } |
1006 | else | |
1007 | { | |
1008 | fd = __open (name, O_RDONLY); | |
1009 | if (fd != -1) | |
1010 | { | |
706074a5 UD |
1011 | realname = local_strdup (name); |
1012 | if (realname == NULL) | |
ba79d61b RM |
1013 | { |
1014 | __close (fd); | |
1015 | fd = -1; | |
1016 | } | |
1017 | } | |
1018 | } | |
1019 | ||
706074a5 UD |
1020 | if (fd != -1) |
1021 | { | |
14bab8de UD |
1022 | name_copy = local_strdup (name); |
1023 | if (name_copy == NULL) | |
706074a5 UD |
1024 | { |
1025 | __close (fd); | |
1026 | fd = -1; | |
1027 | } | |
1028 | } | |
1029 | ||
ba79d61b | 1030 | if (fd == -1) |
46ec036d UD |
1031 | { |
1032 | if (trace_mode) | |
1033 | { | |
1034 | /* We haven't found an appropriate library. But since we | |
1035 | are only interested in the list of libraries this isn't | |
1036 | so severe. Fake an entry with all the information we | |
1ef32c3d | 1037 | have. */ |
fd26970f | 1038 | static const ElfW(Symndx) dummy_bucket = STN_UNDEF; |
46ec036d UD |
1039 | |
1040 | /* Enter the new object in the list of loaded objects. */ | |
1041 | if ((name_copy = local_strdup (name)) == NULL | |
1042 | || (l = _dl_new_object (name_copy, name, type)) == NULL) | |
1043 | _dl_signal_error (ENOMEM, name, | |
1044 | "cannot create shared object descriptor"); | |
1045 | /* We use an opencount of 0 as a sign for the faked entry. */ | |
1046 | l->l_opencount = 0; | |
1047 | l->l_reserved = 0; | |
fd26970f UD |
1048 | l->l_buckets = &dummy_bucket; |
1049 | l->l_nbuckets = 1; | |
1050 | l->l_relocated = 1; | |
1051 | ||
1052 | return l; | |
46ec036d UD |
1053 | } |
1054 | else | |
1055 | _dl_signal_error (errno, name, "cannot open shared object file"); | |
1056 | } | |
ba79d61b | 1057 | |
14bab8de | 1058 | return _dl_map_object_from_fd (name_copy, fd, realname, loader, type); |
ba79d61b | 1059 | } |