1 /* Load the dependencies of a mapped object.
2 Copyright (C) 1996-2013 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 (INTUSE(__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 (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0))\
131 _dl_debug_printf (N_("\
132 cannot load auxiliary `%s' because of empty dynamic string token " \
133 "substitution\n"), __str); \
144 _dl_map_object_deps (struct link_map
*map
,
145 struct link_map
**preloads
, unsigned int npreloads
,
146 int trace_mode
, int open_mode
)
148 struct list
*known
= __alloca (sizeof *known
* (1 + npreloads
+ 1));
149 struct list
*runp
, *tail
;
150 unsigned int nlist
, i
;
155 const char *errstring
;
158 void preload (struct link_map
*map
)
160 known
[nlist
].done
= 0;
161 known
[nlist
].map
= map
;
162 known
[nlist
].next
= &known
[nlist
+ 1];
165 /* We use `l_reserved' as a mark bit to detect objects we have
166 already put in the search list and avoid adding duplicate
167 elements later in the list. */
171 /* No loaded object so far. */
174 /* First load MAP itself. */
177 /* Add the preloaded items after MAP but before any of its dependencies. */
178 for (i
= 0; i
< npreloads
; ++i
)
179 preload (preloads
[i
]);
181 /* Terminate the lists. */
182 known
[nlist
- 1].next
= NULL
;
184 /* Pointer to last unique object. */
185 tail
= &known
[nlist
- 1];
187 /* No alloca'd space yet. */
188 struct link_map
**needed_space
= NULL
;
189 size_t needed_space_bytes
= 0;
191 /* Process each element of the search list, loading each of its
192 auxiliary objects and immediate dependencies. Auxiliary objects
193 will be added in the list before the object itself and
194 dependencies will be appended to the list as we step through it.
195 This produces a flat, ordered list that represents a
196 breadth-first search of the dependency tree.
198 The whole process is complicated by the fact that we better
199 should use alloca for the temporary list elements. But using
200 alloca means we cannot use recursive function calls. */
206 for (runp
= known
; runp
; )
208 struct link_map
*l
= runp
->map
;
209 struct link_map
**needed
= NULL
;
210 unsigned int nneeded
= 0;
212 /* Unless otherwise stated, this object is handled. */
215 /* Allocate a temporary record to contain the references to the
216 dependencies of this object. */
217 if (l
->l_searchlist
.r_list
== NULL
&& l
->l_initfini
== NULL
218 && l
!= map
&& l
->l_ldnum
> 0)
220 size_t new_size
= l
->l_ldnum
* sizeof (struct link_map
*);
222 if (new_size
> needed_space_bytes
)
224 = extend_alloca (needed_space
, needed_space_bytes
, new_size
);
226 needed
= needed_space
;
229 if (l
->l_info
[DT_NEEDED
] || l
->l_info
[AUXTAG
] || l
->l_info
[FILTERTAG
])
231 const char *strtab
= (const void *) D_PTR (l
, l_info
[DT_STRTAB
]);
232 struct openaux_args args
;
236 args
.strtab
= strtab
;
238 args
.trace_mode
= trace_mode
;
239 args
.open_mode
= open_mode
;
242 for (d
= l
->l_ld
; d
->d_tag
!= DT_NULL
; ++d
)
243 if (__builtin_expect (d
->d_tag
, DT_NEEDED
) == DT_NEEDED
)
245 /* Map in the needed object. */
246 struct link_map
*dep
;
248 /* Recognize DSTs. */
249 name
= expand_dst (l
, strtab
+ d
->d_un
.d_val
, 0);
250 /* Store the tag in the argument structure. */
254 int err
= _dl_catch_error (&objname
, &errstring
, &malloced
,
256 if (__builtin_expect (errstring
!= NULL
, 0))
258 char *new_errstring
= strdupa (errstring
);
259 objname
= strdupa (objname
);
261 free ((char *) errstring
);
262 errstring
= new_errstring
;
273 if (! dep
->l_reserved
)
275 /* Allocate new entry. */
278 newp
= alloca (sizeof (struct list
));
280 /* Append DEP to the list. */
287 /* Set the mark bit that says it's already in the list. */
291 /* Remember this dependency. */
293 needed
[nneeded
++] = dep
;
295 else if (d
->d_tag
== DT_AUXILIARY
|| d
->d_tag
== DT_FILTER
)
299 /* Recognize DSTs. */
300 name
= expand_dst (l
, strtab
+ d
->d_un
.d_val
,
301 d
->d_tag
== DT_AUXILIARY
);
302 /* Store the tag in the argument structure. */
305 if (d
->d_tag
== DT_AUXILIARY
)
307 /* Say that we are about to load an auxiliary library. */
308 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
,
310 _dl_debug_printf ("load auxiliary object=%s"
311 " requested by file=%s\n",
313 DSO_FILENAME (l
->l_name
));
315 /* We must be prepared that the addressed shared
316 object is not available. */
318 (void) _dl_catch_error (&objname
, &errstring
, &malloced
,
320 if (__builtin_expect (errstring
!= NULL
, 0))
322 /* We are not interested in the error message. */
323 assert (errstring
!= NULL
);
325 free ((char *) errstring
);
327 /* Simply ignore this error and continue the work. */
333 /* Say that we are about to load an auxiliary library. */
334 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
,
336 _dl_debug_printf ("load filtered object=%s"
337 " requested by file=%s\n",
339 DSO_FILENAME (l
->l_name
));
341 /* For filter objects the dependency must be available. */
343 int err
= _dl_catch_error (&objname
, &errstring
, &malloced
,
345 if (__builtin_expect (errstring
!= NULL
, 0))
347 char *new_errstring
= strdupa (errstring
);
348 objname
= strdupa (objname
);
350 free ((char *) errstring
);
351 errstring
= new_errstring
;
361 /* The auxiliary object is actually available.
362 Incorporate the map in all the lists. */
364 /* Allocate new entry. This always has to be done. */
365 newp
= alloca (sizeof (struct list
));
367 /* We want to insert the new map before the current one,
368 but we have no back links. So we copy the contents of
369 the current entry over. Note that ORIG and NEWP now
370 have switched their meanings. */
371 memcpy (newp
, orig
, sizeof (*newp
));
373 /* Initialize new entry. */
375 orig
->map
= args
.aux
;
377 /* Remember this dependency. */
379 needed
[nneeded
++] = args
.aux
;
381 /* We must handle two situations here: the map is new,
382 so we must add it in all three lists. If the map
383 is already known, we have two further possibilities:
384 - if the object is before the current map in the
385 search list, we do nothing. It is already found
387 - if the object is after the current one, we must
388 move it just before the current map to make sure
389 the symbols are found early enough
391 if (args
.aux
->l_reserved
)
393 /* The object is already somewhere in the list.
397 /* This object is already in the search list we
398 are building. Don't add a duplicate pointer.
399 Just added by _dl_map_object. */
400 for (late
= newp
; late
->next
!= NULL
; late
= late
->next
)
401 if (late
->next
->map
== args
.aux
)
404 if (late
->next
!= NULL
)
406 /* The object is somewhere behind the current
407 position in the search path. We have to
408 move it to this earlier position. */
411 /* Now remove the later entry from the list
412 and adjust the tail pointer. */
413 if (tail
== late
->next
)
415 late
->next
= late
->next
->next
;
417 /* We must move the object earlier in the chain. */
418 if (args
.aux
->l_prev
!= NULL
)
419 args
.aux
->l_prev
->l_next
= args
.aux
->l_next
;
420 if (args
.aux
->l_next
!= NULL
)
421 args
.aux
->l_next
->l_prev
= args
.aux
->l_prev
;
423 args
.aux
->l_prev
= newp
->map
->l_prev
;
424 newp
->map
->l_prev
= args
.aux
;
425 if (args
.aux
->l_prev
!= NULL
)
426 args
.aux
->l_prev
->l_next
= args
.aux
;
427 args
.aux
->l_next
= newp
->map
;
431 /* The object must be somewhere earlier in the
432 list. Undo to the current list element what
434 memcpy (orig
, newp
, sizeof (*newp
));
440 /* This is easy. We just add the symbol right here. */
443 /* Set the mark bit that says it's already in the list. */
444 args
.aux
->l_reserved
= 1;
446 /* The only problem is that in the double linked
447 list of all objects we don't have this new
448 object at the correct place. Correct this here. */
449 if (args
.aux
->l_prev
)
450 args
.aux
->l_prev
->l_next
= args
.aux
->l_next
;
451 if (args
.aux
->l_next
)
452 args
.aux
->l_next
->l_prev
= args
.aux
->l_prev
;
454 args
.aux
->l_prev
= newp
->map
->l_prev
;
455 newp
->map
->l_prev
= args
.aux
;
456 if (args
.aux
->l_prev
!= NULL
)
457 args
.aux
->l_prev
->l_next
= args
.aux
;
458 args
.aux
->l_next
= newp
->map
;
461 /* Move the tail pointer if necessary. */
465 /* Move on the insert point. */
470 /* Terminate the list of dependencies and store the array address. */
473 needed
[nneeded
++] = NULL
;
475 struct link_map
**l_initfini
= (struct link_map
**)
476 malloc ((2 * nneeded
+ 1) * sizeof needed
[0]);
477 if (l_initfini
== NULL
)
478 _dl_signal_error (ENOMEM
, map
->l_name
, NULL
,
479 N_("cannot allocate dependency list"));
481 memcpy (&l_initfini
[1], needed
, nneeded
* sizeof needed
[0]);
482 memcpy (&l_initfini
[nneeded
+ 1], l_initfini
,
483 nneeded
* sizeof needed
[0]);
484 atomic_write_barrier ();
485 l
->l_initfini
= l_initfini
;
486 l
->l_free_initfini
= 1;
489 /* If we have no auxiliary objects just go on to the next map. */
493 while (runp
!= NULL
&& runp
->done
);
497 if (errno
== 0 && errno_saved
!= 0)
498 __set_errno (errno_saved
);
500 struct link_map
**old_l_initfini
= NULL
;
501 if (map
->l_initfini
!= NULL
&& map
->l_type
== lt_loaded
)
503 /* This object was previously loaded as a dependency and we have
504 a separate l_initfini list. We don't need it anymore. */
505 assert (map
->l_searchlist
.r_list
== NULL
);
506 old_l_initfini
= map
->l_initfini
;
509 /* Store the search list we built in the object. It will be used for
510 searches in the scope of this object. */
511 struct link_map
**l_initfini
=
512 (struct link_map
**) malloc ((2 * nlist
+ 1)
513 * sizeof (struct link_map
*));
514 if (l_initfini
== NULL
)
515 _dl_signal_error (ENOMEM
, map
->l_name
, NULL
,
516 N_("cannot allocate symbol search list"));
519 map
->l_searchlist
.r_list
= &l_initfini
[nlist
+ 1];
520 map
->l_searchlist
.r_nlist
= nlist
;
522 for (nlist
= 0, runp
= known
; runp
; runp
= runp
->next
)
524 if (__builtin_expect (trace_mode
, 0) && runp
->map
->l_faked
)
525 /* This can happen when we trace the loading. */
526 --map
->l_searchlist
.r_nlist
;
528 map
->l_searchlist
.r_list
[nlist
++] = runp
->map
;
530 /* Now clear all the mark bits we set in the objects on the search list
531 to avoid duplicates, so the next call starts fresh. */
532 runp
->map
->l_reserved
= 0;
535 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_PRELINK
, 0) != 0
536 && map
== GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
)
538 /* If we are to compute conflicts, we have to build local scope
539 for each library, not just the ultimate loader. */
540 for (i
= 0; i
< nlist
; ++i
)
542 struct link_map
*l
= map
->l_searchlist
.r_list
[i
];
545 /* The local scope has been already computed. */
547 || (l
->l_local_scope
[0]
548 && l
->l_local_scope
[0]->r_nlist
) != 0)
551 if (l
->l_info
[AUXTAG
] || l
->l_info
[FILTERTAG
])
553 /* As current DT_AUXILIARY/DT_FILTER implementation needs to be
554 rewritten, no need to bother with prelinking the old
556 _dl_signal_error (EINVAL
, l
->l_name
, NULL
, N_("\
557 Filters not supported with LD_TRACE_PRELINKING"));
560 cnt
= _dl_build_local_scope (l_initfini
, l
);
561 assert (cnt
<= nlist
);
562 for (j
= 0; j
< cnt
; j
++)
564 l_initfini
[j
]->l_reserved
= 0;
565 if (j
&& __builtin_expect (l_initfini
[j
]->l_info
[DT_SYMBOLIC
]
567 l
->l_symbolic_in_local_scope
= true;
570 l
->l_local_scope
[0] =
571 (struct r_scope_elem
*) malloc (sizeof (struct r_scope_elem
)
573 * sizeof (struct link_map
*)));
574 if (l
->l_local_scope
[0] == NULL
)
575 _dl_signal_error (ENOMEM
, map
->l_name
, NULL
,
576 N_("cannot allocate symbol search list"));
577 l
->l_local_scope
[0]->r_nlist
= cnt
;
578 l
->l_local_scope
[0]->r_list
=
579 (struct link_map
**) (l
->l_local_scope
[0] + 1);
580 memcpy (l
->l_local_scope
[0]->r_list
, l_initfini
,
581 cnt
* sizeof (struct link_map
*));
585 /* Maybe we can remove some relocation dependencies now. */
586 assert (map
->l_searchlist
.r_list
[0] == map
);
587 struct link_map_reldeps
*l_reldeps
= NULL
;
588 if (map
->l_reldeps
!= NULL
)
590 for (i
= 1; i
< nlist
; ++i
)
591 map
->l_searchlist
.r_list
[i
]->l_reserved
= 1;
593 struct link_map
**list
= &map
->l_reldeps
->list
[0];
594 for (i
= 0; i
< map
->l_reldeps
->act
; ++i
)
595 if (list
[i
]->l_reserved
)
597 /* Need to allocate new array of relocation dependencies. */
598 struct link_map_reldeps
*l_reldeps
;
599 l_reldeps
= malloc (sizeof (*l_reldeps
)
601 * sizeof (struct link_map
*));
602 if (l_reldeps
== NULL
)
603 /* Bad luck, keep the reldeps duplicated between
604 map->l_reldeps->list and map->l_initfini lists. */
609 memcpy (&l_reldeps
->list
[0], &list
[0],
610 i
* sizeof (struct link_map
*));
611 for (i
= i
+ 1; i
< map
->l_reldeps
->act
; ++i
)
612 if (!list
[i
]->l_reserved
)
613 l_reldeps
->list
[j
++] = list
[i
];
618 for (i
= 1; i
< nlist
; ++i
)
619 map
->l_searchlist
.r_list
[i
]->l_reserved
= 0;
622 /* Sort the initializer list to take dependencies into account. The binary
623 itself will always be initialize last. */
624 memcpy (l_initfini
, map
->l_searchlist
.r_list
,
625 nlist
* sizeof (struct link_map
*));
626 if (__builtin_expect (nlist
> 1, 1))
628 /* We can skip looking for the binary itself which is at the front
629 of the search list. */
631 uint16_t seen
[nlist
];
632 memset (seen
, 0, nlist
* sizeof (seen
[0]));
635 /* Keep track of which object we looked at this round. */
637 struct link_map
*thisp
= l_initfini
[i
];
639 /* Find the last object in the list for which the current one is
640 a dependency and move the current object behind the object
641 with the dependency. */
642 unsigned int k
= nlist
- 1;
645 struct link_map
**runp
= l_initfini
[k
]->l_initfini
;
647 /* Look through the dependencies of the object. */
648 while (*runp
!= NULL
)
649 if (__builtin_expect (*runp
++ == thisp
, 0))
651 /* Move the current object to the back past the last
652 object with it as the dependency. */
653 memmove (&l_initfini
[i
], &l_initfini
[i
+ 1],
654 (k
- i
) * sizeof (l_initfini
[0]));
655 l_initfini
[k
] = thisp
;
657 if (seen
[i
+ 1] > nlist
- i
)
663 uint16_t this_seen
= seen
[i
];
664 memmove (&seen
[i
], &seen
[i
+ 1],
665 (k
- i
) * sizeof (seen
[0]));
677 memset (&seen
[i
], 0, (nlist
- i
) * sizeof (seen
[0]));
683 /* Terminate the list of dependencies. */
684 l_initfini
[nlist
] = NULL
;
685 atomic_write_barrier ();
686 map
->l_initfini
= l_initfini
;
687 map
->l_free_initfini
= 1;
688 if (l_reldeps
!= NULL
)
690 atomic_write_barrier ();
691 void *old_l_reldeps
= map
->l_reldeps
;
692 map
->l_reldeps
= l_reldeps
;
693 _dl_scope_free (old_l_reldeps
);
695 if (old_l_initfini
!= NULL
)
696 _dl_scope_free (old_l_initfini
);
699 _dl_signal_error (errno_reason
== -1 ? 0 : errno_reason
, objname
,