1 /* Load the dependencies of a mapped object.
2 Copyright (C) 1996-2003, 2004, 2005, 2006, 2007, 2010
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30 #include <sys/param.h>
35 /* Whether an shared object references one or more auxiliary objects
36 is signaled by the AUXTAG entry in l_info. */
37 #define AUXTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
38 + DT_EXTRATAGIDX (DT_AUXILIARY))
39 /* Whether an shared object references one or more auxiliary objects
40 is signaled by the AUXTAG entry in l_info. */
41 #define FILTERTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
42 + DT_EXTRATAGIDX (DT_FILTER))
45 /* When loading auxiliary objects we must ignore errors. It's ok if
46 an object is missing. */
49 /* The arguments to openaux. */
56 /* The return value of openaux. */
63 struct openaux_args
*args
= (struct openaux_args
*) a
;
65 args
->aux
= _dl_map_object (args
->map
, args
->name
, 0,
66 (args
->map
->l_type
== lt_executable
67 ? lt_library
: args
->map
->l_type
),
68 args
->trace_mode
, args
->open_mode
,
74 _dl_build_local_scope (struct link_map
**list
, struct link_map
*map
)
76 struct link_map
**p
= list
;
82 for (q
= map
->l_initfini
+ 1; *q
; ++q
)
83 if (! (*q
)->l_reserved
)
84 p
+= _dl_build_local_scope (p
, *q
);
89 /* We use a very special kind of list to track the path
90 through the list of loaded shared objects. We have to
91 produce a flat list with unique members of all involved objects.
95 int done
; /* Nonzero if this map was processed. */
96 struct link_map
*map
; /* The data. */
97 struct list
*next
; /* Elements for normal list. */
101 /* Macro to expand DST. It is an macro since we use `alloca'. */
102 #define expand_dst(l, str, fatal) \
104 const char *__str = (str); \
105 const char *__result = __str; \
106 size_t __dst_cnt = DL_DST_COUNT (__str, 0); \
108 if (__dst_cnt != 0) \
112 /* DST must not appear in SUID/SGID programs. */ \
113 if (INTUSE(__libc_enable_secure)) \
114 _dl_signal_error (0, __str, NULL, N_("\
115 DST not allowed in SUID/SGID programs")); \
117 __newp = (char *) alloca (DL_DST_REQUIRED (l, __str, strlen (__str), \
120 __result = _dl_dst_substitute (l, __str, __newp, 0); \
122 if (*__result == '\0') \
124 /* The replacement for the DST is not known. We can't \
127 _dl_signal_error (0, __str, NULL, N_("\
128 empty dynamic string token substitution")); \
131 /* This is for DT_AUXILIARY. */ \
132 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0))\
133 _dl_debug_printf (N_("\
134 cannot load auxiliary `%s' because of empty dynamic string token " \
135 "substitution\n"), __str); \
146 _dl_map_object_deps (struct link_map
*map
,
147 struct link_map
**preloads
, unsigned int npreloads
,
148 int trace_mode
, int open_mode
)
150 struct list
*known
= __alloca (sizeof *known
* (1 + npreloads
+ 1));
151 struct list
*runp
, *tail
;
152 unsigned int nlist
, i
;
157 const char *errstring
;
160 auto inline void preload (struct link_map
*map
);
162 inline void preload (struct link_map
*map
)
164 known
[nlist
].done
= 0;
165 known
[nlist
].map
= map
;
166 known
[nlist
].next
= &known
[nlist
+ 1];
169 /* We use `l_reserved' as a mark bit to detect objects we have
170 already put in the search list and avoid adding duplicate
171 elements later in the list. */
175 /* No loaded object so far. */
178 /* First load MAP itself. */
181 /* Add the preloaded items after MAP but before any of its dependencies. */
182 for (i
= 0; i
< npreloads
; ++i
)
183 preload (preloads
[i
]);
185 /* Terminate the lists. */
186 known
[nlist
- 1].next
= NULL
;
188 /* Pointer to last unique object. */
189 tail
= &known
[nlist
- 1];
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)
219 needed
= (struct link_map
**) alloca (l
->l_ldnum
220 * sizeof (struct link_map
*));
222 if (l
->l_info
[DT_NEEDED
] || l
->l_info
[AUXTAG
] || l
->l_info
[FILTERTAG
])
224 const char *strtab
= (const void *) D_PTR (l
, l_info
[DT_STRTAB
]);
225 struct openaux_args args
;
229 args
.strtab
= strtab
;
231 args
.trace_mode
= trace_mode
;
232 args
.open_mode
= open_mode
;
235 for (d
= l
->l_ld
; d
->d_tag
!= DT_NULL
; ++d
)
236 if (__builtin_expect (d
->d_tag
, DT_NEEDED
) == DT_NEEDED
)
238 /* Map in the needed object. */
239 struct link_map
*dep
;
241 /* Recognize DSTs. */
242 name
= expand_dst (l
, strtab
+ d
->d_un
.d_val
, 0);
243 /* Store the tag in the argument structure. */
247 int err
= _dl_catch_error (&objname
, &errstring
, &malloced
,
249 if (__builtin_expect (errstring
!= NULL
, 0))
251 char *new_errstring
= strdupa (errstring
);
252 objname
= strdupa (objname
);
254 free ((char *) errstring
);
255 errstring
= new_errstring
;
266 if (! dep
->l_reserved
)
268 /* Allocate new entry. */
271 newp
= alloca (sizeof (struct list
));
273 /* Append DEP to the list. */
280 /* Set the mark bit that says it's already in the list. */
284 /* Remember this dependency. */
286 needed
[nneeded
++] = dep
;
288 else if (d
->d_tag
== DT_AUXILIARY
|| d
->d_tag
== DT_FILTER
)
292 /* Recognize DSTs. */
293 name
= expand_dst (l
, strtab
+ d
->d_un
.d_val
,
294 d
->d_tag
== DT_AUXILIARY
);
295 /* Store the tag in the argument structure. */
298 if (d
->d_tag
== DT_AUXILIARY
)
300 /* Say that we are about to load an auxiliary library. */
301 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
,
303 _dl_debug_printf ("load auxiliary object=%s"
304 " requested by file=%s\n",
307 ? l
->l_name
: rtld_progname
);
309 /* We must be prepared that the addressed shared
310 object is not available. */
312 (void) _dl_catch_error (&objname
, &errstring
, &malloced
,
314 if (__builtin_expect (errstring
!= NULL
, 0))
316 /* We are not interested in the error message. */
317 assert (errstring
!= NULL
);
319 free ((char *) errstring
);
321 /* Simply ignore this error and continue the work. */
327 /* Say that we are about to load an auxiliary library. */
328 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
,
330 _dl_debug_printf ("load filtered object=%s"
331 " requested by file=%s\n",
334 ? l
->l_name
: rtld_progname
);
336 /* For filter objects the dependency must be available. */
338 int err
= _dl_catch_error (&objname
, &errstring
, &malloced
,
340 if (__builtin_expect (errstring
!= NULL
, 0))
342 char *new_errstring
= strdupa (errstring
);
343 objname
= strdupa (objname
);
345 free ((char *) errstring
);
346 errstring
= new_errstring
;
356 /* The auxiliary object is actually available.
357 Incorporate the map in all the lists. */
359 /* Allocate new entry. This always has to be done. */
360 newp
= alloca (sizeof (struct list
));
362 /* We want to insert the new map before the current one,
363 but we have no back links. So we copy the contents of
364 the current entry over. Note that ORIG and NEWP now
365 have switched their meanings. */
366 memcpy (newp
, orig
, sizeof (*newp
));
368 /* Initialize new entry. */
370 orig
->map
= args
.aux
;
372 /* Remember this dependency. */
374 needed
[nneeded
++] = args
.aux
;
376 /* We must handle two situations here: the map is new,
377 so we must add it in all three lists. If the map
378 is already known, we have two further possibilities:
379 - if the object is before the current map in the
380 search list, we do nothing. It is already found
382 - if the object is after the current one, we must
383 move it just before the current map to make sure
384 the symbols are found early enough
386 if (args
.aux
->l_reserved
)
388 /* The object is already somewhere in the list.
392 /* This object is already in the search list we
393 are building. Don't add a duplicate pointer.
394 Just added by _dl_map_object. */
395 for (late
= newp
; late
->next
!= NULL
; late
= late
->next
)
396 if (late
->next
->map
== args
.aux
)
399 if (late
->next
!= NULL
)
401 /* The object is somewhere behind the current
402 position in the search path. We have to
403 move it to this earlier position. */
406 /* Now remove the later entry from the list
407 and adjust the tail pointer. */
408 if (tail
== late
->next
)
410 late
->next
= late
->next
->next
;
412 /* We must move the object earlier in the chain. */
413 if (args
.aux
->l_prev
!= NULL
)
414 args
.aux
->l_prev
->l_next
= args
.aux
->l_next
;
415 if (args
.aux
->l_next
!= NULL
)
416 args
.aux
->l_next
->l_prev
= args
.aux
->l_prev
;
418 args
.aux
->l_prev
= newp
->map
->l_prev
;
419 newp
->map
->l_prev
= args
.aux
;
420 if (args
.aux
->l_prev
!= NULL
)
421 args
.aux
->l_prev
->l_next
= args
.aux
;
422 args
.aux
->l_next
= newp
->map
;
426 /* The object must be somewhere earlier in the
427 list. Undo to the current list element what
429 memcpy (orig
, newp
, sizeof (*newp
));
435 /* This is easy. We just add the symbol right here. */
438 /* Set the mark bit that says it's already in the list. */
439 args
.aux
->l_reserved
= 1;
441 /* The only problem is that in the double linked
442 list of all objects we don't have this new
443 object at the correct place. Correct this here. */
444 if (args
.aux
->l_prev
)
445 args
.aux
->l_prev
->l_next
= args
.aux
->l_next
;
446 if (args
.aux
->l_next
)
447 args
.aux
->l_next
->l_prev
= args
.aux
->l_prev
;
449 args
.aux
->l_prev
= newp
->map
->l_prev
;
450 newp
->map
->l_prev
= args
.aux
;
451 if (args
.aux
->l_prev
!= NULL
)
452 args
.aux
->l_prev
->l_next
= args
.aux
;
453 args
.aux
->l_next
= newp
->map
;
456 /* Move the tail pointer if necessary. */
460 /* Move on the insert point. */
465 /* Terminate the list of dependencies and store the array address. */
468 needed
[nneeded
++] = NULL
;
470 struct link_map
**l_initfini
= (struct link_map
**)
471 malloc ((2 * nneeded
+ 1) * sizeof needed
[0]);
472 if (l_initfini
== NULL
)
473 _dl_signal_error (ENOMEM
, map
->l_name
, NULL
,
474 N_("cannot allocate dependency list"));
476 memcpy (&l_initfini
[1], needed
, nneeded
* sizeof needed
[0]);
477 memcpy (&l_initfini
[nneeded
+ 1], l_initfini
,
478 nneeded
* sizeof needed
[0]);
479 atomic_write_barrier ();
480 l
->l_initfini
= l_initfini
;
481 l
->l_free_initfini
= 1;
484 /* If we have no auxiliary objects just go on to the next map. */
488 while (runp
!= NULL
&& runp
->done
);
492 if (errno
== 0 && errno_saved
!= 0)
493 __set_errno (errno_saved
);
495 struct link_map
**old_l_initfini
= NULL
;
496 if (map
->l_initfini
!= NULL
&& map
->l_type
== lt_loaded
)
498 /* This object was previously loaded as a dependency and we have
499 a separate l_initfini list. We don't need it anymore. */
500 assert (map
->l_searchlist
.r_list
== NULL
);
501 old_l_initfini
= map
->l_initfini
;
504 /* Store the search list we built in the object. It will be used for
505 searches in the scope of this object. */
506 struct link_map
**l_initfini
=
507 (struct link_map
**) malloc ((2 * nlist
+ 1)
508 * sizeof (struct link_map
*));
509 if (l_initfini
== NULL
)
510 _dl_signal_error (ENOMEM
, map
->l_name
, NULL
,
511 N_("cannot allocate symbol search list"));
514 map
->l_searchlist
.r_list
= &l_initfini
[nlist
+ 1];
515 map
->l_searchlist
.r_nlist
= nlist
;
517 for (nlist
= 0, runp
= known
; runp
; runp
= runp
->next
)
519 if (__builtin_expect (trace_mode
, 0) && runp
->map
->l_faked
)
520 /* This can happen when we trace the loading. */
521 --map
->l_searchlist
.r_nlist
;
523 map
->l_searchlist
.r_list
[nlist
++] = runp
->map
;
525 /* Now clear all the mark bits we set in the objects on the search list
526 to avoid duplicates, so the next call starts fresh. */
527 runp
->map
->l_reserved
= 0;
530 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_PRELINK
, 0) != 0
531 && map
== GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
)
533 /* If we are to compute conflicts, we have to build local scope
534 for each library, not just the ultimate loader. */
535 for (i
= 0; i
< nlist
; ++i
)
537 struct link_map
*l
= map
->l_searchlist
.r_list
[i
];
540 /* The local scope has been already computed. */
542 || (l
->l_local_scope
[0]
543 && l
->l_local_scope
[0]->r_nlist
) != 0)
546 if (l
->l_info
[AUXTAG
] || l
->l_info
[FILTERTAG
])
548 /* As current DT_AUXILIARY/DT_FILTER implementation needs to be
549 rewritten, no need to bother with prelinking the old
551 _dl_signal_error (EINVAL
, l
->l_name
, NULL
, N_("\
552 Filters not supported with LD_TRACE_PRELINKING"));
555 cnt
= _dl_build_local_scope (l_initfini
, l
);
556 assert (cnt
<= nlist
);
557 for (j
= 0; j
< cnt
; j
++)
559 l_initfini
[j
]->l_reserved
= 0;
560 if (j
&& __builtin_expect (l_initfini
[j
]->l_info
[DT_SYMBOLIC
]
562 l
->l_symbolic_in_local_scope
= true;
565 l
->l_local_scope
[0] =
566 (struct r_scope_elem
*) malloc (sizeof (struct r_scope_elem
)
568 * sizeof (struct link_map
*)));
569 if (l
->l_local_scope
[0] == NULL
)
570 _dl_signal_error (ENOMEM
, map
->l_name
, NULL
,
571 N_("cannot allocate symbol search list"));
572 l
->l_local_scope
[0]->r_nlist
= cnt
;
573 l
->l_local_scope
[0]->r_list
=
574 (struct link_map
**) (l
->l_local_scope
[0] + 1);
575 memcpy (l
->l_local_scope
[0]->r_list
, l_initfini
,
576 cnt
* sizeof (struct link_map
*));
580 /* Maybe we can remove some relocation dependencies now. */
581 assert (map
->l_searchlist
.r_list
[0] == map
);
582 struct link_map_reldeps
*l_reldeps
= NULL
;
583 if (map
->l_reldeps
!= NULL
)
585 for (i
= 1; i
< nlist
; ++i
)
586 map
->l_searchlist
.r_list
[i
]->l_reserved
= 1;
588 struct link_map
**list
= &map
->l_reldeps
->list
[0];
589 for (i
= 0; i
< map
->l_reldeps
->act
; ++i
)
590 if (list
[i
]->l_reserved
)
592 /* Need to allocate new array of relocation dependencies. */
593 struct link_map_reldeps
*l_reldeps
;
594 l_reldeps
= malloc (sizeof (*l_reldeps
)
596 * sizeof (struct link_map
*));
597 if (l_reldeps
== NULL
)
598 /* Bad luck, keep the reldeps duplicated between
599 map->l_reldeps->list and map->l_initfini lists. */
604 memcpy (&l_reldeps
->list
[0], &list
[0],
605 i
* sizeof (struct link_map
*));
606 for (i
= i
+ 1; i
< map
->l_reldeps
->act
; ++i
)
607 if (!list
[i
]->l_reserved
)
608 l_reldeps
->list
[j
++] = list
[i
];
613 for (i
= 1; i
< nlist
; ++i
)
614 map
->l_searchlist
.r_list
[i
]->l_reserved
= 0;
617 /* Now determine the order in which the initialization has to happen. */
618 memcpy (l_initfini
, map
->l_searchlist
.r_list
,
619 nlist
* sizeof (struct link_map
*));
620 /* We can skip looking for the binary itself which is at the front
621 of the search list. Look through the list backward so that circular
622 dependencies are not changing the order. */
623 for (i
= 1; i
< nlist
; ++i
)
625 struct link_map
*l
= map
->l_searchlist
.r_list
[i
];
629 /* Find the place in the initfini list where the map is currently
631 for (j
= 1; l_initfini
[j
] != l
; ++j
)
634 /* Find all object for which the current one is a dependency and
635 move the found object (if necessary) in front. */
636 for (k
= j
+ 1; k
< nlist
; ++k
)
638 struct link_map
**runp
;
640 runp
= l_initfini
[k
]->l_initfini
;
643 while (*runp
!= NULL
)
644 if (__builtin_expect (*runp
++ == l
, 0))
646 struct link_map
*here
= l_initfini
[k
];
649 memmove (&l_initfini
[j
] + 1, &l_initfini
[j
],
650 (k
- j
) * sizeof (struct link_map
*));
651 l_initfini
[j
] = here
;
653 /* Don't insert further matches before the last
654 entry moved to the front. */
662 /* Terminate the list of dependencies. */
663 l_initfini
[nlist
] = NULL
;
664 atomic_write_barrier ();
665 map
->l_initfini
= l_initfini
;
666 map
->l_free_initfini
= 1;
667 if (l_reldeps
!= NULL
)
669 atomic_write_barrier ();
670 void *old_l_reldeps
= map
->l_reldeps
;
671 map
->l_reldeps
= l_reldeps
;
672 _dl_scope_free (old_l_reldeps
);
674 if (old_l_initfini
!= NULL
)
675 _dl_scope_free (old_l_initfini
);
678 _dl_signal_error (errno_reason
== -1 ? 0 : errno_reason
, objname
,