1 /* Load the dependencies of a mapped object.
2 Copyright (C) 1996-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
28 #include <sys/param.h>
33 /* Whether an shared object references one or more auxiliary objects
34 is signaled by the AUXTAG entry in l_info. */
35 #define AUXTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
36 + DT_EXTRATAGIDX (DT_AUXILIARY))
37 /* Whether an shared object references one or more auxiliary objects
38 is signaled by the AUXTAG entry in l_info. */
39 #define FILTERTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
40 + DT_EXTRATAGIDX (DT_FILTER))
43 /* When loading auxiliary objects we must ignore errors. It's ok if
44 an object is missing. */
47 /* The arguments to openaux. */
54 /* The return value of openaux. */
61 struct openaux_args
*args
= (struct openaux_args
*) a
;
63 args
->aux
= _dl_map_object (args
->map
, args
->name
,
64 (args
->map
->l_type
== lt_executable
65 ? lt_library
: args
->map
->l_type
),
66 args
->trace_mode
, args
->open_mode
,
72 _dl_build_local_scope (struct link_map
**list
, struct link_map
*map
)
74 struct link_map
**p
= list
;
80 for (q
= map
->l_initfini
+ 1; *q
; ++q
)
81 if (! (*q
)->l_reserved
)
82 p
+= _dl_build_local_scope (p
, *q
);
87 /* We use a very special kind of list to track the path
88 through the list of loaded shared objects. We have to
89 produce a flat list with unique members of all involved objects.
93 int done
; /* Nonzero if this map was processed. */
94 struct link_map
*map
; /* The data. */
95 struct list
*next
; /* Elements for normal list. */
99 /* Macro to expand DST. It is an macro since we use `alloca'. */
100 #define expand_dst(l, str, fatal) \
102 const char *__str = (str); \
103 const char *__result = __str; \
104 size_t __dst_cnt = DL_DST_COUNT (__str, 0); \
106 if (__dst_cnt != 0) \
110 /* DST must not appear in SUID/SGID programs. */ \
111 if (__libc_enable_secure) \
112 _dl_signal_error (0, __str, NULL, N_("\
113 DST not allowed in SUID/SGID programs")); \
115 __newp = (char *) alloca (DL_DST_REQUIRED (l, __str, strlen (__str), \
118 __result = _dl_dst_substitute (l, __str, __newp, 0); \
120 if (*__result == '\0') \
122 /* The replacement for the DST is not known. We can't \
125 _dl_signal_error (0, __str, NULL, N_("\
126 empty dynamic string token substitution")); \
129 /* This is for DT_AUXILIARY. */ \
130 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS)) \
131 _dl_debug_printf (N_("\
132 cannot load auxiliary `%s' because of empty dynamic string token " \
133 "substitution\n"), __str); \
142 preload (struct list
*known
, unsigned int *nlist
, struct link_map
*map
)
144 known
[*nlist
].done
= 0;
145 known
[*nlist
].map
= map
;
146 known
[*nlist
].next
= &known
[*nlist
+ 1];
149 /* We use `l_reserved' as a mark bit to detect objects we have
150 already put in the search list and avoid adding duplicate
151 elements later in the list. */
157 _dl_map_object_deps (struct link_map
*map
,
158 struct link_map
**preloads
, unsigned int npreloads
,
159 int trace_mode
, int open_mode
)
161 struct list
*known
= __alloca (sizeof *known
* (1 + npreloads
+ 1));
162 struct list
*runp
, *tail
;
163 unsigned int nlist
, i
;
168 struct dl_exception exception
;
170 /* No loaded object so far. */
173 /* First load MAP itself. */
174 preload (known
, &nlist
, map
);
176 /* Add the preloaded items after MAP but before any of its dependencies. */
177 for (i
= 0; i
< npreloads
; ++i
)
178 preload (known
, &nlist
, preloads
[i
]);
180 /* Terminate the lists. */
181 known
[nlist
- 1].next
= NULL
;
183 /* Pointer to last unique object. */
184 tail
= &known
[nlist
- 1];
186 /* No alloca'd space yet. */
187 struct link_map
**needed_space
= NULL
;
188 size_t needed_space_bytes
= 0;
190 /* Process each element of the search list, loading each of its
191 auxiliary objects and immediate dependencies. Auxiliary objects
192 will be added in the list before the object itself and
193 dependencies will be appended to the list as we step through it.
194 This produces a flat, ordered list that represents a
195 breadth-first search of the dependency tree.
197 The whole process is complicated by the fact that we better
198 should use alloca for the temporary list elements. But using
199 alloca means we cannot use recursive function calls. */
204 for (runp
= known
; runp
; )
206 struct link_map
*l
= runp
->map
;
207 struct link_map
**needed
= NULL
;
208 unsigned int nneeded
= 0;
210 /* Unless otherwise stated, this object is handled. */
213 /* Allocate a temporary record to contain the references to the
214 dependencies of this object. */
215 if (l
->l_searchlist
.r_list
== NULL
&& l
->l_initfini
== NULL
216 && l
!= map
&& l
->l_ldnum
> 0)
218 size_t new_size
= l
->l_ldnum
* sizeof (struct link_map
*);
220 if (new_size
> needed_space_bytes
)
222 = extend_alloca (needed_space
, needed_space_bytes
, new_size
);
224 needed
= needed_space
;
227 if (l
->l_info
[DT_NEEDED
] || l
->l_info
[AUXTAG
] || l
->l_info
[FILTERTAG
])
229 const char *strtab
= (const void *) D_PTR (l
, l_info
[DT_STRTAB
]);
230 struct openaux_args args
;
234 args
.strtab
= strtab
;
236 args
.trace_mode
= trace_mode
;
237 args
.open_mode
= open_mode
;
240 for (d
= l
->l_ld
; d
->d_tag
!= DT_NULL
; ++d
)
241 if (__builtin_expect (d
->d_tag
, DT_NEEDED
) == DT_NEEDED
)
243 /* Map in the needed object. */
244 struct link_map
*dep
;
246 /* Recognize DSTs. */
247 name
= expand_dst (l
, strtab
+ d
->d_un
.d_val
, 0);
248 /* Store the tag in the argument structure. */
251 int err
= _dl_catch_exception (&exception
, openaux
, &args
);
252 if (__glibc_unlikely (exception
.errstring
!= NULL
))
263 if (! dep
->l_reserved
)
265 /* Allocate new entry. */
268 newp
= alloca (sizeof (struct list
));
270 /* Append DEP to the list. */
277 /* Set the mark bit that says it's already in the list. */
281 /* Remember this dependency. */
283 needed
[nneeded
++] = dep
;
285 else if (d
->d_tag
== DT_AUXILIARY
|| d
->d_tag
== DT_FILTER
)
289 /* Recognize DSTs. */
290 name
= expand_dst (l
, strtab
+ d
->d_un
.d_val
,
291 d
->d_tag
== DT_AUXILIARY
);
292 /* Store the tag in the argument structure. */
295 /* Say that we are about to load an auxiliary library. */
296 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
,
298 _dl_debug_printf ("load auxiliary object=%s"
299 " requested by file=%s\n",
301 DSO_FILENAME (l
->l_name
));
303 /* We must be prepared that the addressed shared
304 object is not available. For filter objects the dependency
305 must be available. */
306 int err
= _dl_catch_exception (&exception
, openaux
, &args
);
307 if (__glibc_unlikely (exception
.errstring
!= NULL
))
309 if (d
->d_tag
== DT_AUXILIARY
)
311 /* We are not interested in the error message. */
312 _dl_exception_free (&exception
);
313 /* Simply ignore this error and continue the work. */
326 /* The auxiliary object is actually available.
327 Incorporate the map in all the lists. */
329 /* Allocate new entry. This always has to be done. */
330 newp
= alloca (sizeof (struct list
));
332 /* We want to insert the new map before the current one,
333 but we have no back links. So we copy the contents of
334 the current entry over. Note that ORIG and NEWP now
335 have switched their meanings. */
336 memcpy (newp
, orig
, sizeof (*newp
));
338 /* Initialize new entry. */
340 orig
->map
= args
.aux
;
342 /* Remember this dependency. */
344 needed
[nneeded
++] = args
.aux
;
346 /* We must handle two situations here: the map is new,
347 so we must add it in all three lists. If the map
348 is already known, we have two further possibilities:
349 - if the object is before the current map in the
350 search list, we do nothing. It is already found
352 - if the object is after the current one, we must
353 move it just before the current map to make sure
354 the symbols are found early enough
356 if (args
.aux
->l_reserved
)
358 /* The object is already somewhere in the list.
362 /* This object is already in the search list we
363 are building. Don't add a duplicate pointer.
364 Just added by _dl_map_object. */
365 for (late
= newp
; late
->next
!= NULL
; late
= late
->next
)
366 if (late
->next
->map
== args
.aux
)
369 if (late
->next
!= NULL
)
371 /* The object is somewhere behind the current
372 position in the search path. We have to
373 move it to this earlier position. */
376 /* Now remove the later entry from the list
377 and adjust the tail pointer. */
378 if (tail
== late
->next
)
380 late
->next
= late
->next
->next
;
382 /* We must move the object earlier in the chain. */
383 if (args
.aux
->l_prev
!= NULL
)
384 args
.aux
->l_prev
->l_next
= args
.aux
->l_next
;
385 if (args
.aux
->l_next
!= NULL
)
386 args
.aux
->l_next
->l_prev
= args
.aux
->l_prev
;
388 args
.aux
->l_prev
= newp
->map
->l_prev
;
389 newp
->map
->l_prev
= args
.aux
;
390 if (args
.aux
->l_prev
!= NULL
)
391 args
.aux
->l_prev
->l_next
= args
.aux
;
392 args
.aux
->l_next
= newp
->map
;
396 /* The object must be somewhere earlier in the
397 list. Undo to the current list element what
399 memcpy (orig
, newp
, sizeof (*newp
));
405 /* This is easy. We just add the symbol right here. */
408 /* Set the mark bit that says it's already in the list. */
409 args
.aux
->l_reserved
= 1;
411 /* The only problem is that in the double linked
412 list of all objects we don't have this new
413 object at the correct place. Correct this here. */
414 if (args
.aux
->l_prev
)
415 args
.aux
->l_prev
->l_next
= args
.aux
->l_next
;
416 if (args
.aux
->l_next
)
417 args
.aux
->l_next
->l_prev
= args
.aux
->l_prev
;
419 args
.aux
->l_prev
= newp
->map
->l_prev
;
420 newp
->map
->l_prev
= args
.aux
;
421 if (args
.aux
->l_prev
!= NULL
)
422 args
.aux
->l_prev
->l_next
= args
.aux
;
423 args
.aux
->l_next
= newp
->map
;
426 /* Move the tail pointer if necessary. */
430 /* Move on the insert point. */
435 /* Terminate the list of dependencies and store the array address. */
438 needed
[nneeded
++] = NULL
;
440 struct link_map
**l_initfini
= (struct link_map
**)
441 malloc ((2 * nneeded
+ 1) * sizeof needed
[0]);
442 if (l_initfini
== NULL
)
443 _dl_signal_error (ENOMEM
, map
->l_name
, NULL
,
444 N_("cannot allocate dependency list"));
446 memcpy (&l_initfini
[1], needed
, nneeded
* sizeof needed
[0]);
447 memcpy (&l_initfini
[nneeded
+ 1], l_initfini
,
448 nneeded
* sizeof needed
[0]);
449 atomic_write_barrier ();
450 l
->l_initfini
= l_initfini
;
451 l
->l_free_initfini
= 1;
454 /* If we have no auxiliary objects just go on to the next map. */
458 while (runp
!= NULL
&& runp
->done
);
462 if (errno
== 0 && errno_saved
!= 0)
463 __set_errno (errno_saved
);
465 struct link_map
**old_l_initfini
= NULL
;
466 if (map
->l_initfini
!= NULL
&& map
->l_type
== lt_loaded
)
468 /* This object was previously loaded as a dependency and we have
469 a separate l_initfini list. We don't need it anymore. */
470 assert (map
->l_searchlist
.r_list
== NULL
);
471 old_l_initfini
= map
->l_initfini
;
474 /* Store the search list we built in the object. It will be used for
475 searches in the scope of this object. */
476 struct link_map
**l_initfini
=
477 (struct link_map
**) malloc ((2 * nlist
+ 1)
478 * sizeof (struct link_map
*));
479 if (l_initfini
== NULL
)
480 _dl_signal_error (ENOMEM
, map
->l_name
, NULL
,
481 N_("cannot allocate symbol search list"));
484 map
->l_searchlist
.r_list
= &l_initfini
[nlist
+ 1];
485 map
->l_searchlist
.r_nlist
= nlist
;
487 for (nlist
= 0, runp
= known
; runp
; runp
= runp
->next
)
489 if (__builtin_expect (trace_mode
, 0) && runp
->map
->l_faked
)
490 /* This can happen when we trace the loading. */
491 --map
->l_searchlist
.r_nlist
;
493 map
->l_searchlist
.r_list
[nlist
++] = runp
->map
;
495 /* Now clear all the mark bits we set in the objects on the search list
496 to avoid duplicates, so the next call starts fresh. */
497 runp
->map
->l_reserved
= 0;
500 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_PRELINK
, 0) != 0
501 && map
== GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
)
503 /* If we are to compute conflicts, we have to build local scope
504 for each library, not just the ultimate loader. */
505 for (i
= 0; i
< nlist
; ++i
)
507 struct link_map
*l
= map
->l_searchlist
.r_list
[i
];
510 /* The local scope has been already computed. */
512 || (l
->l_local_scope
[0]
513 && l
->l_local_scope
[0]->r_nlist
) != 0)
516 if (l
->l_info
[AUXTAG
] || l
->l_info
[FILTERTAG
])
518 /* As current DT_AUXILIARY/DT_FILTER implementation needs to be
519 rewritten, no need to bother with prelinking the old
521 _dl_signal_error (EINVAL
, l
->l_name
, NULL
, N_("\
522 Filters not supported with LD_TRACE_PRELINKING"));
525 cnt
= _dl_build_local_scope (l_initfini
, l
);
526 assert (cnt
<= nlist
);
527 for (j
= 0; j
< cnt
; j
++)
529 l_initfini
[j
]->l_reserved
= 0;
530 if (j
&& __builtin_expect (l_initfini
[j
]->l_info
[DT_SYMBOLIC
]
532 l
->l_symbolic_in_local_scope
= true;
535 l
->l_local_scope
[0] =
536 (struct r_scope_elem
*) malloc (sizeof (struct r_scope_elem
)
538 * sizeof (struct link_map
*)));
539 if (l
->l_local_scope
[0] == NULL
)
540 _dl_signal_error (ENOMEM
, map
->l_name
, NULL
,
541 N_("cannot allocate symbol search list"));
542 l
->l_local_scope
[0]->r_nlist
= cnt
;
543 l
->l_local_scope
[0]->r_list
=
544 (struct link_map
**) (l
->l_local_scope
[0] + 1);
545 memcpy (l
->l_local_scope
[0]->r_list
, l_initfini
,
546 cnt
* sizeof (struct link_map
*));
550 /* Maybe we can remove some relocation dependencies now. */
551 assert (map
->l_searchlist
.r_list
[0] == map
);
552 struct link_map_reldeps
*l_reldeps
= NULL
;
553 if (map
->l_reldeps
!= NULL
)
555 for (i
= 1; i
< nlist
; ++i
)
556 map
->l_searchlist
.r_list
[i
]->l_reserved
= 1;
558 struct link_map
**list
= &map
->l_reldeps
->list
[0];
559 for (i
= 0; i
< map
->l_reldeps
->act
; ++i
)
560 if (list
[i
]->l_reserved
)
562 /* Need to allocate new array of relocation dependencies. */
563 l_reldeps
= malloc (sizeof (*l_reldeps
)
565 * sizeof (struct link_map
*));
566 if (l_reldeps
== NULL
)
567 /* Bad luck, keep the reldeps duplicated between
568 map->l_reldeps->list and map->l_initfini lists. */
573 memcpy (&l_reldeps
->list
[0], &list
[0],
574 i
* sizeof (struct link_map
*));
575 for (i
= i
+ 1; i
< map
->l_reldeps
->act
; ++i
)
576 if (!list
[i
]->l_reserved
)
577 l_reldeps
->list
[j
++] = list
[i
];
582 for (i
= 1; i
< nlist
; ++i
)
583 map
->l_searchlist
.r_list
[i
]->l_reserved
= 0;
586 /* Sort the initializer list to take dependencies into account. The binary
587 itself will always be initialize last. */
588 memcpy (l_initfini
, map
->l_searchlist
.r_list
,
589 nlist
* sizeof (struct link_map
*));
590 if (__glibc_likely (nlist
> 1))
592 /* We can skip looking for the binary itself which is at the front
593 of the search list. */
595 uint16_t seen
[nlist
];
596 memset (seen
, 0, nlist
* sizeof (seen
[0]));
599 /* Keep track of which object we looked at this round. */
601 struct link_map
*thisp
= l_initfini
[i
];
603 /* Find the last object in the list for which the current one is
604 a dependency and move the current object behind the object
605 with the dependency. */
606 unsigned int k
= nlist
- 1;
609 struct link_map
**runp
= l_initfini
[k
]->l_initfini
;
611 /* Look through the dependencies of the object. */
612 while (*runp
!= NULL
)
613 if (__glibc_unlikely (*runp
++ == thisp
))
615 /* Move the current object to the back past the last
616 object with it as the dependency. */
617 memmove (&l_initfini
[i
], &l_initfini
[i
+ 1],
618 (k
- i
) * sizeof (l_initfini
[0]));
619 l_initfini
[k
] = thisp
;
621 if (seen
[i
+ 1] > nlist
- i
)
627 uint16_t this_seen
= seen
[i
];
628 memmove (&seen
[i
], &seen
[i
+ 1],
629 (k
- i
) * sizeof (seen
[0]));
641 memset (&seen
[i
], 0, (nlist
- i
) * sizeof (seen
[0]));
647 /* Terminate the list of dependencies. */
648 l_initfini
[nlist
] = NULL
;
649 atomic_write_barrier ();
650 map
->l_initfini
= l_initfini
;
651 map
->l_free_initfini
= 1;
652 if (l_reldeps
!= NULL
)
654 atomic_write_barrier ();
655 void *old_l_reldeps
= map
->l_reldeps
;
656 map
->l_reldeps
= l_reldeps
;
657 _dl_scope_free (old_l_reldeps
);
659 if (old_l_initfini
!= NULL
)
660 _dl_scope_free (old_l_initfini
);
663 _dl_signal_exception (errno_reason
== -1 ? 0 : errno_reason
,