1 | /* Map in a shared object's segments from the file. |
2 | Copyright (C) 1995-2021 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
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. |
9 | |
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. |
14 | |
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 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <elf.h> |
20 | #include <errno.h> |
21 | #include <fcntl.h> |
22 | #include <libintl.h> |
23 | #include <stdbool.h> |
24 | #include <stdlib.h> |
25 | #include <string.h> |
26 | #include <unistd.h> |
27 | #include <ldsodefs.h> |
28 | #include <bits/wordsize.h> |
29 | #include <sys/mman.h> |
30 | #include <sys/param.h> |
31 | #include <sys/stat.h> |
32 | #include <sys/types.h> |
33 | #include <gnu/lib-names.h> |
34 | |
35 | /* Type for the buffer we put the ELF header and hopefully the program |
36 | header. This buffer does not really have to be too large. In most |
37 | cases the program header follows the ELF header directly. If this |
38 | is not the case all bets are off and we can make the header |
39 | arbitrarily large and still won't get it read. This means the only |
40 | question is how large are the ELF and program header combined. The |
41 | ELF header 32-bit files is 52 bytes long and in 64-bit files is 64 |
42 | bytes long. Each program header entry is again 32 and 56 bytes |
43 | long respectively. I.e., even with a file which has 10 program |
44 | header entries we only have to read 372B/624B respectively. Add to |
45 | this a bit of margin for program notes and reading 512B and 832B |
46 | for 32-bit and 64-bit files respecitvely is enough. If this |
47 | heuristic should really fail for some file the code in |
48 | `_dl_map_object_from_fd' knows how to recover. */ |
49 | struct filebuf |
50 | { |
51 | ssize_t len; |
52 | #if __WORDSIZE == 32 |
53 | # define FILEBUF_SIZE 512 |
54 | #else |
55 | # define FILEBUF_SIZE 832 |
56 | #endif |
57 | char buf[FILEBUF_SIZE] __attribute__ ((aligned (__alignof (ElfW(Ehdr))))); |
58 | }; |
59 | |
60 | #include "dynamic-link.h" |
61 | #include <abi-tag.h> |
62 | #include <stackinfo.h> |
63 | #include <sysdep.h> |
64 | #include <stap-probe.h> |
65 | #include <libc-pointer-arith.h> |
66 | #include <array_length.h> |
67 | |
68 | #include <dl-dst.h> |
69 | #include <dl-load.h> |
70 | #include <dl-map-segments.h> |
71 | #include <dl-unmap-segments.h> |
72 | #include <dl-machine-reject-phdr.h> |
73 | #include <dl-sysdep-open.h> |
74 | #include <dl-prop.h> |
75 | #include <not-cancel.h> |
76 | |
77 | #include <endian.h> |
78 | #if BYTE_ORDER == BIG_ENDIAN |
79 | # define byteorder ELFDATA2MSB |
80 | #elif BYTE_ORDER == LITTLE_ENDIAN |
81 | # define byteorder ELFDATA2LSB |
82 | #else |
83 | # error "Unknown BYTE_ORDER " BYTE_ORDER |
84 | # define byteorder ELFDATANONE |
85 | #endif |
86 | |
87 | #define STRING(x) __STRING (x) |
88 | |
89 | |
90 | int __stack_prot attribute_hidden attribute_relro |
91 | #if _STACK_GROWS_DOWN && defined PROT_GROWSDOWN |
92 | = PROT_GROWSDOWN; |
93 | #elif _STACK_GROWS_UP && defined PROT_GROWSUP |
94 | = PROT_GROWSUP; |
95 | #else |
96 | = 0; |
97 | #endif |
98 | |
99 | |
100 | /* This is the decomposed LD_LIBRARY_PATH search path. */ |
101 | struct r_search_path_struct __rtld_env_path_list attribute_relro; |
102 | |
103 | /* List of the hardware capabilities we might end up using. */ |
104 | #ifdef SHARED |
105 | static const struct r_strlenpair *capstr attribute_relro; |
106 | static size_t ncapstr attribute_relro; |
107 | static size_t max_capstrlen attribute_relro; |
108 | #else |
109 | enum { ncapstr = 1, max_capstrlen = 0 }; |
110 | #endif |
111 | |
112 | |
113 | /* Get the generated information about the trusted directories. Use |
114 | an array of concatenated strings to avoid relocations. See |
115 | gen-trusted-dirs.awk. */ |
116 | #include "trusted-dirs.h" |
117 | |
118 | static const char system_dirs[] = SYSTEM_DIRS; |
119 | static const size_t system_dirs_len[] = |
120 | { |
121 | SYSTEM_DIRS_LEN |
122 | }; |
123 | #define nsystem_dirs_len array_length (system_dirs_len) |
124 | |
125 | static bool |
126 | is_trusted_path_normalize (const char *path, size_t len) |
127 | { |
128 | if (len == 0) |
129 | return false; |
130 | |
131 | char *npath = (char *) alloca (len + 2); |
132 | char *wnp = npath; |
133 | while (*path != '\0') |
134 | { |
135 | if (path[0] == '/') |
136 | { |
137 | if (path[1] == '.') |
138 | { |
139 | if (path[2] == '.' && (path[3] == '/' || path[3] == '\0')) |
140 | { |
141 | while (wnp > npath && *--wnp != '/') |
142 | ; |
143 | path += 3; |
144 | continue; |
145 | } |
146 | else if (path[2] == '/' || path[2] == '\0') |
147 | { |
148 | path += 2; |
149 | continue; |
150 | } |
151 | } |
152 | |
153 | if (wnp > npath && wnp[-1] == '/') |
154 | { |
155 | ++path; |
156 | continue; |
157 | } |
158 | } |
159 | |
160 | *wnp++ = *path++; |
161 | } |
162 | |
163 | if (wnp == npath || wnp[-1] != '/') |
164 | *wnp++ = '/'; |
165 | |
166 | const char *trun = system_dirs; |
167 | |
168 | for (size_t idx = 0; idx < nsystem_dirs_len; ++idx) |
169 | { |
170 | if (wnp - npath >= system_dirs_len[idx] |
171 | && memcmp (trun, npath, system_dirs_len[idx]) == 0) |
172 | /* Found it. */ |
173 | return true; |
174 | |
175 | trun += system_dirs_len[idx] + 1; |
176 | } |
177 | |
178 | return false; |
179 | } |
180 | |
181 | /* Given a substring starting at INPUT, just after the DST '$' start |
182 | token, determine if INPUT contains DST token REF, following the |
183 | ELF gABI rules for DSTs: |
184 | |
185 | * Longest possible sequence using the rules (greedy). |
186 | |
187 | * Must start with a $ (enforced by caller). |
188 | |
189 | * Must follow $ with one underscore or ASCII [A-Za-z] (caller |
190 | follows these rules for REF) or '{' (start curly quoted name). |
191 | |
192 | * Must follow first two characters with zero or more [A-Za-z0-9_] |
193 | (enforced by caller) or '}' (end curly quoted name). |
194 | |
195 | If the sequence is a DST matching REF then the length of the DST |
196 | (excluding the $ sign but including curly braces, if any) is |
197 | returned, otherwise 0. */ |
198 | static size_t |
199 | is_dst (const char *input, const char *ref) |
200 | { |
201 | bool is_curly = false; |
202 | |
203 | /* Is a ${...} input sequence? */ |
204 | if (input[0] == '{') |
205 | { |
206 | is_curly = true; |
207 | ++input; |
208 | } |
209 | |
210 | /* Check for matching name, following closing curly brace (if |
211 | required), or trailing characters which are part of an |
212 | identifier. */ |
213 | size_t rlen = strlen (ref); |
214 | if (strncmp (input, ref, rlen) != 0 |
215 | || (is_curly && input[rlen] != '}') |
216 | || ((input[rlen] >= 'A' && input[rlen] <= 'Z') |
217 | || (input[rlen] >= 'a' && input[rlen] <= 'z') |
218 | || (input[rlen] >= '0' && input[rlen] <= '9') |
219 | || (input[rlen] == '_'))) |
220 | return 0; |
221 | |
222 | if (is_curly) |
223 | /* Count the two curly braces. */ |
224 | return rlen + 2; |
225 | else |
226 | return rlen; |
227 | } |
228 | |
229 | /* INPUT should be the start of a path e.g DT_RPATH or name e.g. |
230 | DT_NEEDED. The return value is the number of known DSTs found. We |
231 | count all known DSTs regardless of __libc_enable_secure; the caller |
232 | is responsible for enforcing the security of the substitution rules |
233 | (usually _dl_dst_substitute). */ |
234 | size_t |
235 | _dl_dst_count (const char *input) |
236 | { |
237 | size_t cnt = 0; |
238 | |
239 | input = strchr (input, '$'); |
240 | |
241 | /* Most likely there is no DST. */ |
242 | if (__glibc_likely (input == NULL)) |
243 | return 0; |
244 | |
245 | do |
246 | { |
247 | size_t len; |
248 | |
249 | ++input; |
250 | /* All DSTs must follow ELF gABI rules, see is_dst (). */ |
251 | if ((len = is_dst (input, "ORIGIN" )) != 0 |
252 | || (len = is_dst (input, "PLATFORM" )) != 0 |
253 | || (len = is_dst (input, "LIB" )) != 0) |
254 | ++cnt; |
255 | |
256 | /* There may be more than one DST in the input. */ |
257 | input = strchr (input + len, '$'); |
258 | } |
259 | while (input != NULL); |
260 | |
261 | return cnt; |
262 | } |
263 | |
264 | /* Process INPUT for DSTs and store in RESULT using the information |
265 | from link map L to resolve the DSTs. This function only handles one |
266 | path at a time and does not handle colon-separated path lists (see |
267 | fillin_rpath ()). Lastly the size of result in bytes should be at |
268 | least equal to the value returned by DL_DST_REQUIRED. Note that it |
269 | is possible for a DT_NEEDED, DT_AUXILIARY, and DT_FILTER entries to |
270 | have colons, but we treat those as literal colons here, not as path |
271 | list delimeters. */ |
272 | char * |
273 | _dl_dst_substitute (struct link_map *l, const char *input, char *result) |
274 | { |
275 | /* Copy character-by-character from input into the working pointer |
276 | looking for any DSTs. We track the start of input and if we are |
277 | going to check for trusted paths, all of which are part of $ORIGIN |
278 | handling in SUID/SGID cases (see below). In some cases, like when |
279 | a DST cannot be replaced, we may set result to an empty string and |
280 | return. */ |
281 | char *wp = result; |
282 | const char *start = input; |
283 | bool check_for_trusted = false; |
284 | |
285 | do |
286 | { |
287 | if (__glibc_unlikely (*input == '$')) |
288 | { |
289 | const char *repl = NULL; |
290 | size_t len; |
291 | |
292 | ++input; |
293 | if ((len = is_dst (input, "ORIGIN" )) != 0) |
294 | { |
295 | /* For SUID/GUID programs we normally ignore the path with |
296 | $ORIGIN in DT_RUNPATH, or DT_RPATH. However, there is |
297 | one exception to this rule, and it is: |
298 | |
299 | * $ORIGIN appears as the first path element, and is |
300 | the only string in the path or is immediately |
301 | followed by a path separator and the rest of the |
302 | path, |
303 | |
304 | and ... |
305 | |
306 | * The path is rooted in a trusted directory. |
307 | |
308 | This exception allows such programs to reference |
309 | shared libraries in subdirectories of trusted |
310 | directories. The use case is one of general |
311 | organization and deployment flexibility. |
312 | Trusted directories are usually such paths as "/lib64" |
313 | or "/usr/lib64", and the usual RPATHs take the form of |
314 | [$ORIGIN/../$LIB/somedir]. */ |
315 | if (__glibc_unlikely (__libc_enable_secure) |
316 | && !(input == start + 1 |
317 | && (input[len] == '\0' || input[len] == '/'))) |
318 | repl = (const char *) -1; |
319 | else |
320 | repl = l->l_origin; |
321 | |
322 | check_for_trusted = (__libc_enable_secure |
323 | && l->l_type == lt_executable); |
324 | } |
325 | else if ((len = is_dst (input, "PLATFORM" )) != 0) |
326 | repl = GLRO(dl_platform); |
327 | else if ((len = is_dst (input, "LIB" )) != 0) |
328 | repl = DL_DST_LIB; |
329 | |
330 | if (repl != NULL && repl != (const char *) -1) |
331 | { |
332 | wp = __stpcpy (wp, repl); |
333 | input += len; |
334 | } |
335 | else if (len != 0) |
336 | { |
337 | /* We found a valid DST that we know about, but we could |
338 | not find a replacement value for it, therefore we |
339 | cannot use this path and discard it. */ |
340 | *result = '\0'; |
341 | return result; |
342 | } |
343 | else |
344 | /* No DST we recognize. */ |
345 | *wp++ = '$'; |
346 | } |
347 | else |
348 | { |
349 | *wp++ = *input++; |
350 | } |
351 | } |
352 | while (*input != '\0'); |
353 | |
354 | /* In SUID/SGID programs, after $ORIGIN expansion the normalized |
355 | path must be rooted in one of the trusted directories. The $LIB |
356 | and $PLATFORM DST cannot in any way be manipulated by the caller |
357 | because they are fixed values that are set by the dynamic loader |
358 | and therefore any paths using just $LIB or $PLATFORM need not be |
359 | checked for trust, the authors of the binaries themselves are |
360 | trusted to have designed this correctly. Only $ORIGIN is tested in |
361 | this way because it may be manipulated in some ways with hard |
362 | links. */ |
363 | if (__glibc_unlikely (check_for_trusted) |
364 | && !is_trusted_path_normalize (result, wp - result)) |
365 | { |
366 | *result = '\0'; |
367 | return result; |
368 | } |
369 | |
370 | *wp = '\0'; |
371 | |
372 | return result; |
373 | } |
374 | |
375 | |
376 | /* Return a malloc allocated copy of INPUT with all recognized DSTs |
377 | replaced. On some platforms it might not be possible to determine the |
378 | path from which the object belonging to the map is loaded. In this |
379 | case the path containing the DST is left out. On error NULL |
380 | is returned. */ |
381 | static char * |
382 | expand_dynamic_string_token (struct link_map *l, const char *input) |
383 | { |
384 | /* We make two runs over the string. First we determine how large the |
385 | resulting string is and then we copy it over. Since this is no |
386 | frequently executed operation we are looking here not for performance |
387 | but rather for code size. */ |
388 | size_t cnt; |
389 | size_t total; |
390 | char *result; |
391 | |
392 | /* Determine the number of DSTs. */ |
393 | cnt = _dl_dst_count (input); |
394 | |
395 | /* If we do not have to replace anything simply copy the string. */ |
396 | if (__glibc_likely (cnt == 0)) |
397 | return __strdup (input); |
398 | |
399 | /* Determine the length of the substituted string. */ |
400 | total = DL_DST_REQUIRED (l, input, strlen (input), cnt); |
401 | |
402 | /* Allocate the necessary memory. */ |
403 | result = (char *) malloc (total + 1); |
404 | if (result == NULL) |
405 | return NULL; |
406 | |
407 | return _dl_dst_substitute (l, input, result); |
408 | } |
409 | |
410 | |
411 | /* Add `name' to the list of names for a particular shared object. |
412 | `name' is expected to have been allocated with malloc and will |
413 | be freed if the shared object already has this name. |
414 | Returns false if the object already had this name. */ |
415 | static void |
416 | add_name_to_object (struct link_map *l, const char *name) |
417 | { |
418 | struct libname_list *lnp, *lastp; |
419 | struct libname_list *newname; |
420 | size_t name_len; |
421 | |
422 | lastp = NULL; |
423 | for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next) |
424 | if (strcmp (name, lnp->name) == 0) |
425 | return; |
426 | |
427 | name_len = strlen (name) + 1; |
428 | newname = (struct libname_list *) malloc (sizeof *newname + name_len); |
429 | if (newname == NULL) |
430 | { |
431 | /* No more memory. */ |
432 | _dl_signal_error (ENOMEM, name, NULL, N_("cannot allocate name record" )); |
433 | return; |
434 | } |
435 | /* The object should have a libname set from _dl_new_object. */ |
436 | assert (lastp != NULL); |
437 | |
438 | newname->name = memcpy (newname + 1, name, name_len); |
439 | newname->next = NULL; |
440 | newname->dont_free = 0; |
441 | /* CONCURRENCY NOTES: |
442 | |
443 | Make sure the initialization of newname happens before its address is |
444 | read from the lastp->next store below. |
445 | |
446 | GL(dl_load_lock) is held here (and by other writers, e.g. dlclose), so |
447 | readers of libname_list->next (e.g. _dl_check_caller or the reads above) |
448 | can use that for synchronization, however the read in _dl_name_match_p |
449 | may be executed without holding the lock during _dl_runtime_resolve |
450 | (i.e. lazy symbol resolution when a function of library l is called). |
451 | |
452 | The release MO store below synchronizes with the acquire MO load in |
453 | _dl_name_match_p. Other writes need to synchronize with that load too, |
454 | however those happen either early when the process is single threaded |
455 | (dl_main) or when the library is unloaded (dlclose) and the user has to |
456 | synchronize library calls with unloading. */ |
457 | atomic_store_release (&lastp->next, newname); |
458 | } |
459 | |
460 | /* Standard search directories. */ |
461 | struct r_search_path_struct __rtld_search_dirs attribute_relro; |
462 | |
463 | static size_t max_dirnamelen; |
464 | |
465 | static struct r_search_path_elem ** |
466 | fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep, |
467 | const char *what, const char *where, struct link_map *l) |
468 | { |
469 | char *cp; |
470 | size_t nelems = 0; |
471 | |
472 | while ((cp = __strsep (&rpath, sep)) != NULL) |
473 | { |
474 | struct r_search_path_elem *dirp; |
475 | char *to_free = NULL; |
476 | size_t len = 0; |
477 | |
478 | /* `strsep' can pass an empty string. */ |
479 | if (*cp != '\0') |
480 | { |
481 | to_free = cp = expand_dynamic_string_token (l, cp); |
482 | |
483 | /* expand_dynamic_string_token can return NULL in case of empty |
484 | path or memory allocation failure. */ |
485 | if (cp == NULL) |
486 | continue; |
487 | |
488 | /* Compute the length after dynamic string token expansion and |
489 | ignore empty paths. */ |
490 | len = strlen (cp); |
491 | if (len == 0) |
492 | { |
493 | free (to_free); |
494 | continue; |
495 | } |
496 | |
497 | /* Remove trailing slashes (except for "/"). */ |
498 | while (len > 1 && cp[len - 1] == '/') |
499 | --len; |
500 | |
501 | /* Now add one if there is none so far. */ |
502 | if (len > 0 && cp[len - 1] != '/') |
503 | cp[len++] = '/'; |
504 | } |
505 | |
506 | /* See if this directory is already known. */ |
507 | for (dirp = GL(dl_all_dirs); dirp != NULL; dirp = dirp->next) |
508 | if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0) |
509 | break; |
510 | |
511 | if (dirp != NULL) |
512 | { |
513 | /* It is available, see whether it's on our own list. */ |
514 | size_t cnt; |
515 | for (cnt = 0; cnt < nelems; ++cnt) |
516 | if (result[cnt] == dirp) |
517 | break; |
518 | |
519 | if (cnt == nelems) |
520 | result[nelems++] = dirp; |
521 | } |
522 | else |
523 | { |
524 | size_t cnt; |
525 | enum r_dir_status init_val; |
526 | size_t where_len = where ? strlen (where) + 1 : 0; |
527 | |
528 | /* It's a new directory. Create an entry and add it. */ |
529 | dirp = (struct r_search_path_elem *) |
530 | malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status) |
531 | + where_len + len + 1); |
532 | if (dirp == NULL) |
533 | _dl_signal_error (ENOMEM, NULL, NULL, |
534 | N_("cannot create cache for search path" )); |
535 | |
536 | dirp->dirname = ((char *) dirp + sizeof (*dirp) |
537 | + ncapstr * sizeof (enum r_dir_status)); |
538 | *((char *) __mempcpy ((char *) dirp->dirname, cp, len)) = '\0'; |
539 | dirp->dirnamelen = len; |
540 | |
541 | if (len > max_dirnamelen) |
542 | max_dirnamelen = len; |
543 | |
544 | /* We have to make sure all the relative directories are |
545 | never ignored. The current directory might change and |
546 | all our saved information would be void. */ |
547 | init_val = cp[0] != '/' ? existing : unknown; |
548 | for (cnt = 0; cnt < ncapstr; ++cnt) |
549 | dirp->status[cnt] = init_val; |
550 | |
551 | dirp->what = what; |
552 | if (__glibc_likely (where != NULL)) |
553 | dirp->where = memcpy ((char *) dirp + sizeof (*dirp) + len + 1 |
554 | + (ncapstr * sizeof (enum r_dir_status)), |
555 | where, where_len); |
556 | else |
557 | dirp->where = NULL; |
558 | |
559 | dirp->next = GL(dl_all_dirs); |
560 | GL(dl_all_dirs) = dirp; |
561 | |
562 | /* Put it in the result array. */ |
563 | result[nelems++] = dirp; |
564 | } |
565 | free (to_free); |
566 | } |
567 | |
568 | /* Terminate the array. */ |
569 | result[nelems] = NULL; |
570 | |
571 | return result; |
572 | } |
573 | |
574 | |
575 | static bool |
576 | decompose_rpath (struct r_search_path_struct *sps, |
577 | const char *rpath, struct link_map *l, const char *what) |
578 | { |
579 | /* Make a copy we can work with. */ |
580 | const char *where = l->l_name; |
581 | char *cp; |
582 | struct r_search_path_elem **result; |
583 | size_t nelems; |
584 | /* Initialize to please the compiler. */ |
585 | const char *errstring = NULL; |
586 | |
587 | /* First see whether we must forget the RUNPATH and RPATH from this |
588 | object. */ |
589 | if (__glibc_unlikely (GLRO(dl_inhibit_rpath) != NULL) |
590 | && !__libc_enable_secure) |
591 | { |
592 | const char *inhp = GLRO(dl_inhibit_rpath); |
593 | |
594 | do |
595 | { |
596 | const char *wp = where; |
597 | |
598 | while (*inhp == *wp && *wp != '\0') |
599 | { |
600 | ++inhp; |
601 | ++wp; |
602 | } |
603 | |
604 | if (*wp == '\0' && (*inhp == '\0' || *inhp == ':')) |
605 | { |
606 | /* This object is on the list of objects for which the |
607 | RUNPATH and RPATH must not be used. */ |
608 | sps->dirs = (void *) -1; |
609 | return false; |
610 | } |
611 | |
612 | while (*inhp != '\0') |
613 | if (*inhp++ == ':') |
614 | break; |
615 | } |
616 | while (*inhp != '\0'); |
617 | } |
618 | |
619 | /* Ignore empty rpaths. */ |
620 | if (*rpath == '\0') |
621 | { |
622 | sps->dirs = (struct r_search_path_elem **) -1; |
623 | return false; |
624 | } |
625 | |
626 | /* Make a writable copy. */ |
627 | char *copy = __strdup (rpath); |
628 | if (copy == NULL) |
629 | { |
630 | errstring = N_("cannot create RUNPATH/RPATH copy" ); |
631 | goto signal_error; |
632 | } |
633 | |
634 | /* Count the number of necessary elements in the result array. */ |
635 | nelems = 0; |
636 | for (cp = copy; *cp != '\0'; ++cp) |
637 | if (*cp == ':') |
638 | ++nelems; |
639 | |
640 | /* Allocate room for the result. NELEMS + 1 is an upper limit for the |
641 | number of necessary entries. */ |
642 | result = (struct r_search_path_elem **) malloc ((nelems + 1 + 1) |
643 | * sizeof (*result)); |
644 | if (result == NULL) |
645 | { |
646 | free (copy); |
647 | errstring = N_("cannot create cache for search path" ); |
648 | signal_error: |
649 | _dl_signal_error (ENOMEM, NULL, NULL, errstring); |
650 | } |
651 | |
652 | fillin_rpath (copy, result, ":" , what, where, l); |
653 | |
654 | /* Free the copied RPATH string. `fillin_rpath' make own copies if |
655 | necessary. */ |
656 | free (copy); |
657 | |
658 | /* There is no path after expansion. */ |
659 | if (result[0] == NULL) |
660 | { |
661 | free (result); |
662 | sps->dirs = (struct r_search_path_elem **) -1; |
663 | return false; |
664 | } |
665 | |
666 | sps->dirs = result; |
667 | /* The caller will change this value if we haven't used a real malloc. */ |
668 | sps->malloced = 1; |
669 | return true; |
670 | } |
671 | |
672 | /* Make sure cached path information is stored in *SP |
673 | and return true if there are any paths to search there. */ |
674 | static bool |
675 | cache_rpath (struct link_map *l, |
676 | struct r_search_path_struct *sp, |
677 | int tag, |
678 | const char *what) |
679 | { |
680 | if (sp->dirs == (void *) -1) |
681 | return false; |
682 | |
683 | if (sp->dirs != NULL) |
684 | return true; |
685 | |
686 | if (l->l_info[tag] == NULL) |
687 | { |
688 | /* There is no path. */ |
689 | sp->dirs = (void *) -1; |
690 | return false; |
691 | } |
692 | |
693 | /* Make sure the cache information is available. */ |
694 | return decompose_rpath (sp, (const char *) (D_PTR (l, l_info[DT_STRTAB]) |
695 | + l->l_info[tag]->d_un.d_val), |
696 | l, what); |
697 | } |
698 | |
699 | |
700 | void |
701 | _dl_init_paths (const char *llp, const char *source, |
702 | const char *glibc_hwcaps_prepend, |
703 | const char *glibc_hwcaps_mask) |
704 | { |
705 | size_t idx; |
706 | const char *strp; |
707 | struct r_search_path_elem *pelem, **aelem; |
708 | size_t round_size; |
709 | struct link_map __attribute__ ((unused)) *l = NULL; |
710 | /* Initialize to please the compiler. */ |
711 | const char *errstring = NULL; |
712 | |
713 | /* Fill in the information about the application's RPATH and the |
714 | directories addressed by the LD_LIBRARY_PATH environment variable. */ |
715 | |
716 | #ifdef SHARED |
717 | /* Get the capabilities. */ |
718 | capstr = _dl_important_hwcaps (glibc_hwcaps_prepend, glibc_hwcaps_mask, |
719 | &ncapstr, &max_capstrlen); |
720 | #endif |
721 | |
722 | /* First set up the rest of the default search directory entries. */ |
723 | aelem = __rtld_search_dirs.dirs = (struct r_search_path_elem **) |
724 | malloc ((nsystem_dirs_len + 1) * sizeof (struct r_search_path_elem *)); |
725 | if (__rtld_search_dirs.dirs == NULL) |
726 | { |
727 | errstring = N_("cannot create search path array" ); |
728 | signal_error: |
729 | _dl_signal_error (ENOMEM, NULL, NULL, errstring); |
730 | } |
731 | |
732 | round_size = ((2 * sizeof (struct r_search_path_elem) - 1 |
733 | + ncapstr * sizeof (enum r_dir_status)) |
734 | / sizeof (struct r_search_path_elem)); |
735 | |
736 | __rtld_search_dirs.dirs[0] |
737 | = malloc (nsystem_dirs_len * round_size |
738 | * sizeof (*__rtld_search_dirs.dirs[0])); |
739 | if (__rtld_search_dirs.dirs[0] == NULL) |
740 | { |
741 | errstring = N_("cannot create cache for search path" ); |
742 | goto signal_error; |
743 | } |
744 | |
745 | __rtld_search_dirs.malloced = 0; |
746 | pelem = GL(dl_all_dirs) = __rtld_search_dirs.dirs[0]; |
747 | strp = system_dirs; |
748 | idx = 0; |
749 | |
750 | do |
751 | { |
752 | size_t cnt; |
753 | |
754 | *aelem++ = pelem; |
755 | |
756 | pelem->what = "system search path" ; |
757 | pelem->where = NULL; |
758 | |
759 | pelem->dirname = strp; |
760 | pelem->dirnamelen = system_dirs_len[idx]; |
761 | strp += system_dirs_len[idx] + 1; |
762 | |
763 | /* System paths must be absolute. */ |
764 | assert (pelem->dirname[0] == '/'); |
765 | for (cnt = 0; cnt < ncapstr; ++cnt) |
766 | pelem->status[cnt] = unknown; |
767 | |
768 | pelem->next = (++idx == nsystem_dirs_len ? NULL : (pelem + round_size)); |
769 | |
770 | pelem += round_size; |
771 | } |
772 | while (idx < nsystem_dirs_len); |
773 | |
774 | max_dirnamelen = SYSTEM_DIRS_MAX_LEN; |
775 | *aelem = NULL; |
776 | |
777 | /* This points to the map of the main object. If there is no main |
778 | object (e.g., under --help, use the dynamic loader itself as a |
779 | stand-in. */ |
780 | l = GL(dl_ns)[LM_ID_BASE]._ns_loaded; |
781 | #ifdef SHARED |
782 | if (l == NULL) |
783 | l = &GL (dl_rtld_map); |
784 | #endif |
785 | assert (l->l_type != lt_loaded); |
786 | |
787 | if (l->l_info[DT_RUNPATH]) |
788 | { |
789 | /* Allocate room for the search path and fill in information |
790 | from RUNPATH. */ |
791 | decompose_rpath (&l->l_runpath_dirs, |
792 | (const void *) (D_PTR (l, l_info[DT_STRTAB]) |
793 | + l->l_info[DT_RUNPATH]->d_un.d_val), |
794 | l, "RUNPATH" ); |
795 | /* During rtld init the memory is allocated by the stub malloc, |
796 | prevent any attempt to free it by the normal malloc. */ |
797 | l->l_runpath_dirs.malloced = 0; |
798 | |
799 | /* The RPATH is ignored. */ |
800 | l->l_rpath_dirs.dirs = (void *) -1; |
801 | } |
802 | else |
803 | { |
804 | l->l_runpath_dirs.dirs = (void *) -1; |
805 | |
806 | if (l->l_info[DT_RPATH]) |
807 | { |
808 | /* Allocate room for the search path and fill in information |
809 | from RPATH. */ |
810 | decompose_rpath (&l->l_rpath_dirs, |
811 | (const void *) (D_PTR (l, l_info[DT_STRTAB]) |
812 | + l->l_info[DT_RPATH]->d_un.d_val), |
813 | l, "RPATH" ); |
814 | /* During rtld init the memory is allocated by the stub |
815 | malloc, prevent any attempt to free it by the normal |
816 | malloc. */ |
817 | l->l_rpath_dirs.malloced = 0; |
818 | } |
819 | else |
820 | l->l_rpath_dirs.dirs = (void *) -1; |
821 | } |
822 | |
823 | if (llp != NULL && *llp != '\0') |
824 | { |
825 | char *llp_tmp = strdupa (llp); |
826 | |
827 | /* Decompose the LD_LIBRARY_PATH contents. First determine how many |
828 | elements it has. */ |
829 | size_t nllp = 1; |
830 | for (const char *cp = llp_tmp; *cp != '\0'; ++cp) |
831 | if (*cp == ':' || *cp == ';') |
832 | ++nllp; |
833 | |
834 | __rtld_env_path_list.dirs = (struct r_search_path_elem **) |
835 | malloc ((nllp + 1) * sizeof (struct r_search_path_elem *)); |
836 | if (__rtld_env_path_list.dirs == NULL) |
837 | { |
838 | errstring = N_("cannot create cache for search path" ); |
839 | goto signal_error; |
840 | } |
841 | |
842 | (void) fillin_rpath (llp_tmp, __rtld_env_path_list.dirs, ":;" , |
843 | source, NULL, l); |
844 | |
845 | if (__rtld_env_path_list.dirs[0] == NULL) |
846 | { |
847 | free (__rtld_env_path_list.dirs); |
848 | __rtld_env_path_list.dirs = (void *) -1; |
849 | } |
850 | |
851 | __rtld_env_path_list.malloced = 0; |
852 | } |
853 | else |
854 | __rtld_env_path_list.dirs = (void *) -1; |
855 | } |
856 | |
857 | |
858 | /* Process PT_GNU_PROPERTY program header PH in module L after |
859 | PT_LOAD segments are mapped. Only one NT_GNU_PROPERTY_TYPE_0 |
860 | note is handled which contains processor specific properties. |
861 | FD is -1 for the kernel mapped main executable otherwise it is |
862 | the fd used for loading module L. */ |
863 | |
864 | void |
865 | _dl_process_pt_gnu_property (struct link_map *l, int fd, const ElfW(Phdr) *ph) |
866 | { |
867 | const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr); |
868 | const ElfW(Addr) size = ph->p_memsz; |
869 | const ElfW(Addr) align = ph->p_align; |
870 | |
871 | /* The NT_GNU_PROPERTY_TYPE_0 note must be aligned to 4 bytes in |
872 | 32-bit objects and to 8 bytes in 64-bit objects. Skip notes |
873 | with incorrect alignment. */ |
874 | if (align != (__ELF_NATIVE_CLASS / 8)) |
875 | return; |
876 | |
877 | const ElfW(Addr) start = (ElfW(Addr)) note; |
878 | unsigned int last_type = 0; |
879 | |
880 | while ((ElfW(Addr)) (note + 1) - start < size) |
881 | { |
882 | /* Find the NT_GNU_PROPERTY_TYPE_0 note. */ |
883 | if (note->n_namesz == 4 |
884 | && note->n_type == NT_GNU_PROPERTY_TYPE_0 |
885 | && memcmp (note + 1, "GNU" , 4) == 0) |
886 | { |
887 | /* Check for invalid property. */ |
888 | if (note->n_descsz < 8 |
889 | || (note->n_descsz % sizeof (ElfW(Addr))) != 0) |
890 | return; |
891 | |
892 | /* Start and end of property array. */ |
893 | unsigned char *ptr = (unsigned char *) (note + 1) + 4; |
894 | unsigned char *ptr_end = ptr + note->n_descsz; |
895 | |
896 | do |
897 | { |
898 | unsigned int type = *(unsigned int *) ptr; |
899 | unsigned int datasz = *(unsigned int *) (ptr + 4); |
900 | |
901 | /* Property type must be in ascending order. */ |
902 | if (type < last_type) |
903 | return; |
904 | |
905 | ptr += 8; |
906 | if ((ptr + datasz) > ptr_end) |
907 | return; |
908 | |
909 | last_type = type; |
910 | |
911 | /* Target specific property processing. */ |
912 | if (_dl_process_gnu_property (l, fd, type, datasz, ptr) == 0) |
913 | return; |
914 | |
915 | /* Check the next property item. */ |
916 | ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr))); |
917 | } |
918 | while ((ptr_end - ptr) >= 8); |
919 | |
920 | /* Only handle one NT_GNU_PROPERTY_TYPE_0. */ |
921 | return; |
922 | } |
923 | |
924 | note = ((const void *) note |
925 | + ELF_NOTE_NEXT_OFFSET (note->n_namesz, note->n_descsz, |
926 | align)); |
927 | } |
928 | } |
929 | |
930 | |
931 | /* Map in the shared object NAME, actually located in REALNAME, and already |
932 | opened on FD. */ |
933 | |
934 | #ifndef EXTERNAL_MAP_FROM_FD |
935 | static |
936 | #endif |
937 | struct link_map * |
938 | _dl_map_object_from_fd (const char *name, const char *origname, int fd, |
939 | struct filebuf *fbp, char *realname, |
940 | struct link_map *loader, int l_type, int mode, |
941 | void **stack_endp, Lmid_t nsid) |
942 | { |
943 | struct link_map *l = NULL; |
944 | const ElfW(Ehdr) *; |
945 | const ElfW(Phdr) *phdr; |
946 | const ElfW(Phdr) *ph; |
947 | size_t maplength; |
948 | int type; |
949 | /* Initialize to keep the compiler happy. */ |
950 | const char *errstring = NULL; |
951 | int errval = 0; |
952 | struct r_debug *r = _dl_debug_initialize (0, nsid); |
953 | bool make_consistent = false; |
954 | |
955 | /* Get file information. To match the kernel behavior, do not fill |
956 | in this information for the executable in case of an explicit |
957 | loader invocation. */ |
958 | struct r_file_id id; |
959 | if (mode & __RTLD_OPENEXEC) |
960 | { |
961 | assert (nsid == LM_ID_BASE); |
962 | memset (&id, 0, sizeof (id)); |
963 | } |
964 | else |
965 | { |
966 | if (__glibc_unlikely (!_dl_get_file_id (fd, &id))) |
967 | { |
968 | errstring = N_("cannot stat shared object" ); |
969 | lose_errno: |
970 | errval = errno; |
971 | lose: |
972 | /* The file might already be closed. */ |
973 | if (fd != -1) |
974 | __close_nocancel (fd); |
975 | if (l != NULL && l->l_map_start != 0) |
976 | _dl_unmap_segments (l); |
977 | if (l != NULL && l->l_origin != (char *) -1l) |
978 | free ((char *) l->l_origin); |
979 | if (l != NULL && !l->l_libname->dont_free) |
980 | free (l->l_libname); |
981 | if (l != NULL && l->l_phdr_allocated) |
982 | free ((void *) l->l_phdr); |
983 | free (l); |
984 | free (realname); |
985 | |
986 | if (make_consistent && r != NULL) |
987 | { |
988 | r->r_state = RT_CONSISTENT; |
989 | _dl_debug_state (); |
990 | LIBC_PROBE (map_failed, 2, nsid, r); |
991 | } |
992 | |
993 | _dl_signal_error (errval, name, NULL, errstring); |
994 | } |
995 | |
996 | /* Look again to see if the real name matched another already loaded. */ |
997 | for (l = GL(dl_ns)[nsid]._ns_loaded; l != NULL; l = l->l_next) |
998 | if (!l->l_removed && _dl_file_id_match_p (&l->l_file_id, &id)) |
999 | { |
1000 | /* The object is already loaded. |
1001 | Just bump its reference count and return it. */ |
1002 | __close_nocancel (fd); |
1003 | |
1004 | /* If the name is not in the list of names for this object add |
1005 | it. */ |
1006 | free (realname); |
1007 | add_name_to_object (l, name); |
1008 | |
1009 | return l; |
1010 | } |
1011 | } |
1012 | |
1013 | #ifdef SHARED |
1014 | /* When loading into a namespace other than the base one we must |
1015 | avoid loading ld.so since there can only be one copy. Ever. */ |
1016 | if (__glibc_unlikely (nsid != LM_ID_BASE) |
1017 | && (_dl_file_id_match_p (&id, &GL(dl_rtld_map).l_file_id) |
1018 | || _dl_name_match_p (name, &GL(dl_rtld_map)))) |
1019 | { |
1020 | /* This is indeed ld.so. Create a new link_map which refers to |
1021 | the real one for almost everything. */ |
1022 | l = _dl_new_object (realname, name, l_type, loader, mode, nsid); |
1023 | if (l == NULL) |
1024 | goto fail_new; |
1025 | |
1026 | /* Refer to the real descriptor. */ |
1027 | l->l_real = &GL(dl_rtld_map); |
1028 | |
1029 | /* No need to bump the refcount of the real object, ld.so will |
1030 | never be unloaded. */ |
1031 | __close_nocancel (fd); |
1032 | |
1033 | /* Add the map for the mirrored object to the object list. */ |
1034 | _dl_add_to_namespace_list (l, nsid); |
1035 | |
1036 | return l; |
1037 | } |
1038 | #endif |
1039 | |
1040 | if (mode & RTLD_NOLOAD) |
1041 | { |
1042 | /* We are not supposed to load the object unless it is already |
1043 | loaded. So return now. */ |
1044 | free (realname); |
1045 | __close_nocancel (fd); |
1046 | return NULL; |
1047 | } |
1048 | |
1049 | /* Print debugging message. */ |
1050 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)) |
1051 | _dl_debug_printf ("file=%s [%lu]; generating link map\n" , name, nsid); |
1052 | |
1053 | /* This is the ELF header. We read it in `open_verify'. */ |
1054 | header = (void *) fbp->buf; |
1055 | |
1056 | /* Signal that we are going to add new objects. */ |
1057 | if (r->r_state == RT_CONSISTENT) |
1058 | { |
1059 | #ifdef SHARED |
1060 | /* Auditing checkpoint: we are going to add new objects. */ |
1061 | if ((mode & __RTLD_AUDIT) == 0 |
1062 | && __glibc_unlikely (GLRO(dl_naudit) > 0)) |
1063 | { |
1064 | struct link_map *head = GL(dl_ns)[nsid]._ns_loaded; |
1065 | /* Do not call the functions for any auditing object. */ |
1066 | if (head->l_auditing == 0) |
1067 | { |
1068 | struct audit_ifaces *afct = GLRO(dl_audit); |
1069 | for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) |
1070 | { |
1071 | if (afct->activity != NULL) |
1072 | afct->activity (&link_map_audit_state (head, cnt)->cookie, |
1073 | LA_ACT_ADD); |
1074 | |
1075 | afct = afct->next; |
1076 | } |
1077 | } |
1078 | } |
1079 | #endif |
1080 | |
1081 | /* Notify the debugger we have added some objects. We need to |
1082 | call _dl_debug_initialize in a static program in case dynamic |
1083 | linking has not been used before. */ |
1084 | r->r_state = RT_ADD; |
1085 | _dl_debug_state (); |
1086 | LIBC_PROBE (map_start, 2, nsid, r); |
1087 | make_consistent = true; |
1088 | } |
1089 | else |
1090 | assert (r->r_state == RT_ADD); |
1091 | |
1092 | /* Enter the new object in the list of loaded objects. */ |
1093 | l = _dl_new_object (realname, name, l_type, loader, mode, nsid); |
1094 | if (__glibc_unlikely (l == NULL)) |
1095 | { |
1096 | #ifdef SHARED |
1097 | fail_new: |
1098 | #endif |
1099 | errstring = N_("cannot create shared object descriptor" ); |
1100 | goto lose_errno; |
1101 | } |
1102 | |
1103 | /* Extract the remaining details we need from the ELF header |
1104 | and then read in the program header table. */ |
1105 | l->l_entry = header->e_entry; |
1106 | type = header->e_type; |
1107 | l->l_phnum = header->e_phnum; |
1108 | |
1109 | maplength = header->e_phnum * sizeof (ElfW(Phdr)); |
1110 | if (header->e_phoff + maplength <= (size_t) fbp->len) |
1111 | phdr = (void *) (fbp->buf + header->e_phoff); |
1112 | else |
1113 | { |
1114 | phdr = alloca (maplength); |
1115 | if ((size_t) __pread64_nocancel (fd, (void *) phdr, maplength, |
1116 | header->e_phoff) != maplength) |
1117 | { |
1118 | errstring = N_("cannot read file data" ); |
1119 | goto lose_errno; |
1120 | } |
1121 | } |
1122 | |
1123 | /* On most platforms presume that PT_GNU_STACK is absent and the stack is |
1124 | * executable. Other platforms default to a nonexecutable stack and don't |
1125 | * need PT_GNU_STACK to do so. */ |
1126 | uint_fast16_t stack_flags = DEFAULT_STACK_PERMS; |
1127 | |
1128 | { |
1129 | /* Scan the program header table, collecting its load commands. */ |
1130 | struct loadcmd loadcmds[l->l_phnum]; |
1131 | size_t nloadcmds = 0; |
1132 | bool has_holes = false; |
1133 | |
1134 | /* The struct is initialized to zero so this is not necessary: |
1135 | l->l_ld = 0; |
1136 | l->l_phdr = 0; |
1137 | l->l_addr = 0; */ |
1138 | for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph) |
1139 | switch (ph->p_type) |
1140 | { |
1141 | /* These entries tell us where to find things once the file's |
1142 | segments are mapped in. We record the addresses it says |
1143 | verbatim, and later correct for the run-time load address. */ |
1144 | case PT_DYNAMIC: |
1145 | if (ph->p_filesz) |
1146 | { |
1147 | /* Debuginfo only files from "objcopy --only-keep-debug" |
1148 | contain a PT_DYNAMIC segment with p_filesz == 0. Skip |
1149 | such a segment to avoid a crash later. */ |
1150 | l->l_ld = (void *) ph->p_vaddr; |
1151 | l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn)); |
1152 | } |
1153 | break; |
1154 | |
1155 | case PT_PHDR: |
1156 | l->l_phdr = (void *) ph->p_vaddr; |
1157 | break; |
1158 | |
1159 | case PT_LOAD: |
1160 | /* A load command tells us to map in part of the file. |
1161 | We record the load commands and process them all later. */ |
1162 | if (__glibc_unlikely ((ph->p_align & (GLRO(dl_pagesize) - 1)) != 0)) |
1163 | { |
1164 | errstring = N_("ELF load command alignment not page-aligned" ); |
1165 | goto lose; |
1166 | } |
1167 | if (__glibc_unlikely (((ph->p_vaddr - ph->p_offset) |
1168 | & (ph->p_align - 1)) != 0)) |
1169 | { |
1170 | errstring |
1171 | = N_("ELF load command address/offset not properly aligned" ); |
1172 | goto lose; |
1173 | } |
1174 | |
1175 | struct loadcmd *c = &loadcmds[nloadcmds++]; |
1176 | c->mapstart = ALIGN_DOWN (ph->p_vaddr, GLRO(dl_pagesize)); |
1177 | c->mapend = ALIGN_UP (ph->p_vaddr + ph->p_filesz, GLRO(dl_pagesize)); |
1178 | c->dataend = ph->p_vaddr + ph->p_filesz; |
1179 | c->allocend = ph->p_vaddr + ph->p_memsz; |
1180 | c->mapoff = ALIGN_DOWN (ph->p_offset, GLRO(dl_pagesize)); |
1181 | |
1182 | /* Determine whether there is a gap between the last segment |
1183 | and this one. */ |
1184 | if (nloadcmds > 1 && c[-1].mapend != c->mapstart) |
1185 | has_holes = true; |
1186 | |
1187 | /* Optimize a common case. */ |
1188 | #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7 |
1189 | c->prot = (PF_TO_PROT |
1190 | >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf; |
1191 | #else |
1192 | c->prot = 0; |
1193 | if (ph->p_flags & PF_R) |
1194 | c->prot |= PROT_READ; |
1195 | if (ph->p_flags & PF_W) |
1196 | c->prot |= PROT_WRITE; |
1197 | if (ph->p_flags & PF_X) |
1198 | c->prot |= PROT_EXEC; |
1199 | #endif |
1200 | break; |
1201 | |
1202 | case PT_TLS: |
1203 | if (ph->p_memsz == 0) |
1204 | /* Nothing to do for an empty segment. */ |
1205 | break; |
1206 | |
1207 | l->l_tls_blocksize = ph->p_memsz; |
1208 | l->l_tls_align = ph->p_align; |
1209 | if (ph->p_align == 0) |
1210 | l->l_tls_firstbyte_offset = 0; |
1211 | else |
1212 | l->l_tls_firstbyte_offset = ph->p_vaddr & (ph->p_align - 1); |
1213 | l->l_tls_initimage_size = ph->p_filesz; |
1214 | /* Since we don't know the load address yet only store the |
1215 | offset. We will adjust it later. */ |
1216 | l->l_tls_initimage = (void *) ph->p_vaddr; |
1217 | |
1218 | /* l->l_tls_modid is assigned below, once there is no |
1219 | possibility for failure. */ |
1220 | |
1221 | if (l->l_type != lt_library |
1222 | && GL(dl_tls_dtv_slotinfo_list) == NULL) |
1223 | { |
1224 | #ifdef SHARED |
1225 | /* We are loading the executable itself when the dynamic |
1226 | linker was executed directly. The setup will happen |
1227 | later. */ |
1228 | assert (l->l_prev == NULL || (mode & __RTLD_AUDIT) != 0); |
1229 | #else |
1230 | assert (false && "TLS not initialized in static application" ); |
1231 | #endif |
1232 | } |
1233 | break; |
1234 | |
1235 | case PT_GNU_STACK: |
1236 | stack_flags = ph->p_flags; |
1237 | break; |
1238 | |
1239 | case PT_GNU_RELRO: |
1240 | l->l_relro_addr = ph->p_vaddr; |
1241 | l->l_relro_size = ph->p_memsz; |
1242 | break; |
1243 | } |
1244 | |
1245 | if (__glibc_unlikely (nloadcmds == 0)) |
1246 | { |
1247 | /* This only happens for a bogus object that will be caught with |
1248 | another error below. But we don't want to go through the |
1249 | calculations below using NLOADCMDS - 1. */ |
1250 | errstring = N_("object file has no loadable segments" ); |
1251 | goto lose; |
1252 | } |
1253 | |
1254 | /* dlopen of an executable is not valid because it is not possible |
1255 | to perform proper relocations, handle static TLS, or run the |
1256 | ELF constructors. For PIE, the check needs the dynamic |
1257 | section, so there is another check below. */ |
1258 | if (__glibc_unlikely (type != ET_DYN) |
1259 | && __glibc_unlikely ((mode & __RTLD_OPENEXEC) == 0)) |
1260 | { |
1261 | /* This object is loaded at a fixed address. This must never |
1262 | happen for objects loaded with dlopen. */ |
1263 | errstring = N_("cannot dynamically load executable" ); |
1264 | goto lose; |
1265 | } |
1266 | |
1267 | /* Length of the sections to be loaded. */ |
1268 | maplength = loadcmds[nloadcmds - 1].allocend - loadcmds[0].mapstart; |
1269 | |
1270 | /* Now process the load commands and map segments into memory. |
1271 | This is responsible for filling in: |
1272 | l_map_start, l_map_end, l_addr, l_contiguous, l_text_end, l_phdr |
1273 | */ |
1274 | errstring = _dl_map_segments (l, fd, header, type, loadcmds, nloadcmds, |
1275 | maplength, has_holes, loader); |
1276 | if (__glibc_unlikely (errstring != NULL)) |
1277 | { |
1278 | /* Mappings can be in an inconsistent state: avoid unmap. */ |
1279 | l->l_map_start = l->l_map_end = 0; |
1280 | goto lose; |
1281 | } |
1282 | } |
1283 | |
1284 | if (l->l_ld == 0) |
1285 | { |
1286 | if (__glibc_unlikely (type == ET_DYN)) |
1287 | { |
1288 | errstring = N_("object file has no dynamic section" ); |
1289 | goto lose; |
1290 | } |
1291 | } |
1292 | else |
1293 | l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr); |
1294 | |
1295 | elf_get_dynamic_info (l, NULL); |
1296 | |
1297 | /* Make sure we are not dlopen'ing an object that has the |
1298 | DF_1_NOOPEN flag set, or a PIE object. */ |
1299 | if ((__glibc_unlikely (l->l_flags_1 & DF_1_NOOPEN) |
1300 | && (mode & __RTLD_DLOPEN)) |
1301 | || (__glibc_unlikely (l->l_flags_1 & DF_1_PIE) |
1302 | && __glibc_unlikely ((mode & __RTLD_OPENEXEC) == 0))) |
1303 | { |
1304 | if (l->l_flags_1 & DF_1_PIE) |
1305 | errstring |
1306 | = N_("cannot dynamically load position-independent executable" ); |
1307 | else |
1308 | errstring = N_("shared object cannot be dlopen()ed" ); |
1309 | goto lose; |
1310 | } |
1311 | |
1312 | if (l->l_phdr == NULL) |
1313 | { |
1314 | /* The program header is not contained in any of the segments. |
1315 | We have to allocate memory ourself and copy it over from out |
1316 | temporary place. */ |
1317 | ElfW(Phdr) *newp = (ElfW(Phdr) *) malloc (header->e_phnum |
1318 | * sizeof (ElfW(Phdr))); |
1319 | if (newp == NULL) |
1320 | { |
1321 | errstring = N_("cannot allocate memory for program header" ); |
1322 | goto lose_errno; |
1323 | } |
1324 | |
1325 | l->l_phdr = memcpy (newp, phdr, |
1326 | (header->e_phnum * sizeof (ElfW(Phdr)))); |
1327 | l->l_phdr_allocated = 1; |
1328 | } |
1329 | else |
1330 | /* Adjust the PT_PHDR value by the runtime load address. */ |
1331 | l->l_phdr = (ElfW(Phdr) *) ((ElfW(Addr)) l->l_phdr + l->l_addr); |
1332 | |
1333 | if (__glibc_unlikely ((stack_flags &~ GL(dl_stack_flags)) & PF_X)) |
1334 | { |
1335 | /* The stack is presently not executable, but this module |
1336 | requires that it be executable. We must change the |
1337 | protection of the variable which contains the flags used in |
1338 | the mprotect calls. */ |
1339 | #ifdef SHARED |
1340 | if ((mode & (__RTLD_DLOPEN | __RTLD_AUDIT)) == __RTLD_DLOPEN) |
1341 | { |
1342 | const uintptr_t p = (uintptr_t) &__stack_prot & -GLRO(dl_pagesize); |
1343 | const size_t s = (uintptr_t) (&__stack_prot + 1) - p; |
1344 | |
1345 | struct link_map *const m = &GL(dl_rtld_map); |
1346 | const uintptr_t relro_end = ((m->l_addr + m->l_relro_addr |
1347 | + m->l_relro_size) |
1348 | & -GLRO(dl_pagesize)); |
1349 | if (__glibc_likely (p + s <= relro_end)) |
1350 | { |
1351 | /* The variable lies in the region protected by RELRO. */ |
1352 | if (__mprotect ((void *) p, s, PROT_READ|PROT_WRITE) < 0) |
1353 | { |
1354 | errstring = N_("cannot change memory protections" ); |
1355 | goto lose_errno; |
1356 | } |
1357 | __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC; |
1358 | __mprotect ((void *) p, s, PROT_READ); |
1359 | } |
1360 | else |
1361 | __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC; |
1362 | } |
1363 | else |
1364 | #endif |
1365 | __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC; |
1366 | |
1367 | #ifdef check_consistency |
1368 | check_consistency (); |
1369 | #endif |
1370 | |
1371 | #if PTHREAD_IN_LIBC |
1372 | errval = _dl_make_stacks_executable (stack_endp); |
1373 | #else |
1374 | errval = (*GL(dl_make_stack_executable_hook)) (stack_endp); |
1375 | #endif |
1376 | if (errval) |
1377 | { |
1378 | errstring = N_("\ |
1379 | cannot enable executable stack as shared object requires" ); |
1380 | goto lose; |
1381 | } |
1382 | } |
1383 | |
1384 | /* Adjust the address of the TLS initialization image. */ |
1385 | if (l->l_tls_initimage != NULL) |
1386 | l->l_tls_initimage = (char *) l->l_tls_initimage + l->l_addr; |
1387 | |
1388 | /* Process program headers again after load segments are mapped in |
1389 | case processing requires accessing those segments. Scan program |
1390 | headers backward so that PT_NOTE can be skipped if PT_GNU_PROPERTY |
1391 | exits. */ |
1392 | for (ph = &l->l_phdr[l->l_phnum]; ph != l->l_phdr; --ph) |
1393 | switch (ph[-1].p_type) |
1394 | { |
1395 | case PT_NOTE: |
1396 | _dl_process_pt_note (l, fd, &ph[-1]); |
1397 | break; |
1398 | case PT_GNU_PROPERTY: |
1399 | _dl_process_pt_gnu_property (l, fd, &ph[-1]); |
1400 | break; |
1401 | } |
1402 | |
1403 | /* We are done mapping in the file. We no longer need the descriptor. */ |
1404 | if (__glibc_unlikely (__close_nocancel (fd) != 0)) |
1405 | { |
1406 | errstring = N_("cannot close file descriptor" ); |
1407 | goto lose_errno; |
1408 | } |
1409 | /* Signal that we closed the file. */ |
1410 | fd = -1; |
1411 | |
1412 | /* Failures before this point are handled locally via lose. |
1413 | There are no more failures in this function until return, |
1414 | to change that the cleanup handling needs to be updated. */ |
1415 | |
1416 | /* If this is ET_EXEC, we should have loaded it as lt_executable. */ |
1417 | assert (type != ET_EXEC || l->l_type == lt_executable); |
1418 | |
1419 | l->l_entry += l->l_addr; |
1420 | |
1421 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)) |
1422 | _dl_debug_printf ("\ |
1423 | dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*Zx\n\ |
1424 | entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n" , |
1425 | (int) sizeof (void *) * 2, |
1426 | (unsigned long int) l->l_ld, |
1427 | (int) sizeof (void *) * 2, |
1428 | (unsigned long int) l->l_addr, |
1429 | (int) sizeof (void *) * 2, maplength, |
1430 | (int) sizeof (void *) * 2, |
1431 | (unsigned long int) l->l_entry, |
1432 | (int) sizeof (void *) * 2, |
1433 | (unsigned long int) l->l_phdr, |
1434 | (int) sizeof (void *) * 2, l->l_phnum); |
1435 | |
1436 | /* Set up the symbol hash table. */ |
1437 | _dl_setup_hash (l); |
1438 | |
1439 | /* If this object has DT_SYMBOLIC set modify now its scope. We don't |
1440 | have to do this for the main map. */ |
1441 | if ((mode & RTLD_DEEPBIND) == 0 |
1442 | && __glibc_unlikely (l->l_info[DT_SYMBOLIC] != NULL) |
1443 | && &l->l_searchlist != l->l_scope[0]) |
1444 | { |
1445 | /* Create an appropriate searchlist. It contains only this map. |
1446 | This is the definition of DT_SYMBOLIC in SysVr4. */ |
1447 | l->l_symbolic_searchlist.r_list[0] = l; |
1448 | l->l_symbolic_searchlist.r_nlist = 1; |
1449 | |
1450 | /* Now move the existing entries one back. */ |
1451 | memmove (&l->l_scope[1], &l->l_scope[0], |
1452 | (l->l_scope_max - 1) * sizeof (l->l_scope[0])); |
1453 | |
1454 | /* Now add the new entry. */ |
1455 | l->l_scope[0] = &l->l_symbolic_searchlist; |
1456 | } |
1457 | |
1458 | /* Remember whether this object must be initialized first. */ |
1459 | if (l->l_flags_1 & DF_1_INITFIRST) |
1460 | GL(dl_initfirst) = l; |
1461 | |
1462 | /* Finally the file information. */ |
1463 | l->l_file_id = id; |
1464 | |
1465 | #ifdef SHARED |
1466 | /* When auditing is used the recorded names might not include the |
1467 | name by which the DSO is actually known. Add that as well. */ |
1468 | if (__glibc_unlikely (origname != NULL)) |
1469 | add_name_to_object (l, origname); |
1470 | #else |
1471 | /* Audit modules only exist when linking is dynamic so ORIGNAME |
1472 | cannot be non-NULL. */ |
1473 | assert (origname == NULL); |
1474 | #endif |
1475 | |
1476 | /* When we profile the SONAME might be needed for something else but |
1477 | loading. Add it right away. */ |
1478 | if (__glibc_unlikely (GLRO(dl_profile) != NULL) |
1479 | && l->l_info[DT_SONAME] != NULL) |
1480 | add_name_to_object (l, ((const char *) D_PTR (l, l_info[DT_STRTAB]) |
1481 | + l->l_info[DT_SONAME]->d_un.d_val)); |
1482 | |
1483 | /* If we have newly loaded libc.so, update the namespace |
1484 | description. */ |
1485 | if (GL(dl_ns)[nsid].libc_map == NULL |
1486 | && l->l_info[DT_SONAME] != NULL |
1487 | && strcmp (((const char *) D_PTR (l, l_info[DT_STRTAB]) |
1488 | + l->l_info[DT_SONAME]->d_un.d_val), LIBC_SO) == 0) |
1489 | GL(dl_ns)[nsid].libc_map = l; |
1490 | |
1491 | /* _dl_close can only eventually undo the module ID assignment (via |
1492 | remove_slotinfo) if this function returns a pointer to a link |
1493 | map. Therefore, delay this step until all possibilities for |
1494 | failure have been excluded. */ |
1495 | if (l->l_tls_blocksize > 0 |
1496 | && (__glibc_likely (l->l_type == lt_library) |
1497 | /* If GL(dl_tls_dtv_slotinfo_list) == NULL, then rtld.c did |
1498 | not set up TLS data structures, so don't use them now. */ |
1499 | || __glibc_likely (GL(dl_tls_dtv_slotinfo_list) != NULL))) |
1500 | /* Assign the next available module ID. */ |
1501 | _dl_assign_tls_modid (l); |
1502 | |
1503 | #ifdef DL_AFTER_LOAD |
1504 | DL_AFTER_LOAD (l); |
1505 | #endif |
1506 | |
1507 | /* Now that the object is fully initialized add it to the object list. */ |
1508 | _dl_add_to_namespace_list (l, nsid); |
1509 | |
1510 | #ifdef SHARED |
1511 | /* Auditing checkpoint: we have a new object. */ |
1512 | if (__glibc_unlikely (GLRO(dl_naudit) > 0) |
1513 | && !GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing) |
1514 | { |
1515 | struct audit_ifaces *afct = GLRO(dl_audit); |
1516 | for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) |
1517 | { |
1518 | if (afct->objopen != NULL) |
1519 | { |
1520 | struct auditstate *state = link_map_audit_state (l, cnt); |
1521 | state->bindflags = afct->objopen (l, nsid, &state->cookie); |
1522 | l->l_audit_any_plt |= state->bindflags != 0; |
1523 | } |
1524 | |
1525 | afct = afct->next; |
1526 | } |
1527 | } |
1528 | #endif |
1529 | |
1530 | return l; |
1531 | } |
1532 | |
1533 | /* Print search path. */ |
1534 | static void |
1535 | print_search_path (struct r_search_path_elem **list, |
1536 | const char *what, const char *name) |
1537 | { |
1538 | char buf[max_dirnamelen + max_capstrlen]; |
1539 | int first = 1; |
1540 | |
1541 | _dl_debug_printf (" search path=" ); |
1542 | |
1543 | while (*list != NULL && (*list)->what == what) /* Yes, ==. */ |
1544 | { |
1545 | char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen); |
1546 | size_t cnt; |
1547 | |
1548 | for (cnt = 0; cnt < ncapstr; ++cnt) |
1549 | if ((*list)->status[cnt] != nonexisting) |
1550 | { |
1551 | #ifdef SHARED |
1552 | char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len); |
1553 | if (cp == buf || (cp == buf + 1 && buf[0] == '/')) |
1554 | cp[0] = '\0'; |
1555 | else |
1556 | cp[-1] = '\0'; |
1557 | #else |
1558 | *endp = '\0'; |
1559 | #endif |
1560 | |
1561 | _dl_debug_printf_c (first ? "%s" : ":%s" , buf); |
1562 | first = 0; |
1563 | } |
1564 | |
1565 | ++list; |
1566 | } |
1567 | |
1568 | if (name != NULL) |
1569 | _dl_debug_printf_c ("\t\t(%s from file %s)\n" , what, |
1570 | DSO_FILENAME (name)); |
1571 | else |
1572 | _dl_debug_printf_c ("\t\t(%s)\n" , what); |
1573 | } |
1574 | |
1575 | /* Open a file and verify it is an ELF file for this architecture. We |
1576 | ignore only ELF files for other architectures. Non-ELF files and |
1577 | ELF files with different header information cause fatal errors since |
1578 | this could mean there is something wrong in the installation and the |
1579 | user might want to know about this. |
1580 | |
1581 | If FD is not -1, then the file is already open and FD refers to it. |
1582 | In that case, FD is consumed for both successful and error returns. */ |
1583 | static int |
1584 | open_verify (const char *name, int fd, |
1585 | struct filebuf *fbp, struct link_map *loader, |
1586 | int whatcode, int mode, bool *found_other_class, bool free_name) |
1587 | { |
1588 | /* This is the expected ELF header. */ |
1589 | #define ELF32_CLASS ELFCLASS32 |
1590 | #define ELF64_CLASS ELFCLASS64 |
1591 | #ifndef VALID_ELF_HEADER |
1592 | # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0) |
1593 | # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV) |
1594 | # define VALID_ELF_ABIVERSION(osabi,ver) (ver == 0) |
1595 | #elif defined MORE_ELF_HEADER_DATA |
1596 | MORE_ELF_HEADER_DATA; |
1597 | #endif |
1598 | static const unsigned char expected[EI_NIDENT] = |
1599 | { |
1600 | [EI_MAG0] = ELFMAG0, |
1601 | [EI_MAG1] = ELFMAG1, |
1602 | [EI_MAG2] = ELFMAG2, |
1603 | [EI_MAG3] = ELFMAG3, |
1604 | [EI_CLASS] = ELFW(CLASS), |
1605 | [EI_DATA] = byteorder, |
1606 | [EI_VERSION] = EV_CURRENT, |
1607 | [EI_OSABI] = ELFOSABI_SYSV, |
1608 | [EI_ABIVERSION] = 0 |
1609 | }; |
1610 | static const struct |
1611 | { |
1612 | ElfW(Word) vendorlen; |
1613 | ElfW(Word) datalen; |
1614 | ElfW(Word) type; |
1615 | char vendor[4]; |
1616 | } expected_note = { 4, 16, 1, "GNU" }; |
1617 | /* Initialize it to make the compiler happy. */ |
1618 | const char *errstring = NULL; |
1619 | int errval = 0; |
1620 | |
1621 | #ifdef SHARED |
1622 | /* Give the auditing libraries a chance. */ |
1623 | if (__glibc_unlikely (GLRO(dl_naudit) > 0) && whatcode != 0 |
1624 | && loader->l_auditing == 0) |
1625 | { |
1626 | const char *original_name = name; |
1627 | struct audit_ifaces *afct = GLRO(dl_audit); |
1628 | for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) |
1629 | { |
1630 | if (afct->objsearch != NULL) |
1631 | { |
1632 | struct auditstate *state = link_map_audit_state (loader, cnt); |
1633 | name = afct->objsearch (name, &state->cookie, whatcode); |
1634 | if (name == NULL) |
1635 | /* Ignore the path. */ |
1636 | return -1; |
1637 | } |
1638 | |
1639 | afct = afct->next; |
1640 | } |
1641 | |
1642 | if (fd != -1 && name != original_name && strcmp (name, original_name)) |
1643 | { |
1644 | /* An audit library changed what we're supposed to open, |
1645 | so FD no longer matches it. */ |
1646 | __close_nocancel (fd); |
1647 | fd = -1; |
1648 | } |
1649 | } |
1650 | #endif |
1651 | |
1652 | if (fd == -1) |
1653 | /* Open the file. We always open files read-only. */ |
1654 | fd = __open64_nocancel (name, O_RDONLY | O_CLOEXEC); |
1655 | |
1656 | if (fd != -1) |
1657 | { |
1658 | ElfW(Ehdr) *ehdr; |
1659 | ElfW(Phdr) *phdr, *ph; |
1660 | ElfW(Word) *abi_note; |
1661 | ElfW(Word) *abi_note_malloced = NULL; |
1662 | unsigned int osversion; |
1663 | size_t maplength; |
1664 | |
1665 | /* We successfully opened the file. Now verify it is a file |
1666 | we can use. */ |
1667 | __set_errno (0); |
1668 | fbp->len = 0; |
1669 | assert (sizeof (fbp->buf) > sizeof (ElfW(Ehdr))); |
1670 | /* Read in the header. */ |
1671 | do |
1672 | { |
1673 | ssize_t retlen = __read_nocancel (fd, fbp->buf + fbp->len, |
1674 | sizeof (fbp->buf) - fbp->len); |
1675 | if (retlen <= 0) |
1676 | break; |
1677 | fbp->len += retlen; |
1678 | } |
1679 | while (__glibc_unlikely (fbp->len < sizeof (ElfW(Ehdr)))); |
1680 | |
1681 | /* This is where the ELF header is loaded. */ |
1682 | ehdr = (ElfW(Ehdr) *) fbp->buf; |
1683 | |
1684 | /* Now run the tests. */ |
1685 | if (__glibc_unlikely (fbp->len < (ssize_t) sizeof (ElfW(Ehdr)))) |
1686 | { |
1687 | errval = errno; |
1688 | errstring = (errval == 0 |
1689 | ? N_("file too short" ) : N_("cannot read file data" )); |
1690 | lose: |
1691 | if (free_name) |
1692 | { |
1693 | char *realname = (char *) name; |
1694 | name = strdupa (realname); |
1695 | free (realname); |
1696 | } |
1697 | __close_nocancel (fd); |
1698 | _dl_signal_error (errval, name, NULL, errstring); |
1699 | } |
1700 | |
1701 | /* See whether the ELF header is what we expect. */ |
1702 | if (__glibc_unlikely (! VALID_ELF_HEADER (ehdr->e_ident, expected, |
1703 | EI_ABIVERSION) |
1704 | || !VALID_ELF_ABIVERSION (ehdr->e_ident[EI_OSABI], |
1705 | ehdr->e_ident[EI_ABIVERSION]) |
1706 | || memcmp (&ehdr->e_ident[EI_PAD], |
1707 | &expected[EI_PAD], |
1708 | EI_NIDENT - EI_PAD) != 0)) |
1709 | { |
1710 | /* Something is wrong. */ |
1711 | const Elf32_Word *magp = (const void *) ehdr->e_ident; |
1712 | if (*magp != |
1713 | #if BYTE_ORDER == LITTLE_ENDIAN |
1714 | ((ELFMAG0 << (EI_MAG0 * 8)) |
1715 | | (ELFMAG1 << (EI_MAG1 * 8)) |
1716 | | (ELFMAG2 << (EI_MAG2 * 8)) |
1717 | | (ELFMAG3 << (EI_MAG3 * 8))) |
1718 | #else |
1719 | ((ELFMAG0 << (EI_MAG3 * 8)) |
1720 | | (ELFMAG1 << (EI_MAG2 * 8)) |
1721 | | (ELFMAG2 << (EI_MAG1 * 8)) |
1722 | | (ELFMAG3 << (EI_MAG0 * 8))) |
1723 | #endif |
1724 | ) |
1725 | errstring = N_("invalid ELF header" ); |
1726 | else if (ehdr->e_ident[EI_CLASS] != ELFW(CLASS)) |
1727 | { |
1728 | /* This is not a fatal error. On architectures where |
1729 | 32-bit and 64-bit binaries can be run this might |
1730 | happen. */ |
1731 | *found_other_class = true; |
1732 | goto close_and_out; |
1733 | } |
1734 | else if (ehdr->e_ident[EI_DATA] != byteorder) |
1735 | { |
1736 | if (BYTE_ORDER == BIG_ENDIAN) |
1737 | errstring = N_("ELF file data encoding not big-endian" ); |
1738 | else |
1739 | errstring = N_("ELF file data encoding not little-endian" ); |
1740 | } |
1741 | else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT) |
1742 | errstring |
1743 | = N_("ELF file version ident does not match current one" ); |
1744 | /* XXX We should be able so set system specific versions which are |
1745 | allowed here. */ |
1746 | else if (!VALID_ELF_OSABI (ehdr->e_ident[EI_OSABI])) |
1747 | errstring = N_("ELF file OS ABI invalid" ); |
1748 | else if (!VALID_ELF_ABIVERSION (ehdr->e_ident[EI_OSABI], |
1749 | ehdr->e_ident[EI_ABIVERSION])) |
1750 | errstring = N_("ELF file ABI version invalid" ); |
1751 | else if (memcmp (&ehdr->e_ident[EI_PAD], &expected[EI_PAD], |
1752 | EI_NIDENT - EI_PAD) != 0) |
1753 | errstring = N_("nonzero padding in e_ident" ); |
1754 | else |
1755 | /* Otherwise we don't know what went wrong. */ |
1756 | errstring = N_("internal error" ); |
1757 | |
1758 | goto lose; |
1759 | } |
1760 | |
1761 | if (__glibc_unlikely (ehdr->e_version != EV_CURRENT)) |
1762 | { |
1763 | errstring = N_("ELF file version does not match current one" ); |
1764 | goto lose; |
1765 | } |
1766 | if (! __glibc_likely (elf_machine_matches_host (ehdr))) |
1767 | goto close_and_out; |
1768 | else if (__glibc_unlikely (ehdr->e_type != ET_DYN |
1769 | && ehdr->e_type != ET_EXEC)) |
1770 | { |
1771 | errstring = N_("only ET_DYN and ET_EXEC can be loaded" ); |
1772 | goto lose; |
1773 | } |
1774 | else if (__glibc_unlikely (ehdr->e_phentsize != sizeof (ElfW(Phdr)))) |
1775 | { |
1776 | errstring = N_("ELF file's phentsize not the expected size" ); |
1777 | goto lose; |
1778 | } |
1779 | |
1780 | maplength = ehdr->e_phnum * sizeof (ElfW(Phdr)); |
1781 | if (ehdr->e_phoff + maplength <= (size_t) fbp->len) |
1782 | phdr = (void *) (fbp->buf + ehdr->e_phoff); |
1783 | else |
1784 | { |
1785 | phdr = alloca (maplength); |
1786 | if ((size_t) __pread64_nocancel (fd, (void *) phdr, maplength, |
1787 | ehdr->e_phoff) != maplength) |
1788 | { |
1789 | read_error: |
1790 | errval = errno; |
1791 | errstring = N_("cannot read file data" ); |
1792 | goto lose; |
1793 | } |
1794 | } |
1795 | |
1796 | if (__glibc_unlikely (elf_machine_reject_phdr_p |
1797 | (phdr, ehdr->e_phnum, fbp->buf, fbp->len, |
1798 | loader, fd))) |
1799 | goto close_and_out; |
1800 | |
1801 | /* Check .note.ABI-tag if present. */ |
1802 | for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph) |
1803 | if (ph->p_type == PT_NOTE && ph->p_filesz >= 32 |
1804 | && (ph->p_align == 4 || ph->p_align == 8)) |
1805 | { |
1806 | ElfW(Addr) size = ph->p_filesz; |
1807 | |
1808 | if (ph->p_offset + size <= (size_t) fbp->len) |
1809 | abi_note = (void *) (fbp->buf + ph->p_offset); |
1810 | else |
1811 | { |
1812 | /* Note: __libc_use_alloca is not usable here, because |
1813 | thread info may not have been set up yet. */ |
1814 | if (size < __MAX_ALLOCA_CUTOFF) |
1815 | abi_note = alloca (size); |
1816 | else |
1817 | { |
1818 | /* There could be multiple PT_NOTEs. */ |
1819 | abi_note_malloced = realloc (abi_note_malloced, size); |
1820 | if (abi_note_malloced == NULL) |
1821 | goto read_error; |
1822 | |
1823 | abi_note = abi_note_malloced; |
1824 | } |
1825 | if (__pread64_nocancel (fd, (void *) abi_note, size, |
1826 | ph->p_offset) != size) |
1827 | { |
1828 | free (abi_note_malloced); |
1829 | goto read_error; |
1830 | } |
1831 | } |
1832 | |
1833 | while (memcmp (abi_note, &expected_note, sizeof (expected_note))) |
1834 | { |
1835 | ElfW(Addr) note_size |
1836 | = ELF_NOTE_NEXT_OFFSET (abi_note[0], abi_note[1], |
1837 | ph->p_align); |
1838 | |
1839 | if (size - 32 < note_size) |
1840 | { |
1841 | size = 0; |
1842 | break; |
1843 | } |
1844 | size -= note_size; |
1845 | abi_note = (void *) abi_note + note_size; |
1846 | } |
1847 | |
1848 | if (size == 0) |
1849 | continue; |
1850 | |
1851 | osversion = (abi_note[5] & 0xff) * 65536 |
1852 | + (abi_note[6] & 0xff) * 256 |
1853 | + (abi_note[7] & 0xff); |
1854 | if (abi_note[4] != __ABI_TAG_OS |
1855 | || (GLRO(dl_osversion) && GLRO(dl_osversion) < osversion)) |
1856 | { |
1857 | close_and_out: |
1858 | __close_nocancel (fd); |
1859 | __set_errno (ENOENT); |
1860 | fd = -1; |
1861 | } |
1862 | |
1863 | break; |
1864 | } |
1865 | free (abi_note_malloced); |
1866 | } |
1867 | |
1868 | return fd; |
1869 | } |
1870 | |
1871 | /* Try to open NAME in one of the directories in *DIRSP. |
1872 | Return the fd, or -1. If successful, fill in *REALNAME |
1873 | with the malloc'd full directory name. If it turns out |
1874 | that none of the directories in *DIRSP exists, *DIRSP is |
1875 | replaced with (void *) -1, and the old value is free()d |
1876 | if MAY_FREE_DIRS is true. */ |
1877 | |
1878 | static int |
1879 | open_path (const char *name, size_t namelen, int mode, |
1880 | struct r_search_path_struct *sps, char **realname, |
1881 | struct filebuf *fbp, struct link_map *loader, int whatcode, |
1882 | bool *found_other_class) |
1883 | { |
1884 | struct r_search_path_elem **dirs = sps->dirs; |
1885 | char *buf; |
1886 | int fd = -1; |
1887 | const char *current_what = NULL; |
1888 | int any = 0; |
1889 | |
1890 | if (__glibc_unlikely (dirs == NULL)) |
1891 | /* We're called before _dl_init_paths when loading the main executable |
1892 | given on the command line when rtld is run directly. */ |
1893 | return -1; |
1894 | |
1895 | buf = alloca (max_dirnamelen + max_capstrlen + namelen); |
1896 | do |
1897 | { |
1898 | struct r_search_path_elem *this_dir = *dirs; |
1899 | size_t buflen = 0; |
1900 | size_t cnt; |
1901 | char *edp; |
1902 | int here_any = 0; |
1903 | int err; |
1904 | |
1905 | /* If we are debugging the search for libraries print the path |
1906 | now if it hasn't happened now. */ |
1907 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS) |
1908 | && current_what != this_dir->what) |
1909 | { |
1910 | current_what = this_dir->what; |
1911 | print_search_path (dirs, current_what, this_dir->where); |
1912 | } |
1913 | |
1914 | edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen); |
1915 | for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt) |
1916 | { |
1917 | /* Skip this directory if we know it does not exist. */ |
1918 | if (this_dir->status[cnt] == nonexisting) |
1919 | continue; |
1920 | |
1921 | #ifdef SHARED |
1922 | buflen = |
1923 | ((char *) __mempcpy (__mempcpy (edp, capstr[cnt].str, |
1924 | capstr[cnt].len), |
1925 | name, namelen) |
1926 | - buf); |
1927 | #else |
1928 | buflen = (char *) __mempcpy (edp, name, namelen) - buf; |
1929 | #endif |
1930 | |
1931 | /* Print name we try if this is wanted. */ |
1932 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS)) |
1933 | _dl_debug_printf (" trying file=%s\n" , buf); |
1934 | |
1935 | fd = open_verify (buf, -1, fbp, loader, whatcode, mode, |
1936 | found_other_class, false); |
1937 | if (this_dir->status[cnt] == unknown) |
1938 | { |
1939 | if (fd != -1) |
1940 | this_dir->status[cnt] = existing; |
1941 | /* Do not update the directory information when loading |
1942 | auditing code. We must try to disturb the program as |
1943 | little as possible. */ |
1944 | else if (loader == NULL |
1945 | || GL(dl_ns)[loader->l_ns]._ns_loaded->l_auditing == 0) |
1946 | { |
1947 | /* We failed to open machine dependent library. Let's |
1948 | test whether there is any directory at all. */ |
1949 | struct __stat64_t64 st; |
1950 | |
1951 | buf[buflen - namelen - 1] = '\0'; |
1952 | |
1953 | if (__stat64_time64 (buf, &st) != 0 |
1954 | || ! S_ISDIR (st.st_mode)) |
1955 | /* The directory does not exist or it is no directory. */ |
1956 | this_dir->status[cnt] = nonexisting; |
1957 | else |
1958 | this_dir->status[cnt] = existing; |
1959 | } |
1960 | } |
1961 | |
1962 | /* Remember whether we found any existing directory. */ |
1963 | here_any |= this_dir->status[cnt] != nonexisting; |
1964 | |
1965 | if (fd != -1 && __glibc_unlikely (mode & __RTLD_SECURE) |
1966 | && __libc_enable_secure) |
1967 | { |
1968 | /* This is an extra security effort to make sure nobody can |
1969 | preload broken shared objects which are in the trusted |
1970 | directories and so exploit the bugs. */ |
1971 | struct __stat64_t64 st; |
1972 | |
1973 | if (__fstat64_time64 (fd, &st) != 0 |
1974 | || (st.st_mode & S_ISUID) == 0) |
1975 | { |
1976 | /* The shared object cannot be tested for being SUID |
1977 | or this bit is not set. In this case we must not |
1978 | use this object. */ |
1979 | __close_nocancel (fd); |
1980 | fd = -1; |
1981 | /* We simply ignore the file, signal this by setting |
1982 | the error value which would have been set by `open'. */ |
1983 | errno = ENOENT; |
1984 | } |
1985 | } |
1986 | } |
1987 | |
1988 | if (fd != -1) |
1989 | { |
1990 | *realname = (char *) malloc (buflen); |
1991 | if (*realname != NULL) |
1992 | { |
1993 | memcpy (*realname, buf, buflen); |
1994 | return fd; |
1995 | } |
1996 | else |
1997 | { |
1998 | /* No memory for the name, we certainly won't be able |
1999 | to load and link it. */ |
2000 | __close_nocancel (fd); |
2001 | return -1; |
2002 | } |
2003 | } |
2004 | if (here_any && (err = errno) != ENOENT && err != EACCES) |
2005 | /* The file exists and is readable, but something went wrong. */ |
2006 | return -1; |
2007 | |
2008 | /* Remember whether we found anything. */ |
2009 | any |= here_any; |
2010 | } |
2011 | while (*++dirs != NULL); |
2012 | |
2013 | /* Remove the whole path if none of the directories exists. */ |
2014 | if (__glibc_unlikely (! any)) |
2015 | { |
2016 | /* Paths which were allocated using the minimal malloc() in ld.so |
2017 | must not be freed using the general free() in libc. */ |
2018 | if (sps->malloced) |
2019 | free (sps->dirs); |
2020 | |
2021 | /* __rtld_search_dirs and __rtld_env_path_list are |
2022 | attribute_relro, therefore avoid writing to them. */ |
2023 | if (sps != &__rtld_search_dirs && sps != &__rtld_env_path_list) |
2024 | sps->dirs = (void *) -1; |
2025 | } |
2026 | |
2027 | return -1; |
2028 | } |
2029 | |
2030 | /* Map in the shared object file NAME. */ |
2031 | |
2032 | struct link_map * |
2033 | _dl_map_object (struct link_map *loader, const char *name, |
2034 | int type, int trace_mode, int mode, Lmid_t nsid) |
2035 | { |
2036 | int fd; |
2037 | const char *origname = NULL; |
2038 | char *realname; |
2039 | char *name_copy; |
2040 | struct link_map *l; |
2041 | struct filebuf fb; |
2042 | |
2043 | assert (nsid >= 0); |
2044 | assert (nsid < GL(dl_nns)); |
2045 | |
2046 | /* Look for this name among those already loaded. */ |
2047 | for (l = GL(dl_ns)[nsid]._ns_loaded; l; l = l->l_next) |
2048 | { |
2049 | /* If the requested name matches the soname of a loaded object, |
2050 | use that object. Elide this check for names that have not |
2051 | yet been opened. */ |
2052 | if (__glibc_unlikely ((l->l_faked | l->l_removed) != 0)) |
2053 | continue; |
2054 | if (!_dl_name_match_p (name, l)) |
2055 | { |
2056 | const char *soname; |
2057 | |
2058 | if (__glibc_likely (l->l_soname_added) |
2059 | || l->l_info[DT_SONAME] == NULL) |
2060 | continue; |
2061 | |
2062 | soname = ((const char *) D_PTR (l, l_info[DT_STRTAB]) |
2063 | + l->l_info[DT_SONAME]->d_un.d_val); |
2064 | if (strcmp (name, soname) != 0) |
2065 | continue; |
2066 | |
2067 | /* We have a match on a new name -- cache it. */ |
2068 | add_name_to_object (l, soname); |
2069 | l->l_soname_added = 1; |
2070 | } |
2071 | |
2072 | /* We have a match. */ |
2073 | return l; |
2074 | } |
2075 | |
2076 | /* Display information if we are debugging. */ |
2077 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES) |
2078 | && loader != NULL) |
2079 | _dl_debug_printf ((mode & __RTLD_CALLMAP) == 0 |
2080 | ? "\nfile=%s [%lu]; needed by %s [%lu]\n" |
2081 | : "\nfile=%s [%lu]; dynamically loaded by %s [%lu]\n" , |
2082 | name, nsid, DSO_FILENAME (loader->l_name), loader->l_ns); |
2083 | |
2084 | #ifdef SHARED |
2085 | /* Give the auditing libraries a chance to change the name before we |
2086 | try anything. */ |
2087 | if (__glibc_unlikely (GLRO(dl_naudit) > 0) |
2088 | && (loader == NULL || loader->l_auditing == 0)) |
2089 | { |
2090 | struct audit_ifaces *afct = GLRO(dl_audit); |
2091 | for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) |
2092 | { |
2093 | if (afct->objsearch != NULL) |
2094 | { |
2095 | const char *before = name; |
2096 | struct auditstate *state = link_map_audit_state (loader, cnt); |
2097 | name = afct->objsearch (name, &state->cookie, LA_SER_ORIG); |
2098 | if (name == NULL) |
2099 | { |
2100 | /* Do not try anything further. */ |
2101 | fd = -1; |
2102 | goto no_file; |
2103 | } |
2104 | if (before != name && strcmp (before, name) != 0) |
2105 | { |
2106 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)) |
2107 | _dl_debug_printf ("audit changed filename %s -> %s\n" , |
2108 | before, name); |
2109 | |
2110 | if (origname == NULL) |
2111 | origname = before; |
2112 | } |
2113 | } |
2114 | |
2115 | afct = afct->next; |
2116 | } |
2117 | } |
2118 | #endif |
2119 | |
2120 | /* Will be true if we found a DSO which is of the other ELF class. */ |
2121 | bool found_other_class = false; |
2122 | |
2123 | if (strchr (name, '/') == NULL) |
2124 | { |
2125 | /* Search for NAME in several places. */ |
2126 | |
2127 | size_t namelen = strlen (name) + 1; |
2128 | |
2129 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS)) |
2130 | _dl_debug_printf ("find library=%s [%lu]; searching\n" , name, nsid); |
2131 | |
2132 | fd = -1; |
2133 | |
2134 | /* When the object has the RUNPATH information we don't use any |
2135 | RPATHs. */ |
2136 | if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL) |
2137 | { |
2138 | /* This is the executable's map (if there is one). Make sure that |
2139 | we do not look at it twice. */ |
2140 | struct link_map *main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded; |
2141 | bool did_main_map = false; |
2142 | |
2143 | /* First try the DT_RPATH of the dependent object that caused NAME |
2144 | to be loaded. Then that object's dependent, and on up. */ |
2145 | for (l = loader; l; l = l->l_loader) |
2146 | if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH" )) |
2147 | { |
2148 | fd = open_path (name, namelen, mode, |
2149 | &l->l_rpath_dirs, |
2150 | &realname, &fb, loader, LA_SER_RUNPATH, |
2151 | &found_other_class); |
2152 | if (fd != -1) |
2153 | break; |
2154 | |
2155 | did_main_map |= l == main_map; |
2156 | } |
2157 | |
2158 | /* If dynamically linked, try the DT_RPATH of the executable |
2159 | itself. NB: we do this for lookups in any namespace. */ |
2160 | if (fd == -1 && !did_main_map |
2161 | && main_map != NULL && main_map->l_type != lt_loaded |
2162 | && cache_rpath (main_map, &main_map->l_rpath_dirs, DT_RPATH, |
2163 | "RPATH" )) |
2164 | fd = open_path (name, namelen, mode, |
2165 | &main_map->l_rpath_dirs, |
2166 | &realname, &fb, loader ?: main_map, LA_SER_RUNPATH, |
2167 | &found_other_class); |
2168 | } |
2169 | |
2170 | /* Try the LD_LIBRARY_PATH environment variable. */ |
2171 | if (fd == -1 && __rtld_env_path_list.dirs != (void *) -1) |
2172 | fd = open_path (name, namelen, mode, &__rtld_env_path_list, |
2173 | &realname, &fb, |
2174 | loader ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded, |
2175 | LA_SER_LIBPATH, &found_other_class); |
2176 | |
2177 | /* Look at the RUNPATH information for this binary. */ |
2178 | if (fd == -1 && loader != NULL |
2179 | && cache_rpath (loader, &loader->l_runpath_dirs, |
2180 | DT_RUNPATH, "RUNPATH" )) |
2181 | fd = open_path (name, namelen, mode, |
2182 | &loader->l_runpath_dirs, &realname, &fb, loader, |
2183 | LA_SER_RUNPATH, &found_other_class); |
2184 | |
2185 | if (fd == -1) |
2186 | { |
2187 | realname = _dl_sysdep_open_object (name, namelen, &fd); |
2188 | if (realname != NULL) |
2189 | { |
2190 | fd = open_verify (realname, fd, |
2191 | &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded, |
2192 | LA_SER_CONFIG, mode, &found_other_class, |
2193 | false); |
2194 | if (fd == -1) |
2195 | free (realname); |
2196 | } |
2197 | } |
2198 | |
2199 | #ifdef USE_LDCONFIG |
2200 | if (fd == -1 |
2201 | && (__glibc_likely ((mode & __RTLD_SECURE) == 0) |
2202 | || ! __libc_enable_secure) |
2203 | && __glibc_likely (GLRO(dl_inhibit_cache) == 0)) |
2204 | { |
2205 | /* Check the list of libraries in the file /etc/ld.so.cache, |
2206 | for compatibility with Linux's ldconfig program. */ |
2207 | char *cached = _dl_load_cache_lookup (name); |
2208 | |
2209 | if (cached != NULL) |
2210 | { |
2211 | // XXX Correct to unconditionally default to namespace 0? |
2212 | l = (loader |
2213 | ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded |
2214 | # ifdef SHARED |
2215 | ?: &GL(dl_rtld_map) |
2216 | # endif |
2217 | ); |
2218 | |
2219 | /* If the loader has the DF_1_NODEFLIB flag set we must not |
2220 | use a cache entry from any of these directories. */ |
2221 | if (__glibc_unlikely (l->l_flags_1 & DF_1_NODEFLIB)) |
2222 | { |
2223 | const char *dirp = system_dirs; |
2224 | unsigned int cnt = 0; |
2225 | |
2226 | do |
2227 | { |
2228 | if (memcmp (cached, dirp, system_dirs_len[cnt]) == 0) |
2229 | { |
2230 | /* The prefix matches. Don't use the entry. */ |
2231 | free (cached); |
2232 | cached = NULL; |
2233 | break; |
2234 | } |
2235 | |
2236 | dirp += system_dirs_len[cnt] + 1; |
2237 | ++cnt; |
2238 | } |
2239 | while (cnt < nsystem_dirs_len); |
2240 | } |
2241 | |
2242 | if (cached != NULL) |
2243 | { |
2244 | fd = open_verify (cached, -1, |
2245 | &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded, |
2246 | LA_SER_CONFIG, mode, &found_other_class, |
2247 | false); |
2248 | if (__glibc_likely (fd != -1)) |
2249 | realname = cached; |
2250 | else |
2251 | free (cached); |
2252 | } |
2253 | } |
2254 | } |
2255 | #endif |
2256 | |
2257 | /* Finally, try the default path. */ |
2258 | if (fd == -1 |
2259 | && ((l = loader ?: GL(dl_ns)[nsid]._ns_loaded) == NULL |
2260 | || __glibc_likely (!(l->l_flags_1 & DF_1_NODEFLIB))) |
2261 | && __rtld_search_dirs.dirs != (void *) -1) |
2262 | fd = open_path (name, namelen, mode, &__rtld_search_dirs, |
2263 | &realname, &fb, l, LA_SER_DEFAULT, &found_other_class); |
2264 | |
2265 | /* Add another newline when we are tracing the library loading. */ |
2266 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS)) |
2267 | _dl_debug_printf ("\n" ); |
2268 | } |
2269 | else |
2270 | { |
2271 | /* The path may contain dynamic string tokens. */ |
2272 | realname = (loader |
2273 | ? expand_dynamic_string_token (loader, name) |
2274 | : __strdup (name)); |
2275 | if (realname == NULL) |
2276 | fd = -1; |
2277 | else |
2278 | { |
2279 | fd = open_verify (realname, -1, &fb, |
2280 | loader ?: GL(dl_ns)[nsid]._ns_loaded, 0, mode, |
2281 | &found_other_class, true); |
2282 | if (__glibc_unlikely (fd == -1)) |
2283 | free (realname); |
2284 | } |
2285 | } |
2286 | |
2287 | #ifdef SHARED |
2288 | no_file: |
2289 | #endif |
2290 | /* In case the LOADER information has only been provided to get to |
2291 | the appropriate RUNPATH/RPATH information we do not need it |
2292 | anymore. */ |
2293 | if (mode & __RTLD_CALLMAP) |
2294 | loader = NULL; |
2295 | |
2296 | if (__glibc_unlikely (fd == -1)) |
2297 | { |
2298 | if (trace_mode |
2299 | && __glibc_likely ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK) == 0)) |
2300 | { |
2301 | /* We haven't found an appropriate library. But since we |
2302 | are only interested in the list of libraries this isn't |
2303 | so severe. Fake an entry with all the information we |
2304 | have. */ |
2305 | static const Elf_Symndx dummy_bucket = STN_UNDEF; |
2306 | |
2307 | /* Allocate a new object map. */ |
2308 | if ((name_copy = __strdup (name)) == NULL |
2309 | || (l = _dl_new_object (name_copy, name, type, loader, |
2310 | mode, nsid)) == NULL) |
2311 | { |
2312 | free (name_copy); |
2313 | _dl_signal_error (ENOMEM, name, NULL, |
2314 | N_("cannot create shared object descriptor" )); |
2315 | } |
2316 | /* Signal that this is a faked entry. */ |
2317 | l->l_faked = 1; |
2318 | /* Since the descriptor is initialized with zero we do not |
2319 | have do this here. |
2320 | l->l_reserved = 0; */ |
2321 | l->l_buckets = &dummy_bucket; |
2322 | l->l_nbuckets = 1; |
2323 | l->l_relocated = 1; |
2324 | |
2325 | /* Enter the object in the object list. */ |
2326 | _dl_add_to_namespace_list (l, nsid); |
2327 | |
2328 | return l; |
2329 | } |
2330 | else if (found_other_class) |
2331 | _dl_signal_error (0, name, NULL, |
2332 | ELFW(CLASS) == ELFCLASS32 |
2333 | ? N_("wrong ELF class: ELFCLASS64" ) |
2334 | : N_("wrong ELF class: ELFCLASS32" )); |
2335 | else |
2336 | _dl_signal_error (errno, name, NULL, |
2337 | N_("cannot open shared object file" )); |
2338 | } |
2339 | |
2340 | void *stack_end = __libc_stack_end; |
2341 | return _dl_map_object_from_fd (name, origname, fd, &fb, realname, loader, |
2342 | type, mode, &stack_end, nsid); |
2343 | } |
2344 | |
2345 | struct add_path_state |
2346 | { |
2347 | bool counting; |
2348 | unsigned int idx; |
2349 | Dl_serinfo *si; |
2350 | char *allocptr; |
2351 | }; |
2352 | |
2353 | static void |
2354 | add_path (struct add_path_state *p, const struct r_search_path_struct *sps, |
2355 | unsigned int flags) |
2356 | { |
2357 | if (sps->dirs != (void *) -1) |
2358 | { |
2359 | struct r_search_path_elem **dirs = sps->dirs; |
2360 | do |
2361 | { |
2362 | const struct r_search_path_elem *const r = *dirs++; |
2363 | if (p->counting) |
2364 | { |
2365 | p->si->dls_cnt++; |
2366 | p->si->dls_size += MAX (2, r->dirnamelen); |
2367 | } |
2368 | else |
2369 | { |
2370 | Dl_serpath *const sp = &p->si->dls_serpath[p->idx++]; |
2371 | sp->dls_name = p->allocptr; |
2372 | if (r->dirnamelen < 2) |
2373 | *p->allocptr++ = r->dirnamelen ? '/' : '.'; |
2374 | else |
2375 | p->allocptr = __mempcpy (p->allocptr, |
2376 | r->dirname, r->dirnamelen - 1); |
2377 | *p->allocptr++ = '\0'; |
2378 | sp->dls_flags = flags; |
2379 | } |
2380 | } |
2381 | while (*dirs != NULL); |
2382 | } |
2383 | } |
2384 | |
2385 | void |
2386 | _dl_rtld_di_serinfo (struct link_map *loader, Dl_serinfo *si, bool counting) |
2387 | { |
2388 | if (counting) |
2389 | { |
2390 | si->dls_cnt = 0; |
2391 | si->dls_size = 0; |
2392 | } |
2393 | |
2394 | struct add_path_state p = |
2395 | { |
2396 | .counting = counting, |
2397 | .idx = 0, |
2398 | .si = si, |
2399 | .allocptr = (char *) &si->dls_serpath[si->dls_cnt] |
2400 | }; |
2401 | |
2402 | # define add_path(p, sps, flags) add_path(p, sps, 0) /* XXX */ |
2403 | |
2404 | /* When the object has the RUNPATH information we don't use any RPATHs. */ |
2405 | if (loader->l_info[DT_RUNPATH] == NULL) |
2406 | { |
2407 | /* First try the DT_RPATH of the dependent object that caused NAME |
2408 | to be loaded. Then that object's dependent, and on up. */ |
2409 | |
2410 | struct link_map *l = loader; |
2411 | do |
2412 | { |
2413 | if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH" )) |
2414 | add_path (&p, &l->l_rpath_dirs, XXX_RPATH); |
2415 | l = l->l_loader; |
2416 | } |
2417 | while (l != NULL); |
2418 | |
2419 | /* If dynamically linked, try the DT_RPATH of the executable itself. */ |
2420 | if (loader->l_ns == LM_ID_BASE) |
2421 | { |
2422 | l = GL(dl_ns)[LM_ID_BASE]._ns_loaded; |
2423 | if (l != NULL && l->l_type != lt_loaded && l != loader) |
2424 | if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH" )) |
2425 | add_path (&p, &l->l_rpath_dirs, XXX_RPATH); |
2426 | } |
2427 | } |
2428 | |
2429 | /* Try the LD_LIBRARY_PATH environment variable. */ |
2430 | add_path (&p, &__rtld_env_path_list, XXX_ENV); |
2431 | |
2432 | /* Look at the RUNPATH information for this binary. */ |
2433 | if (cache_rpath (loader, &loader->l_runpath_dirs, DT_RUNPATH, "RUNPATH" )) |
2434 | add_path (&p, &loader->l_runpath_dirs, XXX_RUNPATH); |
2435 | |
2436 | /* XXX |
2437 | Here is where ld.so.cache gets checked, but we don't have |
2438 | a way to indicate that in the results for Dl_serinfo. */ |
2439 | |
2440 | /* Finally, try the default path. */ |
2441 | if (!(loader->l_flags_1 & DF_1_NODEFLIB)) |
2442 | add_path (&p, &__rtld_search_dirs, XXX_default); |
2443 | |
2444 | if (counting) |
2445 | /* Count the struct size before the string area, which we didn't |
2446 | know before we completed dls_cnt. */ |
2447 | si->dls_size += (char *) &si->dls_serpath[si->dls_cnt] - (char *) si; |
2448 | } |
2449 | |