4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1988 AT&T
26 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
27 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
31 * Programmatic interface to the run_time linker.
34 #include <sys/debug.h>
45 #include "_inline_gen.h"
49 * Determine who called us - given a pc determine in which object it resides.
51 * For dlopen() the link map of the caller must be passed to load_so() so that
52 * the appropriate search rules (4.x or 5.0) are used to locate any
53 * dependencies. Also, if we've been called from a 4.x module it may be
54 * necessary to fix the specified pathname so that it conforms with the 5.0 elf
57 * For dlsym() the link map of the caller is used to determine RTLD_NEXT
58 * requests, together with requests based off of a dlopen(0).
59 * For dladdr() this routines provides a generic means of scanning all loaded
63 _caller(caddr_t cpc
, int flags
)
68 for (APLIST_TRAVERSE(dynlm_list
, idx1
, lml
)) {
72 for (ALIST_TRAVERSE(lml
->lm_lists
, idx2
, lmc
)) {
75 for (lmp
= lmc
->lc_head
; lmp
;
76 lmp
= NEXT_RT_MAP(lmp
)) {
78 if (find_segment(cpc
, lmp
))
85 * No mapping can be determined. If asked for a default, assume this
86 * is from the executable.
88 if (flags
& CL_EXECDEF
)
89 return ((Rt_map
*)lml_main
.lm_head
);
94 #pragma weak _dlerror = dlerror
97 * External entry for dlerror(3dl). Returns a pointer to the string describing
98 * the last occurring error. The last occurring error is cleared.
109 clmp
= _caller(caller(), CL_EXECDEF
);
111 DBG_CALL(Dbg_dl_dlerror(clmp
, lasterr
));
117 leave(LIST(clmp
), 0);
122 * Add a dependency as a group descriptor to a group handle. Returns 0 on
123 * failure. On success, returns the group descriptor, and if alep is non-NULL
124 * the *alep is set to ALE_EXISTS if the dependency already exists, or to
125 * ALE_CREATE if the dependency is newly created.
128 hdl_add(Grp_hdl
*ghp
, Rt_map
*lmp
, uint_t dflags
, int *alep
)
132 int ale
= ALE_CREATE
;
136 * Make sure this dependency hasn't already been recorded.
138 for (ALIST_TRAVERSE(ghp
->gh_depends
, idx
, gdp
)) {
139 if (gdp
->gd_depend
== lmp
) {
145 if (ale
== ALE_CREATE
) {
149 * Create a new handle descriptor.
155 * Indicate this object is a part of this handles group.
157 if (aplist_append(&GROUPS(lmp
), ghp
, AL_CNT_GROUPS
) == NULL
)
161 * Append the new dependency to this handle.
163 if ((gdp
= alist_append(&ghp
->gh_depends
, &gd
,
164 sizeof (Grp_desc
), AL_CNT_DEPENDS
)) == NULL
)
168 oflags
= gdp
->gd_flags
;
169 gdp
->gd_flags
|= dflags
;
172 if (ale
== ALE_CREATE
)
173 DBG_CALL(Dbg_file_hdl_action(ghp
, lmp
, DBG_DEP_ADD
,
175 else if (gdp
->gd_flags
!= oflags
)
176 DBG_CALL(Dbg_file_hdl_action(ghp
, lmp
, DBG_DEP_UPDATE
,
188 * rlmp - represents the reference link-map for which the handle is being
190 * clmp - represents the caller who is requesting the handle.
191 * hflags - provide group handle flags (GPH_*) that affect the use of the
192 * handle, such as dlopen(0), or use or use of RTLD_FIRST.
193 * rdflags - provide group dependency flags for the reference link-map rlmp,
194 * such as whether the dependency can be used for dlsym(), can be
195 * relocated against, or whether this objects dependencies should
197 * cdflags - provide group dependency flags for the caller.
200 hdl_create(Lm_list
*lml
, Rt_map
*rlmp
, Rt_map
*clmp
, uint_t hflags
,
201 uint_t rdflags
, uint_t cdflags
)
203 Grp_hdl
*ghp
= NULL
, *aghp
;
208 * For dlopen(0) the handle is maintained as part of the link-map list,
209 * otherwise the handle is associated with the reference link-map.
211 if (hflags
& GPH_ZERO
)
212 alpp
= &(lml
->lm_handle
);
214 alpp
= &(HANDLES(rlmp
));
217 * Objects can contain multiple handles depending on the handle flags
218 * supplied. Most RTLD flags pertain to the object itself and the
219 * bindings that it can achieve. Multiple handles for these flags
220 * don't make sense. But if the flag determines how the handle might
221 * be used, then multiple handles may exist. Presently this only makes
222 * sense for RTLD_FIRST. Determine if an appropriate handle already
225 for (APLIST_TRAVERSE(*alpp
, idx
, aghp
)) {
226 if ((aghp
->gh_flags
& GPH_FIRST
) == (hflags
& GPH_FIRST
)) {
236 * If this is the first request for this handle, allocate and
237 * initialize a new handle.
239 DBG_CALL(Dbg_file_hdl_title(DBG_HDL_CREATE
));
241 if ((ghp
= malloc(sizeof (Grp_hdl
))) == NULL
)
245 * Associate the handle with the link-map list or the reference
246 * link-map as appropriate.
248 if (aplist_append(alpp
, ghp
, AL_CNT_GROUPS
) == NULL
) {
254 * Record the existence of this handle for future verification.
257 ndx
= (uintptr_t)ghp
% HDLIST_SZ
;
259 if (aplist_append(&hdl_alp
[ndx
], ghp
, AL_CNT_HANDLES
) == NULL
) {
260 (void) aplist_delete_value(*alpp
, ghp
);
265 ghp
->gh_depends
= NULL
;
267 ghp
->gh_flags
= hflags
;
270 * A dlopen(0) handle is identified by the GPH_ZERO flag, the
271 * head of the link-map list is defined as the owner. There is
272 * no need to maintain a list of dependencies, for when this
273 * handle is used (for dlsym()) a dynamic search through the
274 * entire link-map list provides for searching all objects with
277 if (hflags
& GPH_ZERO
) {
278 ghp
->gh_ownlmp
= lml
->lm_head
;
279 ghp
->gh_ownlml
= lml
;
281 ghp
->gh_ownlmp
= rlmp
;
282 ghp
->gh_ownlml
= LIST(rlmp
);
284 if (hdl_add(ghp
, rlmp
, rdflags
, NULL
) == NULL
)
288 * If this new handle is a private handle, there's no
289 * need to track the caller, so we're done.
291 if (hflags
& GPH_PRIVATE
)
295 * If this new handle is public, and isn't a special
296 * handle representing ld.so.1, indicate that a local
297 * group now exists. This state allows singleton
298 * searches to be optimized.
300 if ((hflags
& GPH_LDSO
) == 0)
301 LIST(rlmp
)->lm_flags
|= LML_FLG_GROUPSEXIST
;
305 * If a handle already exists, bump its reference count.
307 * If the previous reference count was 0, then this is a handle
308 * that an earlier call to dlclose() was unable to remove. Such
309 * handles are put on the orphan list. As this handle is back
310 * in use, it must be removed from the orphan list.
312 * Note, handles associated with a link-map list itself (i.e.
313 * dlopen(0)) can have a reference count of 0. However, these
314 * handles are never deleted, and therefore are never moved to
317 if ((ghp
->gh_refcnt
++ == 0) &&
318 ((ghp
->gh_flags
& GPH_ZERO
) == 0)) {
322 ndx
= (uintptr_t)ghp
% HDLIST_SZ
;
324 (void) aplist_delete_value(hdl_alp
[HDLIST_ORP
], ghp
);
325 (void) aplist_append(&hdl_alp
[ndx
], ghp
,
332 DBG_CALL(Dbg_file_hdl_title(DBG_HDL_REINST
));
333 for (ALIST_TRAVERSE(ghp
->gh_depends
, idx
, gdp
))
334 DBG_CALL(Dbg_file_hdl_action(ghp
,
335 gdp
->gd_depend
, DBG_DEP_REINST
, 0));
340 * If we've been asked to create a private handle, there's no
341 * need to track the caller.
343 if (hflags
& GPH_PRIVATE
) {
345 * Negate the reference count increment.
351 * If a private handle already exists, promote this
352 * handle to public by initializing both the reference
353 * count and the handle flags.
355 if (ghp
->gh_flags
& GPH_PRIVATE
) {
357 ghp
->gh_flags
&= ~GPH_PRIVATE
;
358 ghp
->gh_flags
|= hflags
;
364 * Keep track of the parent (caller). As this object can be referenced
365 * by different parents, this processing is carried out every time a
366 * handle is requested.
368 if (clmp
&& (hdl_add(ghp
, clmp
, cdflags
, NULL
) == NULL
))
375 * Initialize a handle that has been created for an object that is already
376 * loaded. The handle is initialized with the present dependencies of that
377 * object. Once this initialization has occurred, any new objects that might
378 * be loaded as dependencies (lazy-loading) are added to the handle as each new
382 hdl_initialize(Grp_hdl
*ghp
, Rt_map
*nlmp
, int mode
, int promote
)
388 * If the handle has already been initialized, and the initial object's
389 * mode hasn't been promoted, there's no need to recompute the modes of
390 * any dependencies. If the object we've added has just been opened,
391 * the objects dependencies will not yet have been processed. These
392 * dependencies will be added on later calls to load_one(). Otherwise,
393 * this object already exists, so add all of its dependencies to the
394 * handle were operating on.
396 if (((ghp
->gh_flags
& GPH_INITIAL
) && (promote
== 0)) ||
397 ((FLAGS(nlmp
) & FLG_RT_ANALYZED
) == 0)) {
398 ghp
->gh_flags
|= GPH_INITIAL
;
402 DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD
));
403 for (ALIST_TRAVERSE(ghp
->gh_depends
, idx
, gdp
)) {
404 Rt_map
*lmp
= gdp
->gd_depend
;
409 * If this dependency doesn't indicate that its dependencies
410 * should be added to a handle, ignore it. This case identifies
411 * a parent of a dlopen(RTLD_PARENT) request.
413 if ((gdp
->gd_flags
& GPD_ADDEPS
) == 0)
416 for (APLIST_TRAVERSE(DEPENDS(lmp
), idx1
, bdp
)) {
417 Rt_map
*dlmp
= bdp
->b_depend
;
419 if ((bdp
->b_flags
& BND_NEEDED
) == 0)
422 if (hdl_add(ghp
, dlmp
,
423 (GPD_DLSYM
| GPD_RELOC
| GPD_ADDEPS
), NULL
) == NULL
)
426 (void) update_mode(dlmp
, MODE(dlmp
), mode
);
429 ghp
->gh_flags
|= GPH_INITIAL
;
434 * Sanity check a program-provided handle.
437 hdl_validate(Grp_hdl
*ghp
)
444 ndx
= (uintptr_t)ghp
% HDLIST_SZ
;
446 for (APLIST_TRAVERSE(hdl_alp
[ndx
], idx
, lghp
)) {
447 if ((lghp
== ghp
) && (ghp
->gh_refcnt
!= 0))
454 * Core dlclose activity.
457 dlclose_core(Grp_hdl
*ghp
, Rt_map
*clmp
, Lm_list
*lml
)
463 * If we're already at atexit() there's no point processing further,
464 * all objects have already been tsorted for fini processing.
466 if (rtld_flags
& RT_FL_ATEXIT
)
470 * Diagnose what we're up to.
472 if (ghp
->gh_flags
& GPH_ZERO
) {
473 DBG_CALL(Dbg_dl_dlclose(clmp
, MSG_ORIG(MSG_STR_ZERO
),
474 DBG_DLCLOSE_IGNORE
));
476 DBG_CALL(Dbg_dl_dlclose(clmp
, NAME(ghp
->gh_ownlmp
),
481 * Decrement reference count of this object.
483 if (--(ghp
->gh_refcnt
))
487 * If this handle is special (dlopen(0)), then leave it around - it
488 * has little overhead.
490 if (ghp
->gh_flags
& GPH_ZERO
)
494 * If this handle is associated with an object that is not on the base
495 * link-map control list, or it has not yet been relocated, then this
496 * handle must have originated from an auditors interaction, or some
497 * permutation of RTLD_CONFGEN use (crle(1), moe(1), etc.). User code
498 * can only execute and bind to relocated objects on the base link-map
499 * control list. Outside of RTLD_CONFGEN use, a non-relocated object,
500 * or an object on a non-base link-map control list, is in the process
501 * of being loaded, and therefore we do not attempt to remove the
504 if (((lmp
= ghp
->gh_ownlmp
) != NULL
) &&
505 ((MODE(lmp
) & RTLD_CONFGEN
) == 0) &&
506 ((CNTL(lmp
) != ALIST_OFF_DATA
) ||
507 ((FLAGS(lmp
) & FLG_RT_RELOCED
) == 0)))
511 * This handle is no longer being referenced, remove it. If this handle
512 * is part of an alternative link-map list, determine if the whole list
513 * can be removed also.
515 error
= remove_hdl(ghp
, clmp
, NULL
);
517 if ((lml
->lm_flags
& (LML_FLG_BASELM
| LML_FLG_RTLDLM
)) == 0)
524 * Internal dlclose activity. Called from user level or directly for internal
528 dlclose_intn(Grp_hdl
*ghp
, Rt_map
*clmp
)
531 Lm_list
*olml
= NULL
;
535 * Although we're deleting object(s) it's quite possible that additional
536 * objects get loaded from running the .fini section(s) of the objects
537 * being deleted. These objects will have been added to the same
538 * link-map list as those objects being deleted. Remember this list
539 * for later investigation.
541 olml
= ghp
->gh_ownlml
;
543 error
= dlclose_core(ghp
, clmp
, olml
);
546 * Determine whether the original link-map list still exists. In the
547 * case of a dlclose of an alternative (dlmopen) link-map the whole
548 * list may have been removed.
554 for (APLIST_TRAVERSE(dynlm_list
, idx
, lml
)) {
556 nlmp
= olml
->lm_head
;
561 load_completion(nlmp
);
566 * Argument checking for dlclose. Only called via external entry.
569 dlclose_check(void *handle
, Rt_map
*clmp
)
571 Grp_hdl
*ghp
= (Grp_hdl
*)handle
;
573 if (hdl_validate(ghp
) == 0) {
574 Conv_inv_buf_t inv_buf
;
576 (void) conv_invalid_val(&inv_buf
, EC_NATPTR(ghp
), 0);
577 DBG_CALL(Dbg_dl_dlclose(clmp
, inv_buf
.buf
, DBG_DLCLOSE_NULL
));
579 eprintf(LIST(clmp
), ERR_FATAL
, MSG_INTL(MSG_ARG_INVHNDL
),
583 return (dlclose_intn(ghp
, clmp
));
586 #pragma weak _dlclose = dlclose
589 * External entry for dlclose(3dl). Returns 0 for success, non-zero otherwise.
592 dlclose(void *handle
)
599 clmp
= _caller(caller(), CL_EXECDEF
);
601 error
= dlclose_check(handle
, clmp
);
604 leave(LIST(clmp
), 0);
608 static uint_t lmid
= 0;
611 * The addition of new link-map lists is assumed to be in small quantities.
612 * Here, we assign a unique link-map id for diagnostic use. Simply update the
613 * running link-map count until we max out.
616 newlmid(Lm_list
*lml
)
618 char buffer
[MSG_LMID_ALT_SIZE
+ 12];
620 if (lmid
== UINT_MAX
) {
621 lml
->lm_lmid
= UINT_MAX
;
622 (void) strncpy(buffer
, MSG_ORIG(MSG_LMID_MAXED
),
623 MSG_LMID_ALT_SIZE
+ 12);
625 lml
->lm_lmid
= lmid
++;
626 (void) snprintf(buffer
, MSG_LMID_ALT_SIZE
+ 12,
627 MSG_ORIG(MSG_LMID_FMT
), MSG_ORIG(MSG_LMID_ALT
),
630 if ((lml
->lm_lmidstr
= strdup(buffer
)) == NULL
)
637 * Core dlopen activity.
640 dlmopen_core(Lm_list
*lml
, Lm_list
*olml
, const char *path
, int mode
,
641 Rt_map
*clmp
, uint_t flags
, uint_t orig
, int *in_nfavl
)
648 DBG_CALL(Dbg_dl_dlopen(clmp
,
649 (path
? path
: MSG_ORIG(MSG_STR_ZERO
)), in_nfavl
, mode
));
652 * Having diagnosed the originally defined modes, assign any defaults
655 if (((mode
& (RTLD_GROUP
| RTLD_WORLD
)) == 0) &&
656 ((mode
& RTLD_NOLOAD
) == 0))
657 mode
|= (RTLD_GROUP
| RTLD_WORLD
);
658 if ((mode
& RTLD_NOW
) && (rtld_flags2
& RT_FL2_BINDLAZY
)) {
664 * If the path specified is null then we're operating on global
665 * objects. Associate a dummy handle with the link-map list.
669 uint_t hflags
, rdflags
, cdflags
;
673 * Establish any flags for the handle (Grp_hdl).
675 * - This is a dummy, public, handle (0) that provides for a
676 * dynamic search of all global objects within the process.
677 * - Use of the RTLD_FIRST mode indicates that only the first
678 * dependency on the handle (the referenced object) can be
679 * used to satisfy dlsym() requests.
681 hflags
= (GPH_PUBLIC
| GPH_ZERO
);
682 if (mode
& RTLD_FIRST
)
686 * Establish the flags for the referenced dependency descriptor
689 * - The referenced object is available for dlsym().
690 * - The referenced object is available to relocate against.
691 * - The referenced object should have it's dependencies
692 * added to this handle.
694 rdflags
= (GPD_DLSYM
| GPD_RELOC
| GPD_ADDEPS
);
697 * Establish the flags for this callers dependency descriptor
700 * - The explicit creation of a handle creates a descriptor
701 * for the referenced object and the parent (caller).
702 * - Use of the RTLD_PARENT flag indicates that the parent
703 * can be relocated against.
705 cdflags
= GPD_PARENT
;
706 if (mode
& RTLD_PARENT
)
707 cdflags
|= GPD_RELOC
;
709 if ((ghp
= hdl_create(lml
, 0, clmp
, hflags
, rdflags
,
714 * Traverse the main link-map control list, updating the mode
715 * of any objects as necessary. Call the relocation engine if
716 * this mode promotes the existing state of any relocations.
717 * crle()'s first pass loads all objects necessary for building
718 * a configuration file, however none of them are relocated.
719 * crle()'s second pass relocates objects in preparation for
720 * dldump()'ing using dlopen(0, RTLD_NOW).
722 if ((mode
& (RTLD_NOW
| RTLD_CONFGEN
)) == RTLD_CONFGEN
)
725 for (nlmp
= lml
->lm_head
; nlmp
; nlmp
= NEXT_RT_MAP(nlmp
)) {
726 if (((MODE(nlmp
) & RTLD_GLOBAL
) == 0) ||
727 (FLAGS(nlmp
) & FLG_RT_DELETE
))
730 if (update_mode(nlmp
, MODE(nlmp
), mode
))
734 (void) relocate_lmc(lml
, ALIST_OFF_DATA
, clmp
,
735 lml
->lm_head
, in_nfavl
);
741 * Fix the pathname. If this object expands to multiple paths (ie.
742 * $ISALIST or $HWCAP have been used), then make sure the user has also
743 * furnished the RTLD_FIRST flag. As yet, we don't support opening
744 * more than one object at a time, so enforcing the RTLD_FIRST flag
745 * provides flexibility should we be able to support dlopening more
746 * than one object in the future.
748 if (LM_FIX_NAME(clmp
)(path
, clmp
, &palp
, AL_CNT_NEEDED
, orig
) == 0)
751 if ((palp
->al_arritems
> 1) && ((mode
& RTLD_FIRST
) == 0)) {
752 remove_alist(&palp
, 1);
753 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_ARG_ILLMODE_5
));
758 * Establish a link-map control list for this request, and load the
761 if ((nlmco
= create_cntl(lml
, 1)) == 0) {
762 remove_alist(&palp
, 1);
767 nlmp
= load_one(lml
, nlmco
, palp
, clmp
, mode
, (flags
| FLG_RT_PUBHDL
),
771 * Remove any expanded pathname infrastructure, and if the dependency
772 * couldn't be loaded, cleanup.
774 remove_alist(&palp
, 1);
776 remove_cntl(lml
, olmco
);
781 * If loading an auditor was requested, and the auditor already existed,
782 * then the link-map returned will be to the original auditor. The new
783 * link-map list that was initially created, and the associated link-map
784 * control list are no longer needed. As the auditor is already loaded,
785 * we're probably done, but fall through in case additional relocations
786 * would be triggered by the mode of the caller.
788 if ((flags
& FLG_RT_AUDIT
) && (LIST(nlmp
) != lml
)) {
789 remove_cntl(lml
, olmco
);
792 nlmco
= ALIST_OFF_DATA
;
796 * Finish processing the objects associated with this request.
798 if (((nlmp
= analyze_lmc(lml
, nlmco
, nlmp
, clmp
, in_nfavl
)) == NULL
) ||
799 (relocate_lmc(lml
, nlmco
, clmp
, nlmp
, in_nfavl
) == 0)) {
805 * If the dlopen has failed, clean up any objects that might have been
806 * loaded successfully on this new link-map control list.
808 if (olmco
&& (nlmp
== NULL
))
809 remove_lmc(lml
, clmp
, olmco
, path
);
812 * Finally, remove any temporary link-map control list. Note, if this
813 * operation successfully established a new link-map list, then a base
814 * link-map control list will have been created, which must remain.
816 if (olmco
&& ((nlmp
== NULL
) || (olml
!= (Lm_list
*)LM_ID_NEWLM
)))
817 remove_cntl(lml
, olmco
);
823 * dlopen() and dlsym() operations are the means by which a process can
824 * test for the existence of required dependencies. If the necessary
825 * dependencies don't exist, then associated functionality can't be used.
826 * However, the lack of dependencies can be fixed, and the dlopen() and
827 * dlsym() requests can be repeated. As we use a "not-found" AVL tree to
828 * cache any failed full path loads, secondary dlopen() and dlsym() requests
829 * will fail, even if the dependencies have been installed.
831 * dlopen() and dlsym() retry any failures by removing the "not-found" AVL
832 * tree. Should any dependencies be found, their names are added to the
833 * FullPath AVL tree. This routine removes any new "not-found" AVL tree,
834 * so that the dlopen() or dlsym() can replace the original "not-found" tree.
837 nfavl_remove(avl_tree_t
*avlt
)
843 while ((pnp
= avl_destroy_nodes(avlt
, &cookie
)) != NULL
)
852 * Internal dlopen() activity. Called from user level or directly for internal
853 * opens that require a handle.
856 dlmopen_intn(Lm_list
*lml
, const char *path
, int mode
, Rt_map
*clmp
,
857 uint_t flags
, uint_t orig
)
865 * Check for magic link-map list values:
867 * LM_ID_BASE: Operate on the PRIMARY (executables) link map
868 * LM_ID_LDSO: Operation on ld.so.1's link map
869 * LM_ID_NEWLM: Create a new link-map.
871 if (lml
== (Lm_list
*)LM_ID_NEWLM
) {
872 if ((lml
= calloc(sizeof (Lm_list
), 1)) == NULL
)
876 * Establish the new link-map flags from the callers and those
877 * explicitly provided.
879 lml
->lm_tflags
= LIST(clmp
)->lm_tflags
;
880 if (flags
& FLG_RT_AUDIT
) {
882 * Unset any auditing flags - an auditor shouldn't be
883 * audited. Insure all audit dependencies are loaded.
885 lml
->lm_tflags
&= ~LML_TFLG_AUD_MASK
;
886 lml
->lm_tflags
|= (LML_TFLG_NOLAZYLD
|
887 LML_TFLG_LOADFLTR
| LML_TFLG_NOAUDIT
);
890 if (aplist_append(&dynlm_list
, lml
, AL_CNT_DYNLIST
) == NULL
) {
894 if (newlmid(lml
) == 0) {
895 (void) aplist_delete_value(dynlm_list
, lml
);
899 } else if ((uintptr_t)lml
< LM_ID_NUM
) {
900 if ((uintptr_t)lml
== LM_ID_BASE
)
902 else if ((uintptr_t)lml
== LM_ID_LDSO
)
907 * Open the required object on the associated link-map list.
909 ghp
= dlmopen_core(lml
, olml
, path
, mode
, clmp
, flags
, orig
, &in_nfavl
);
912 * If the object could not be found it is possible that the "not-found"
913 * AVL tree had indicated that the file does not exist. In case the
914 * file system has changed since this "not-found" recording was made,
915 * retry the dlopen() with a clean "not-found" AVL tree.
917 if ((ghp
== NULL
) && in_nfavl
) {
918 avl_tree_t
*oavlt
= nfavl
;
921 ghp
= dlmopen_core(lml
, olml
, path
, mode
, clmp
, flags
, orig
,
925 * If the file is found, then its full path name will have been
926 * registered in the FullPath AVL tree. Remove any new
927 * "not-found" AVL information, and restore the former AVL tree.
934 * Establish the new link-map from which .init processing will begin.
935 * Ignore .init firing when constructing a configuration file (crle(1)).
937 if (ghp
&& ((mode
& RTLD_CONFGEN
) == 0))
938 dlmp
= ghp
->gh_ownlmp
;
941 * If loading an auditor was requested, and the auditor already existed,
942 * then the link-map returned will be to the original auditor. Remove
943 * the link-map control list that was created for this request.
945 if (dlmp
&& (flags
& FLG_RT_AUDIT
) && (LIST(dlmp
) != lml
)) {
951 * If this load failed, remove any alternative link-map list.
954 ((lml
->lm_flags
& (LML_FLG_BASELM
| LML_FLG_RTLDLM
)) == 0)) {
960 * Finish this load request. If objects were loaded, .init processing
961 * is computed. Finally, the debuggers are informed of the link-map
962 * lists being stable.
964 load_completion(dlmp
);
970 * Argument checking for dlopen. Only called via external entry.
973 dlmopen_check(Lm_list
*lml
, const char *path
, int mode
, Rt_map
*clmp
)
976 * Verify that a valid pathname has been supplied.
978 if (path
&& (*path
== '\0')) {
979 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_ARG_ILLPATH
));
984 * Historically we've always verified the mode is either RTLD_NOW or
985 * RTLD_LAZY. RTLD_NOLOAD is valid by itself. Use of LM_ID_NEWLM
986 * requires a specific pathname, and use of RTLD_PARENT is meaningless.
988 if ((mode
& (RTLD_NOW
| RTLD_LAZY
| RTLD_NOLOAD
)) == 0) {
989 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_ARG_ILLMODE_1
));
992 if ((mode
& (RTLD_NOW
| RTLD_LAZY
)) == (RTLD_NOW
| RTLD_LAZY
)) {
993 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_ARG_ILLMODE_2
));
996 if ((lml
== (Lm_list
*)LM_ID_NEWLM
) && (path
== NULL
)) {
997 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_ARG_ILLMODE_3
));
1000 if ((lml
== (Lm_list
*)LM_ID_NEWLM
) && (mode
& RTLD_PARENT
)) {
1001 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_ARG_ILLMODE_4
));
1005 return (dlmopen_intn(lml
, path
, mode
, clmp
, 0, 0));
1008 #pragma weak _dlopen = dlopen
1011 * External entry for dlopen(3dl). On success, returns a pointer (handle) to
1012 * the structure containing information about the newly added object, ie. can
1013 * be used by dlsym(). On failure, returns a null pointer.
1016 dlopen(const char *path
, int mode
)
1025 clmp
= _caller(caller(), CL_EXECDEF
);
1028 ghp
= dlmopen_check(lml
, path
, mode
, clmp
);
1032 return ((void *)ghp
);
1035 #pragma weak _dlmopen = dlmopen
1038 * External entry for dlmopen(3dl).
1041 dlmopen(Lmid_t lmid
, const char *path
, int mode
)
1049 clmp
= _caller(caller(), CL_EXECDEF
);
1051 ghp
= dlmopen_check((Lm_list
*)lmid
, path
, mode
, clmp
);
1054 leave(LIST(clmp
), 0);
1055 return ((void *)ghp
);
1059 * Handle processing for dlsym.
1062 dlsym_handle(Grp_hdl
*ghp
, Slookup
*slp
, Sresult
*srp
, uint_t
*binfo
,
1065 Rt_map
*nlmp
, * lmp
= ghp
->gh_ownlmp
;
1066 Rt_map
*clmp
= slp
->sl_cmap
;
1067 const char *name
= slp
->sl_name
;
1070 sl
.sl_flags
= (LKUP_FIRST
| LKUP_DLSYM
| LKUP_SPEC
);
1073 * Continue processing a dlsym request. Lookup the required symbol in
1074 * each link-map specified by the handle.
1076 * To leverage off of lazy loading, dlsym() requests can result in two
1077 * passes. The first descends the link-maps of any objects already in
1078 * the address space. If the symbol isn't located, and lazy
1079 * dependencies still exist, then a second pass is made to load these
1080 * dependencies if applicable. This model means that in the case where
1081 * a symbol exists in more than one object, the one located may not be
1082 * constant - this is the standard issue with lazy loading. In addition,
1083 * attempting to locate a symbol that doesn't exist will result in the
1084 * loading of all lazy dependencies on the given handle, which can
1085 * defeat some of the advantages of lazy loading (look out JVM).
1087 if (ghp
->gh_flags
& GPH_ZERO
) {
1092 * If this symbol lookup is triggered from a dlopen(0) handle,
1093 * traverse the present link-map list looking for promiscuous
1096 for (nlmp
= lmp
; nlmp
; nlmp
= NEXT_RT_MAP(nlmp
)) {
1098 * If this handle indicates we're only to look in the
1099 * first object check whether we're done.
1101 if ((nlmp
!= lmp
) && (ghp
->gh_flags
& GPH_FIRST
))
1104 if (!(MODE(nlmp
) & RTLD_GLOBAL
))
1106 if ((FLAGS(nlmp
) & FLG_RT_DELETE
) &&
1107 ((FLAGS(clmp
) & FLG_RT_DELETE
) == 0))
1111 if (LM_LOOKUP_SYM(clmp
)(&sl
, srp
, binfo
, in_nfavl
))
1115 * Keep track of any global pending lazy loads.
1121 * If we're unable to locate the symbol and this link-map list
1122 * still has pending lazy dependencies, start loading them in an
1123 * attempt to exhaust the search. Note that as we're already
1124 * traversing a dynamic linked list of link-maps there's no
1125 * need for elf_lazy_find_sym() to descend the link-maps itself.
1129 DBG_CALL(Dbg_syms_lazy_rescan(lml
, name
));
1131 sl
.sl_flags
|= LKUP_NODESCENT
;
1133 for (nlmp
= lmp
; nlmp
; nlmp
= NEXT_RT_MAP(nlmp
)) {
1135 if (!(MODE(nlmp
) & RTLD_GLOBAL
) || !LAZY(nlmp
))
1137 if ((FLAGS(nlmp
) & FLG_RT_DELETE
) &&
1138 ((FLAGS(clmp
) & FLG_RT_DELETE
) == 0))
1142 if (elf_lazy_find_sym(&sl
, srp
, binfo
,
1149 * Traverse the dlopen() handle searching all presently loaded
1156 for (ALIST_TRAVERSE(ghp
->gh_depends
, idx
, gdp
)) {
1157 nlmp
= gdp
->gd_depend
;
1159 if ((gdp
->gd_flags
& GPD_DLSYM
) == 0)
1163 if (LM_LOOKUP_SYM(clmp
)(&sl
, srp
, binfo
, in_nfavl
))
1166 if (ghp
->gh_flags
& GPH_FIRST
)
1170 * Keep track of any pending lazy loads associated
1177 * If we're unable to locate the symbol and this handle still
1178 * has pending lazy dependencies, start loading the lazy
1179 * dependencies, in an attempt to exhaust the search.
1182 DBG_CALL(Dbg_syms_lazy_rescan(LIST(lmp
), name
));
1184 for (ALIST_TRAVERSE(ghp
->gh_depends
, idx
, gdp
)) {
1185 nlmp
= gdp
->gd_depend
;
1187 if (((gdp
->gd_flags
& GPD_DLSYM
) == 0) ||
1192 if (elf_lazy_find_sym(&sl
, srp
, binfo
,
1202 * Determine whether a symbol resides in a caller. This may be a reference,
1203 * which is associated with a specific dependency.
1206 sym_lookup_in_caller(Rt_map
*clmp
, Slookup
*slp
, Sresult
*srp
, uint_t
*binfo
)
1208 if (THIS_IS_ELF(clmp
) && SYMINTP(clmp
)(slp
, srp
, binfo
, NULL
)) {
1209 Sym
*sym
= srp
->sr_sym
;
1211 slp
->sl_rsymndx
= (((ulong_t
)sym
-
1212 (ulong_t
)SYMTAB(clmp
)) / SYMENT(clmp
));
1220 * Core dlsym activity. Selects symbol lookup method from handle.
1223 dlsym_core(void *handle
, const char *name
, Rt_map
*clmp
, Rt_map
**dlmp
,
1234 * Initialize the symbol lookup data structure.
1236 * Standard relocations are evaluated using the symbol index of the
1237 * associated relocation symbol. This index provides for loading
1238 * any lazy dependency and establishing a direct binding if necessary.
1239 * If a dlsym() operation originates from an object that contains a
1240 * symbol table entry for the same name, then we need to establish the
1241 * symbol index so that any dependency requirements can be triggered.
1243 * Therefore, the first symbol lookup that is carried out is for the
1244 * symbol name within the calling object. If this symbol exists, the
1245 * symbols index is computed, added to the Slookup data, and thus used
1246 * to seed the real symbol lookup.
1248 SLOOKUP_INIT(sl
, name
, clmp
, clmp
, ld_entry_cnt
, elf_hash(name
),
1249 0, 0, 0, LKUP_SYMNDX
);
1250 SRESULT_INIT(sr
, name
);
1251 sym
= sym_lookup_in_caller(clmp
, &sl
, &sr
, &binfo
);
1253 SRESULT_INIT(sr
, name
);
1255 if (sym
&& (ELF_ST_VISIBILITY(sym
->st_other
) == STV_SINGLETON
)) {
1256 Rt_map
*hlmp
= LIST(clmp
)->lm_head
;
1259 * If a symbol reference is known, and that reference indicates
1260 * that the symbol is a singleton, then the search for the
1261 * symbol must follow the default search path.
1263 DBG_CALL(Dbg_dl_dlsym(clmp
, name
, in_nfavl
, 0,
1264 DBG_DLSYM_SINGLETON
));
1267 if (handle
== RTLD_PROBE
)
1268 sl
.sl_flags
= LKUP_NOFALLBACK
;
1270 sl
.sl_flags
= LKUP_SPEC
;
1271 ret
= LM_LOOKUP_SYM(clmp
)(&sl
, &sr
, &binfo
, in_nfavl
);
1273 } else if (handle
== RTLD_NEXT
) {
1277 * If this handle is RTLD_NEXT determine whether a lazy load
1278 * from the caller might provide the next object. This mimics
1279 * the lazy loading initialization normally carried out by
1280 * lookup_sym(), however here, we must do this up-front, as
1281 * lookup_sym() will be used to inspect the next object.
1283 if ((sl
.sl_rsymndx
) && ((sip
= SYMINFO(clmp
)) != NULL
)) {
1285 sip
= (Syminfo
*)((char *)sip
+
1286 (sl
.sl_rsymndx
* SYMINENT(clmp
)));
1288 if ((sip
->si_flags
& SYMINFO_FLG_DIRECT
) &&
1289 (sip
->si_boundto
< SYMINFO_BT_LOWRESERVE
))
1290 (void) elf_lazy_load(clmp
, &sl
,
1291 sip
->si_boundto
, name
, 0, NULL
, in_nfavl
);
1294 * Clear the symbol index, so as not to confuse
1295 * lookup_sym() of the next object.
1302 * If the handle is RTLD_NEXT, start searching in the next link
1303 * map from the callers. Determine permissions from the
1304 * present link map. Indicate to lookup_sym() that we're on an
1305 * RTLD_NEXT request so that it will use the callers link map to
1306 * start any possible lazy dependency loading.
1308 sl
.sl_imap
= nlmp
= NEXT_RT_MAP(clmp
);
1310 DBG_CALL(Dbg_dl_dlsym(clmp
, name
, in_nfavl
,
1311 (nlmp
? NAME(nlmp
) : MSG_INTL(MSG_STR_NULL
)),
1317 sl
.sl_flags
= LKUP_NEXT
;
1318 ret
= LM_LOOKUP_SYM(clmp
)(&sl
, &sr
, &binfo
, in_nfavl
);
1320 } else if (handle
== RTLD_SELF
) {
1322 * If the handle is RTLD_SELF start searching from the caller.
1324 DBG_CALL(Dbg_dl_dlsym(clmp
, name
, in_nfavl
, NAME(clmp
),
1328 sl
.sl_flags
= (LKUP_SPEC
| LKUP_SELF
);
1329 ret
= LM_LOOKUP_SYM(clmp
)(&sl
, &sr
, &binfo
, in_nfavl
);
1331 } else if (handle
== RTLD_DEFAULT
) {
1332 Rt_map
*hlmp
= LIST(clmp
)->lm_head
;
1335 * If the handle is RTLD_DEFAULT mimic the standard symbol
1336 * lookup as would be triggered by a relocation.
1338 DBG_CALL(Dbg_dl_dlsym(clmp
, name
, in_nfavl
, 0,
1339 DBG_DLSYM_DEFAULT
));
1342 sl
.sl_flags
= LKUP_SPEC
;
1343 ret
= LM_LOOKUP_SYM(clmp
)(&sl
, &sr
, &binfo
, in_nfavl
);
1345 } else if (handle
== RTLD_PROBE
) {
1346 Rt_map
*hlmp
= LIST(clmp
)->lm_head
;
1349 * If the handle is RTLD_PROBE, mimic the standard symbol
1350 * lookup as would be triggered by a relocation, however do
1351 * not fall back to a lazy loading rescan if the symbol can't be
1352 * found within the currently loaded objects. Note, a lazy
1353 * loaded dependency required by the caller might still get
1354 * loaded to satisfy this request, but no exhaustive lazy load
1355 * rescan is carried out.
1357 DBG_CALL(Dbg_dl_dlsym(clmp
, name
, in_nfavl
, 0,
1361 sl
.sl_flags
= LKUP_NOFALLBACK
;
1362 ret
= LM_LOOKUP_SYM(clmp
)(&sl
, &sr
, &binfo
, in_nfavl
);
1365 Grp_hdl
*ghp
= (Grp_hdl
*)handle
;
1368 * Look in the shared object specified by the handle and in all
1369 * of its dependencies.
1371 DBG_CALL(Dbg_dl_dlsym(clmp
, name
, in_nfavl
,
1372 NAME(ghp
->gh_ownlmp
), DBG_DLSYM_DEF
));
1374 ret
= LM_DLSYM(clmp
)(ghp
, &sl
, &sr
, &binfo
, in_nfavl
);
1377 if (ret
&& ((sym
= sr
.sr_sym
) != NULL
)) {
1378 Lm_list
*lml
= LIST(clmp
);
1379 Addr addr
= sym
->st_value
;
1382 if (!(FLAGS(*dlmp
) & FLG_RT_FIXED
))
1383 addr
+= ADDR(*dlmp
);
1386 * Indicate that the defining object is now used.
1389 FLAGS1(*dlmp
) |= FL1_RT_USED
;
1391 DBG_CALL(Dbg_bind_global(clmp
, 0, 0, (Xword
)-1, PLT_T_NONE
,
1392 *dlmp
, addr
, sym
->st_value
, sr
.sr_name
, binfo
));
1394 if ((lml
->lm_tflags
| AFLAGS(clmp
) | AFLAGS(*dlmp
)) &
1395 LML_TFLG_AUD_SYMBIND
) {
1396 uint_t sb_flags
= LA_SYMB_DLSYM
;
1398 uint_t symndx
= (uint_t
)(((Xword
)sym
-
1399 (Xword
)SYMTAB(*dlmp
)) / SYMENT(*dlmp
));
1400 addr
= audit_symbind(clmp
, *dlmp
, sym
, symndx
, addr
,
1403 return ((void *)addr
);
1410 * Internal dlsym activity. Called from user level or directly for internal
1414 dlsym_intn(void *handle
, const char *name
, Rt_map
*clmp
, Rt_map
**dlmp
)
1416 Rt_map
*llmp
= NULL
;
1423 * While looking for symbols it's quite possible that additional objects
1424 * get loaded from lazy loading. These objects will have been added to
1425 * the same link-map list as those objects on the handle. Remember this
1426 * list for later investigation.
1428 if ((handle
== RTLD_NEXT
) || (handle
== RTLD_DEFAULT
) ||
1429 (handle
== RTLD_SELF
) || (handle
== RTLD_PROBE
))
1430 llmp
= LIST(clmp
)->lm_tail
;
1432 Grp_hdl
*ghp
= (Grp_hdl
*)handle
;
1435 llmp
= LIST(ghp
->gh_ownlmp
)->lm_tail
;
1437 for (ALIST_TRAVERSE(ghp
->gh_depends
, idx
, gdp
)) {
1439 LIST(gdp
->gd_depend
)->lm_tail
) != NULL
)
1445 error
= dlsym_core(handle
, name
, clmp
, dlmp
, &in_nfavl
);
1448 * If the symbol could not be found it is possible that the "not-found"
1449 * AVL tree had indicated that a required file does not exist. In case
1450 * the file system has changed since this "not-found" recording was
1451 * made, retry the dlsym() with a clean "not-found" AVL tree.
1453 if ((error
== NULL
) && in_nfavl
) {
1454 avl_tree_t
*oavlt
= nfavl
;
1457 error
= dlsym_core(handle
, name
, clmp
, dlmp
, NULL
);
1460 * If the symbol is found, then any file that was loaded will
1461 * have had its full path name registered in the FullPath AVL
1462 * tree. Remove any new "not-found" AVL information, and
1463 * restore the former AVL tree.
1465 nfavl_remove(nfavl
);
1469 if (error
== NULL
) {
1471 * Cache the error message, as Java tends to fall through this
1474 if (nosym_str
== NULL
)
1475 nosym_str
= MSG_INTL(MSG_GEN_NOSYM
);
1476 eprintf(LIST(clmp
), ERR_FATAL
, nosym_str
, name
);
1479 load_completion(llmp
);
1484 * Argument checking for dlsym. Only called via external entry.
1487 dlsym_check(void *handle
, const char *name
, Rt_map
*clmp
, Rt_map
**dlmp
)
1490 * Verify the arguments.
1493 eprintf(LIST(clmp
), ERR_FATAL
, MSG_INTL(MSG_ARG_ILLSYM
));
1496 if ((handle
!= RTLD_NEXT
) && (handle
!= RTLD_DEFAULT
) &&
1497 (handle
!= RTLD_SELF
) && (handle
!= RTLD_PROBE
) &&
1498 (hdl_validate((Grp_hdl
*)handle
) == 0)) {
1499 eprintf(LIST(clmp
), ERR_FATAL
, MSG_INTL(MSG_ARG_INVHNDL
),
1503 return (dlsym_intn(handle
, name
, clmp
, dlmp
));
1507 #pragma weak _dlsym = dlsym
1510 * External entry for dlsym(). On success, returns the address of the specified
1511 * symbol. On error returns a null.
1514 dlsym(void *handle
, const char *name
)
1517 Rt_map
*clmp
, *dlmp
= NULL
;
1522 clmp
= _caller(caller(), CL_EXECDEF
);
1524 addr
= dlsym_check(handle
, name
, clmp
, &dlmp
);
1528 is_dep_init(dlmp
, clmp
);
1529 leave(LIST(clmp
), 0);
1535 * Core dladdr activity.
1538 dladdr_core(Rt_map
*almp
, void *addr
, Dl_info_t
*dlip
, void **info
, int flags
)
1541 * Set up generic information and any defaults.
1543 dlip
->dli_fname
= PATHNAME(almp
);
1545 dlip
->dli_fbase
= (void *)ADDR(almp
);
1546 dlip
->dli_sname
= NULL
;
1547 dlip
->dli_saddr
= NULL
;
1550 * Determine the nearest symbol to this address.
1552 LM_DLADDR(almp
)((ulong_t
)addr
, almp
, dlip
, info
, flags
);
1555 #pragma weak _dladdr = dladdr
1558 * External entry for dladdr(3dl) and dladdr1(3dl). Returns an information
1559 * structure that reflects the symbol closest to the address specified.
1562 dladdr(void *addr
, Dl_info_t
*dlip
)
1565 Rt_map
*clmp
, *almp
;
1570 clmp
= _caller(caller(), CL_EXECDEF
);
1573 DBG_CALL(Dbg_dl_dladdr(clmp
, addr
));
1576 * Use our calling technique to determine what object is associated
1577 * with the supplied address. If a caller can't be determined,
1578 * indicate the failure.
1580 if ((almp
= _caller(addr
, CL_NONE
)) == NULL
) {
1581 eprintf(clml
, ERR_FATAL
, MSG_INTL(MSG_ARG_INVADDR
),
1585 dladdr_core(almp
, addr
, dlip
, 0, 0);
1594 #pragma weak _dladdr1 = dladdr1
1597 dladdr1(void *addr
, Dl_info_t
*dlip
, void **info
, int flags
)
1600 Rt_map
*clmp
, *almp
;
1605 clmp
= _caller(caller(), CL_EXECDEF
);
1608 DBG_CALL(Dbg_dl_dladdr(clmp
, addr
));
1611 * Validate any flags.
1616 if (((request
= (flags
& RTLD_DL_MASK
)) != RTLD_DL_SYMENT
) &&
1617 (request
!= RTLD_DL_LINKMAP
)) {
1618 eprintf(clml
, ERR_FATAL
, MSG_INTL(MSG_ARG_ILLFLAGS
),
1622 } else if (info
== NULL
) {
1623 eprintf(clml
, ERR_FATAL
, MSG_INTL(MSG_ARG_ILLINFO
),
1630 * Use our calling technique to determine what object is associated
1631 * with the supplied address. If a caller can't be determined,
1632 * indicate the failure.
1635 if ((almp
= _caller(addr
, CL_NONE
)) == NULL
) {
1636 eprintf(clml
, ERR_FATAL
, MSG_INTL(MSG_ARG_INVADDR
),
1640 dladdr_core(almp
, addr
, dlip
, info
, flags
);
1649 * Core dldump activity.
1652 dldump_core(Rt_map
*clmp
, Rt_map
*lmp
, const char *ipath
, const char *opath
,
1655 Lm_list
*lml
= LIST(clmp
);
1659 * Verify any arguments first.
1661 if ((opath
== NULL
) || (opath
[0] == '\0') ||
1662 ((lmp
== NULL
) && (ipath
[0] == '\0'))) {
1663 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_ARG_ILLPATH
));
1668 * If an input file is specified make sure its one of our dependencies
1669 * on the main link-map list. Note, this has really all evolved for
1670 * crle(), which uses libcrle.so on an alternative link-map to trigger
1671 * dumping objects from the main link-map list. If we ever want to
1672 * dump objects from alternative link-maps, this model is going to
1673 * have to be revisited.
1676 if ((lmp
= is_so_loaded(&lml_main
, ipath
, NULL
)) == NULL
) {
1677 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_GEN_NOFILE
),
1681 if (FLAGS(lmp
) & FLG_RT_ALTER
) {
1682 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_GEN_ALTER
), ipath
);
1685 if (FLAGS(lmp
) & FLG_RT_NODUMP
) {
1686 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_GEN_NODUMP
),
1693 * If the object being dump'ed isn't fixed identify its mapping.
1695 if (!(FLAGS(lmp
) & FLG_RT_FIXED
))
1699 * As rt_dldump() will effectively lazy load the necessary support
1700 * libraries, make sure ld.so.1 is initialized for plt relocations.
1702 if (elf_rtld_load() == 0)
1706 * Dump the required image.
1708 return (rt_dldump(lmp
, opath
, flags
, addr
));
1711 #pragma weak _dldump = dldump
1714 * External entry for dldump(3c). Returns 0 on success, non-zero otherwise.
1717 dldump(const char *ipath
, const char *opath
, int flags
)
1724 clmp
= _caller(caller(), CL_EXECDEF
);
1729 lmp
= lml_main
.lm_head
;
1733 DBG_CALL(Dbg_dl_dldump(clmp
, ipath
, opath
, flags
));
1735 error
= dldump_core(clmp
, lmp
, ipath
, opath
, flags
);
1738 leave(LIST(clmp
), 0);
1743 * get_linkmap_id() translates Lm_list * pointers to the Link_map id as used by
1744 * the rtld_db and dlmopen() interfaces. It checks to see if the Link_map is
1745 * one of the primary ones and if so returns it's special token:
1749 * If it's not one of the primary link_map id's it will instead returns a
1750 * pointer to the Lm_list structure which uniquely identifies the Link_map.
1753 get_linkmap_id(Lm_list
*lml
)
1755 if (lml
->lm_flags
& LML_FLG_BASELM
)
1756 return (LM_ID_BASE
);
1757 if (lml
->lm_flags
& LML_FLG_RTLDLM
)
1758 return (LM_ID_LDSO
);
1760 return ((Lmid_t
)lml
);
1764 * Set a new deferred dependency name.
1767 set_def_need(Lm_list
*lml
, Dyninfo
*dyip
, const char *name
)
1770 * If this dependency has already been established, then this dlinfo()
1773 if (dyip
->di_info
) {
1774 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_DEF_DEPLOADED
),
1780 * Assign the new dependency name.
1782 DBG_CALL(Dbg_file_deferred(lml
, dyip
->di_name
, name
));
1783 dyip
->di_flags
|= FLG_DI_DEF_DONE
;
1784 dyip
->di_name
= name
;
1789 * Extract information for a dlopen() handle.
1792 dlinfo_core(void *handle
, int request
, void *p
, Rt_map
*clmp
)
1794 Conv_inv_buf_t inv_buf
;
1796 Lm_list
*lml
= LIST(clmp
);
1800 * Determine whether a handle is provided. A handle isn't needed for
1801 * all operations, but it is validated here for the initial diagnostic.
1803 if (handle
== RTLD_SELF
) {
1806 Grp_hdl
*ghp
= (Grp_hdl
*)handle
;
1808 if (hdl_validate(ghp
))
1809 lmp
= ghp
->gh_ownlmp
;
1812 handlename
= NAME(lmp
);
1814 (void) conv_invalid_val(&inv_buf
, EC_NATPTR(handle
), 0);
1815 handlename
= inv_buf
.buf
;
1818 DBG_CALL(Dbg_dl_dlinfo(clmp
, handlename
, request
, p
));
1821 * Validate the request and return buffer.
1823 if ((request
> RTLD_DI_MAX
) || (p
== NULL
)) {
1824 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_ARG_ILLVAL
));
1829 * Return configuration cache name and address.
1831 if (request
== RTLD_DI_CONFIGADDR
) {
1832 Dl_info_t
*dlip
= (Dl_info_t
*)p
;
1834 if ((config
->c_name
== NULL
) || (config
->c_bgn
== 0) ||
1835 (config
->c_end
== 0)) {
1836 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_ARG_NOCONFIG
));
1839 dlip
->dli_fname
= config
->c_name
;
1840 dlip
->dli_fbase
= (void *)config
->c_bgn
;
1845 * Return profiled object name (used by ldprof audit library).
1847 if (request
== RTLD_DI_PROFILENAME
) {
1848 if (profile_name
== NULL
) {
1849 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_ARG_NOPROFNAME
));
1853 *(const char **)p
= profile_name
;
1856 if (request
== RTLD_DI_PROFILEOUT
) {
1858 * If a profile destination directory hasn't been specified
1859 * provide a default.
1861 if (profile_out
== NULL
)
1862 profile_out
= MSG_ORIG(MSG_PTH_VARTMP
);
1864 *(const char **)p
= profile_out
;
1869 * Obtain or establish a termination signal.
1871 if (request
== RTLD_DI_GETSIGNAL
) {
1872 *(int *)p
= killsig
;
1876 if (request
== RTLD_DI_SETSIGNAL
) {
1878 int sig
= *(int *)p
;
1881 * Determine whether the signal is in range.
1883 (void) sigfillset(&set
);
1884 if (sigismember(&set
, sig
) != 1) {
1885 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_ARG_INVSIG
), sig
);
1894 * For any other request a link-map is required. Verify the handle.
1897 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_ARG_INVHNDL
),
1903 * Obtain the process arguments, environment and auxv. Note, as the
1904 * environment can be modified by the user (putenv(3c)), reinitialize
1905 * the environment pointer on each request.
1907 if (request
== RTLD_DI_ARGSINFO
) {
1908 Dl_argsinfo_t
*aip
= (Dl_argsinfo_t
*)p
;
1909 Lm_list
*lml
= LIST(lmp
);
1912 if (lml
->lm_flags
& LML_FLG_ENVIRON
)
1913 aip
->dla_envp
= *(lml
->lm_environ
);
1919 * Return Lmid_t of the Link-Map list that the specified object is
1922 if (request
== RTLD_DI_LMID
) {
1923 *(Lmid_t
*)p
= get_linkmap_id(LIST(lmp
));
1928 * Return a pointer to the Link-Map structure associated with the
1931 if (request
== RTLD_DI_LINKMAP
) {
1932 *(Link_map
**)p
= (Link_map
*)lmp
;
1937 * Return search path information, or the size of the buffer required
1938 * to store the information.
1940 if ((request
== RTLD_DI_SERINFO
) || (request
== RTLD_DI_SERINFOSIZE
)) {
1941 Spath_desc sd
= { search_rules
, NULL
, 0 };
1946 size_t size
= sizeof (Dl_serinfo_t
);
1949 info
= (Dl_serinfo_t
*)p
;
1950 path
= &info
->dls_serpath
[0];
1951 strs
= (char *)&info
->dls_serpath
[info
->dls_cnt
];
1954 * Traverse search path entries for this object.
1956 while ((pdp
= get_next_dir(&sd
, lmp
, 0)) != NULL
) {
1959 if (pdp
->pd_pname
== NULL
)
1963 * If configuration information exists, it's possible
1964 * this path has been identified as non-existent, if so
1968 Rtc_obj
*dobj
= (Rtc_obj
*)pdp
->pd_info
;
1969 if (dobj
->co_flags
& RTC_OBJ_NOEXIST
)
1974 * Keep track of search path count and total info size.
1977 size
+= sizeof (Dl_serpath_t
);
1978 _size
= pdp
->pd_plen
+ 1;
1981 if (request
== RTLD_DI_SERINFOSIZE
)
1985 * If we're filling in search path information, confirm
1986 * there's sufficient space.
1988 if (size
> info
->dls_size
) {
1989 eprintf(lml
, ERR_FATAL
,
1990 MSG_INTL(MSG_ARG_SERSIZE
),
1991 EC_OFF(info
->dls_size
));
1994 if (cnt
> info
->dls_cnt
) {
1995 eprintf(lml
, ERR_FATAL
,
1996 MSG_INTL(MSG_ARG_SERCNT
), info
->dls_cnt
);
2001 * Append the path to the information buffer.
2003 (void) strcpy(strs
, pdp
->pd_pname
);
2004 path
->dls_name
= strs
;
2005 path
->dls_flags
= (pdp
->pd_flags
& LA_SER_MASK
);
2007 strs
= strs
+ _size
;
2012 * If we're here to size the search buffer fill it in.
2014 if (request
== RTLD_DI_SERINFOSIZE
) {
2015 info
->dls_size
= size
;
2016 info
->dls_cnt
= cnt
;
2023 * Return the origin of the object associated with this link-map.
2024 * Basically return the dirname(1) of the objects fullpath.
2026 if (request
== RTLD_DI_ORIGIN
) {
2027 char *str
= (char *)p
;
2029 (void) strncpy(str
, ORIGNAME(lmp
), DIRSZ(lmp
));
2037 * Return the number of object mappings, or the mapping information for
2040 if (request
== RTLD_DI_MMAPCNT
) {
2041 uint_t
*cnt
= (uint_t
*)p
;
2043 *cnt
= MMAPCNT(lmp
);
2046 if (request
== RTLD_DI_MMAPS
) {
2047 Dl_mapinfo_t
*mip
= (Dl_mapinfo_t
*)p
;
2049 if (mip
->dlm_acnt
&& mip
->dlm_maps
) {
2052 while ((cnt
< mip
->dlm_acnt
) && (cnt
< MMAPCNT(lmp
))) {
2053 mip
->dlm_maps
[cnt
] = MMAPS(lmp
)[cnt
];
2056 mip
->dlm_rcnt
= cnt
;
2062 * Assign a new dependency name to a deferred dependency.
2064 if ((request
== RTLD_DI_DEFERRED
) ||
2065 (request
== RTLD_DI_DEFERRED_SYM
)) {
2066 Dl_definfo_t
*dfip
= (Dl_definfo_t
*)p
;
2068 const char *dname
, *rname
;
2073 if ((dfip
->dld_refname
== NULL
) ||
2074 (dfip
->dld_depname
== NULL
)) {
2075 eprintf(LIST(clmp
), ERR_FATAL
,
2076 MSG_INTL(MSG_ARG_ILLNAME
));
2080 dname
= dfip
->dld_depname
;
2081 rname
= dfip
->dld_refname
;
2084 * A deferred dependency can be determined by referencing a
2085 * symbol family member that is associated to the dependency,
2086 * or by looking for the dependency by its name.
2088 if (request
== RTLD_DI_DEFERRED_SYM
) {
2095 * Lookup the symbol in the associated object.
2097 SLOOKUP_INIT(sl
, rname
, lmp
, lmp
, ld_entry_cnt
,
2098 elf_hash(rname
), 0, 0, 0, LKUP_SYMNDX
);
2099 SRESULT_INIT(sr
, rname
);
2100 if (sym_lookup_in_caller(clmp
, &sl
, &sr
,
2102 eprintf(LIST(clmp
), ERR_FATAL
,
2103 MSG_INTL(MSG_DEF_NOSYMFOUND
), rname
);
2108 * Use the symbols index to reference the Syminfo entry
2109 * and thus find the associated dependency.
2111 if (sl
.sl_rsymndx
&& ((sip
= SYMINFO(clmp
)) != NULL
)) {
2113 sip
= (Syminfo
*)((char *)sip
+
2114 (sl
.sl_rsymndx
* SYMINENT(lmp
)));
2116 if ((sip
->si_flags
& SYMINFO_FLG_DEFERRED
) &&
2117 (sip
->si_boundto
< SYMINFO_BT_LOWRESERVE
) &&
2118 ((dyip
= DYNINFO(lmp
)) != NULL
)) {
2119 dyip
+= sip
->si_boundto
;
2121 if (!(dyip
->di_flags
& FLG_DI_IGNORE
))
2122 return (set_def_need(lml
,
2128 * No deferred symbol found.
2130 eprintf(LIST(clmp
), ERR_FATAL
,
2131 MSG_INTL(MSG_DEF_NOSYMFOUND
), rname
);
2138 * Using the target objects dependency information, find
2139 * the associated deferred dependency.
2141 for (dyn
= DYN(lmp
), dyip
= DYNINFO(lmp
);
2142 !(dyip
->di_flags
& FLG_DI_IGNORE
); dyn
++, dyip
++) {
2145 if ((dyip
->di_flags
& FLG_DI_DEFERRED
) == 0)
2148 if (strcmp(rname
, dyip
->di_name
) == 0)
2149 return (set_def_need(lml
, dyip
, dname
));
2152 * If this dependency name has been changed by
2153 * a previous dlinfo(), check the original
2154 * dynamic entry string. The user might be
2155 * attempting to re-change an entry using the
2156 * original name as the reference.
2158 if ((dyip
->di_flags
& FLG_DI_DEF_DONE
) == 0)
2161 oname
= STRTAB(lmp
) + dyn
->d_un
.d_val
;
2162 if (strcmp(rname
, oname
) == 0)
2163 return (set_def_need(lml
, dyip
, dname
));
2167 * No deferred dependency found.
2169 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_DEF_NODEPFOUND
),
2177 #pragma weak _dlinfo = dlinfo
2180 * External entry for dlinfo(3dl).
2183 dlinfo(void *handle
, int request
, void *p
)
2190 clmp
= _caller(caller(), CL_EXECDEF
);
2192 error
= dlinfo_core(handle
, request
, p
, clmp
);
2195 leave(LIST(clmp
), 0);
2200 * GNU defined function to iterate through the program headers for all
2201 * currently loaded dynamic objects. The caller supplies a callback function
2202 * which is called for each object.
2205 * callback - Callback function to call. The arguments to the callback
2207 * info - Address of dl_phdr_info structure
2208 * size - sizeof (struct dl_phdr_info)
2209 * data - Caller supplied value.
2210 * data - Value supplied by caller, which is passed to callback without
2214 * callback is called for each dynamic ELF object in the process address
2215 * space, halting when a non-zero value is returned, or when the last
2216 * object has been processed. The return value from the last call
2217 * to callback is returned.
2220 * The Linux implementation has added additional fields to the
2221 * dl_phdr_info structure over time. The callback function is
2222 * supposed to use the size field to determine which fields are
2223 * present, and to avoid attempts to access non-existent fields.
2224 * We have added those fields that are compatible with Solaris, and
2225 * which are used by GNU C++ (g++) runtime exception handling support.
2228 * We issue a callback for every ELF object mapped into the process
2229 * address space at the time this routine is entered. These callbacks
2230 * are arbitrary functions that can do anything, including possibly
2231 * causing new objects to be mapped into the process, or unmapped.
2232 * This complicates matters:
2234 * - Adding new objects can cause the alists to be reallocated
2235 * or for contents to move. This can happen explicitly via
2236 * dlopen(), or implicitly via lazy loading. One might consider
2237 * simply banning dlopen from a callback, but lazy loading must
2238 * be allowed, in which case there's no reason to ban dlopen().
2240 * - Removing objects can leave us holding references to freed
2241 * memory that must not be accessed, and can cause the list
2242 * items to move in a way that would cause us to miss reporting
2243 * one, or double report others.
2245 * - We cannot allocate memory to build a separate data structure,
2246 * because the interface to dl_iterate_phdr() does not have a
2247 * way to communicate allocation errors back to the caller.
2248 * Even if we could, it would be difficult to do so efficiently.
2250 * - It is possible for dl_iterate_phdr() to be called recursively
2251 * from a callback, and there is no way for us to detect or manage
2252 * this effectively, particularly as the user might use longjmp()
2253 * to skip past us on return. Hence, we must be reentrant
2254 * (stateless), further precluding the option of building a
2255 * separate data structure.
2257 * Despite these constraints, we are able to traverse the link-map
2260 * - Once interposer (preload) objects have been processed at
2261 * startup, we know that new objects are always placed at the
2262 * end of the list. Hence, if we are reading a list when that
2263 * happens, the new object will not alter the part of the list
2264 * that we've already processed.
2266 * - The alist _TRAVERSE macros recalculate the address of the
2267 * current item from scratch on each iteration, rather than
2268 * incrementing a pointer. Hence, alist additions that occur
2269 * in mid-traverse will not cause confusion.
2271 * There is one limitation: We cannot continue operation if an object
2272 * is removed from the process from within a callback. We detect when
2273 * this happens and return immediately with a -1 return value.
2276 * As currently implemented, if a callback causes an object to be loaded,
2277 * that object may or may not be reported by the current invocation of
2278 * dl_iterate_phdr(), based on whether or not we have already processed
2279 * the link-map list that receives it. If we want to prevent this, it
2280 * can be done efficiently by associating the current value of cnt_map
2281 * with each new Rt_map entered into the system. Then this function can
2282 * use that to detect and skip new objects that enter the system in
2283 * mid-iteration. However, the Linux documentation is ambiguous on whether
2284 * this is necessary, and it does not appear to matter in practice.
2285 * We have therefore chosen not to do so at this time.
2288 dl_iterate_phdr(int (*callback
)(struct dl_phdr_info
*, size_t, void *),
2291 struct dl_phdr_info info
;
2292 u_longlong_t l_cnt_map
= cnt_map
;
2293 u_longlong_t l_cnt_unmap
= cnt_unmap
;
2294 Lm_list
*lml
, *clml
;
2303 clmp
= _caller(caller(), CL_EXECDEF
);
2306 DBG_CALL(Dbg_dl_iphdr_enter(clmp
, cnt_map
, cnt_unmap
));
2308 /* Issue a callback for each ELF object in the process */
2309 for (APLIST_TRAVERSE(dynlm_list
, idx1
, lml
)) {
2310 for (ALIST_TRAVERSE(lml
->lm_lists
, idx2
, lmc
)) {
2311 for (lmp
= lmc
->lc_head
; lmp
; lmp
= NEXT_RT_MAP(lmp
)) {
2312 #if defined(_sparc) && !defined(_LP64)
2314 * On 32-bit sparc, the possibility exists that
2315 * this object is not ELF.
2317 if (THIS_IS_NOT_ELF(lmp
))
2320 /* Prepare the object information structure */
2321 ehdr
= (Ehdr
*) ADDR(lmp
);
2322 info
.dlpi_addr
= (ehdr
->e_type
== ET_EXEC
) ?
2324 info
.dlpi_name
= lmp
->rt_pathname
;
2325 info
.dlpi_phdr
= (Phdr
*)
2326 (ADDR(lmp
) + ehdr
->e_phoff
);
2327 info
.dlpi_phnum
= ehdr
->e_phnum
;
2328 info
.dlpi_adds
= cnt_map
;
2329 info
.dlpi_subs
= cnt_unmap
;
2331 /* Issue the callback */
2332 DBG_CALL(Dbg_dl_iphdr_callback(clml
, &info
));
2333 leave(clml
, thr_flg_reenter
);
2334 ret
= (* callback
)(&info
, sizeof (info
), data
);
2335 (void) enter(thr_flg_reenter
);
2337 /* Return immediately on non-zero result */
2341 /* Adapt to object mapping changes */
2342 if ((cnt_map
== l_cnt_map
) &&
2343 (cnt_unmap
== l_cnt_unmap
))
2346 DBG_CALL(Dbg_dl_iphdr_mapchange(clml
, cnt_map
,
2349 /* Stop if an object was unmapped */
2350 if (cnt_unmap
== l_cnt_unmap
) {
2351 l_cnt_map
= cnt_map
;
2356 DBG_CALL(Dbg_dl_iphdr_unmap_ret(clml
));
2364 leave(LIST(clmp
), 0);