dlfcns.c revision 8af2c5b9bdbf69a55f079d7ad9483d38fae9f023
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1988 AT&T
* All Rights Reserved
*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Programmatic interface to the run_time linker.
*/
#include "_synonyms.h"
#include <string.h>
#include <dlfcn.h>
#include <synch.h>
#include <limits.h>
#include <debug.h>
#include "_rtld.h"
#include "_audit.h"
#include "_elf.h"
#include "msg.h"
/*
* Determine who called us - given a pc determine in which object it resides.
*
* For dlopen() the link map of the caller must be passed to load_so() so that
* the appropriate search rules (4.x or 5.0) are used to locate any
* dependencies. Also, if we've been called from a 4.x module it may be
* necessary to fix the specified pathname so that it conforms with the 5.0 elf
* rules.
*
* For dlsym() the link map of the caller is used to determine RTLD_NEXT
* requests, together with requests based off of a dlopen(0).
* For dladdr() this routines provides a generic means of scanning all loaded
* segments.
*/
Rt_map *
{
/*
* Traverse this objects mappings testing
* whether the pc falls within its range.
*/
return (lmp);
}
}
}
}
/*
* No mapping can be determined. If asked for a default, assume this
* is from the executable.
*/
if (flags & CL_EXECDEF)
return (0);
}
/*
* External entry for dlerror(3dl). Returns a pointer to the string describing
* the last occurring error. The last occurring error is cleared.
*/
char *
_dlerror()
{
char *error;
int entry;
lasterr = (char *)0;
if (entry)
return (error);
}
/*
* Add a dependency as a group descriptor to a group handle. Returns 0 on
* failure, ALE_EXISTS if the dependency already exists, or ALE_CREATE if it
* is newly created.
*/
int
{
int found = ALE_CREATE;
/*
* Make sure this dependency hasn't already been recorded.
*/
found = ALE_EXISTS;
break;
}
}
if (found == ALE_CREATE) {
/*
* Create a new handle descriptor.
*/
/*
* Indicate this object is a part of this handles group.
*/
sizeof (Grp_hdl *), AL_CNT_GROUPS) == 0)
return (0);
/*
* Append the new dependency to this handle.
*/
sizeof (Grp_desc), AL_CNT_DEPENDS)) == 0)
return (0);
}
if (DBG_ENABLED) {
if (found == ALE_CREATE)
}
return (found);
}
/*
* Allocate a handle and record its existence on the handle list for future
* verification.
*/
Grp_hdl *
{
return (0);
/* LINTED */
return (0);
}
return (ghp);
}
/*
* Create a handle.
*/
Grp_hdl *
{
/*
* For dlopen(0) the handle is maintained as part of the link-map list,
* otherwise it is associated with the referenced link-map.
*/
else
/*
* Objects can contain multiple handles depending on the handle flags
* supplied. Most RTLD flags pertain to the object itself and the
* bindings that it can achieve. Multiple handles for these flags
* don't make sense. But if the flag determines how the handle might
* be used, then multiple handles may exist. Presently this only makes
* sense for RTLD_FIRST. Determine if an appropriate handle already
* exists.
*/
break;
}
}
if (ghp == 0) {
/*
* If this is the first dlopen() request for this handle
* allocate and initialize a new handle.
*/
return (0);
AL_CNT_GROUPS) == 0)
return (0);
/*
* Indicate that this object has been referenced. In truth a
* reference hasn't yet occurred, it's a dlsym() that makes the
* reference. However, we assume that anyone performing a
* dlopen() will eventually call dlsym(), plus this makes for a
* better diagnostic location rather than having to call
* unused() after every dlsym() operation.
*/
if (nlmp)
/*
* A dlopen(0) handle is identified by the GPH_ZERO flag, the
* head of the link-map list is defined as the owner. There is
* no need to maintain a list of dependencies, for when this
* handle is used (for dlsym()) a dynamic search through the
* entire link-map list provides for searching all objects with
* GLOBAL visibility.
*/
} else {
return (0);
}
} else {
/*
* If a handle already exists, bump its reference count.
*
* If the previous reference count was 0, then this is a handle
* that an earlier call to dlclose() was unable to remove. Such
* handles are put on the orphan list. As this handle is back
* in use, it must be removed from the orphan list.
*
* Note, handles associated with a link-map list itself (i.e.
* dlopen(0)) can have a reference count of 0. However, these
* handles are never deleted, and therefore are never moved to
* the orphan list.
*/
/* LINTED */
if (DBG_ENABLED) {
}
}
}
/*
* Keep track of the parent (caller). As this object could be opened
* by different parents, this processing is carried out every time a
* handle is requested.
*/
return (0);
return (ghp);
}
/*
* Initialize a handle that has been created for an object that is already
* loaded. The handle is initialized with the present dependencies of that
* object. Once this initialization has occurred, any new objects that might
* be loaded as dependencies (lazy-loading) are added to the handle as each new
* object is loaded.
*/
int
{
/*
* If the handle has already been initialized, and the initial object's
* mode hasn't been promoted, there's no need to recompute the modes of
* any dependencies. If the object we've added has just been opened,
* the objects dependencies will not yet have been processed. These
* dependencies will be added on later calls to load_one(). Otherwise,
* this object already exists, so add all of its dependencies to the
* handle were operating on.
*/
return (1);
}
/*
* If this dependency doesn't indicate that its dependencies
* should be added to a handle, ignore it. This case identifies
* a parent of a dlopen(RTLD_PARENT) request.
*/
continue;
continue;
return (0);
}
}
return (1);
}
/*
* Sanity check a program-provided handle.
*/
static int
{
/* LINTED */
return (1);
}
return (0);
}
/*
* Core dlclose activity.
*/
int
{
int error;
/*
* If we're already at atexit() there's no point processing further,
* all objects have already been tsorted for fini processing.
*/
if ((rtld_flags & RT_FL_ATEXIT) != 0)
return (0);
/*
* Diagnose what we're up to.
*/
} else {
}
/*
* Decrement reference count of this object.
*/
return (0);
/*
* If this handle is special (dlopen(0)), then leave it around - it
* has little overhead.
*/
return (0);
/*
* This handle is no longer being referenced, remove it. If this handle
* is part of an alternative link-map list, determine if the whole list
* can be removed also.
*/
return (error);
}
/*
* Internal dlclose activity. Called from user level or directly for internal
* error cleanup.
*/
int
{
int error;
/*
* Although we're deleting object(s) it's quite possible that additional
* objects get loaded from running the .fini section(s) of the objects
* being deleted. These objects will have been added to the same
* link-map list as those objects being deleted. Remember this list
* for later investigation.
*/
/*
* Determine whether the original link-map list still exists. In the
* case of a dlclose of an alternative (dlmopen) link-map the whole
* list may have been removed.
*/
if (olml) {
break;
}
}
}
return (error);
}
/*
* Argument checking for dlclose. Only called via external entry.
*/
static int
{
if (hdl_validate(ghp) == 0) {
return (1);
}
}
/*
* External entry for dlclose(3dl). Returns 0 for success, non-zero otherwise.
*/
int
{
if (entry)
return (error);
}
/*
* The addition of new link-map lists is assumed to be in small quantities.
* Here, we assign a unique link-map id for diagnostic use. Simply update the
* running link-map count until we max out.
*/
int
{
MSG_LMID_ALT_SIZE + 12);
} else {
}
return (0);
return (1);
}
/*
* Core dlopen activity.
*/
static Grp_hdl *
{
/*
* If the path specified is null then we're operating on global
* objects. Associate a dummy handle with the link-map list.
*/
if (path == 0) {
int promote = 0;
/*
* Establish any flags for the handle (Grp_hdl).
*
* . This is a dummy handle (0) that provides for a dynamic
* search of all global objects within the process.
*
* . Use of the RTLD_FIRST flag indicates that only the first
* dependency on the handle (the new object) can be used
* to satisfy dlsym() requests.
*/
if (mode & RTLD_FIRST)
/*
* Establish the flags for this callers dependency descriptor
* (Grp_desc).
*
* . The explicit creation of a handle creates a descriptor
* for the new object and the parent (caller),
*
* . Use of the RTLD_PARENT flag indicates that the parent
* can be relocated against.
*/
if (mode & RTLD_PARENT)
return (0);
/*
* Traverse the main link-map control list, updating the mode
* of any objects as necessary. Call the relocation engine if
* this mode promotes the existing state of any relocations.
* crle()'s first pass loads all objects necessary for building
* a configuration file, however none of them are relocated.
* crle()'s second pass relocates objects in preparation for
* dldump()'ing using dlopen(0, RTLD_NOW).
*/
return (ghp);
continue;
promote = 1;
}
if (promote)
return (ghp);
}
/*
* Fix the pathname. If this object expands to multiple paths (ie.
* $ISALIST or $HWCAP have been used), then make sure the user has also
* furnished the RTLD_FIRST flag. As yet, we don't support opening
* more than one object at a time, so enforcing the RTLD_FIRST flag
* provides flexibility should we be able to support dlopening more
* than one object in the future.
*/
return (0);
((mode & RTLD_FIRST) == 0)) {
return (0);
}
/*
* Create a new link-map control list for this request, and load the
* associated object.
*/
AL_CNT_LMLISTS)) == 0) {
return (0);
}
/*
* Remove any expanded pathname infrastructure, and if the dependency
* couldn't be loaded, cleanup.
*/
if (nlmp == 0) {
return (0);
}
/*
* If loading an auditor was requested, and the auditor already existed,
* then the link-map returned will be to the original auditor. The new
* link-map list that was initially created, and the associated link-map
* control list are no longer needed. As the auditor is already loaded,
* we're probably done, but fall through in case additional relocations
* would be triggered by the mode of the caller.
*/
olmco = 0;
}
/*
* Finish processing the objects associated with this request.
*/
ghp = 0;
nlmp = 0;
}
/*
* If this lazyload has failed, and we've created a new link-map
* control list to which this request has added objects, then remove
* all the objects that have been associated to this request.
*/
/*
* Finally, remove any link-map control list that was created.
*/
if (olmco)
return (ghp);
}
/*
* Internal dlopen() activity. Called from user level or directly for internal
* opens that require a handle.
*/
Grp_hdl *
{
int objcnt;
/*
* Check for magic link-map list values:
*
* LM_ID_BASE: Operate on the PRIMARY (executables) link map
* LM_ID_LDSO: Operation on ld.so.1's link map
* LM_ID_NEWLM: Create a new link-map.
*/
return (0);
/*
* Establish the new link-map flags from the callers and those
* explicitly provided.
*/
if (flags & FLG_RT_AUDIT) {
/*
* Unset any auditing flags - an auditor shouldn't be
* audited. Insure all audit dependencies are loaded.
*/
}
return (0);
}
return (0);
}
}
/*
* Open the required object on the associated link-map list.
*/
(orig | PN_SER_DLOPEN))) != 0) {
/*
* Establish the new link-map from which .init processing will
* begin. Ignore .init firing when constructing a configuration
* file (crle(1)).
*/
if ((mode & RTLD_CONFGEN) == 0)
}
/*
* If loading an auditor was requested, and the auditor already existed,
* then the link-map returned will be to the original auditor. Remove
* the link-map control list that was created for this request.
*/
}
/*
* Return the number of objects loaded if required. This is used to
* trigger used() processing on return from a dlopen().
*/
if (loaded)
/*
* If this load failed, remove any alternative link-map list.
*/
if ((ghp == 0) &&
lml = 0;
}
/*
* Finish this load request. If objects were loaded, .init processing
* is computed. Finally, the debuggers are informed of the link-map
* lists being stable.
*/
return (ghp);
}
/*
* Argument checking for dlopen. Only called via external entry.
*/
static Grp_hdl *
int *loaded)
{
/*
* Verify that a valid pathname has been supplied.
*/
return (0);
}
/*
* Historically we've always verified the mode is either RTLD_NOW or
* RTLD_LAZY. RTLD_NOLOAD is valid by itself. Use of LM_ID_NEWLM
* requires a specific pathname, and use of RTLD_PARENT is meaningless.
*/
return (0);
}
return (0);
}
return (0);
}
return (0);
}
((mode & RTLD_NOLOAD) == 0))
}
}
/*
* External entry for dlopen(3dl). On success, returns a pointer (handle) to
* the structure containing information about the newly added object, ie. can
* be used by dlsym(). On failure, returns a null pointer.
*/
void *
{
if (entry)
return ((void *)ghp);
}
/*
* External entry for dlmopen(3dl).
*/
void *
{
if (entry)
return ((void *)ghp);
}
/*
* Handle processing for dlsym.
*/
Sym *
{
/*
* Continue processing a dlsym request. Lookup the required symbol in
* each link-map specified by the handle.
*
* To leverage off of lazy loading, dlsym() requests can result in two
* passes. The first descends the link-maps of any objects already in
* the address space. If the symbol isn't located, and lazy
* dependencies still exist, then a second pass is made to load these
* dependencies if applicable. This model means that in the case where
* a symbols exists in more than one object, the one located may not be
* constant - this is the standard issue with lazy loading. In addition,
* attempting to locate a symbol that doesn't exist will result in the
* loading of all lazy dependencies on the given handle, which can
* defeat some of the advantages of lazy loading (look out JVM).
*/
/*
* If this symbol lookup is triggered from a dlopen(0) handle,
* traverse the present link-map list looking for promiscuous
* entries.
*/
/*
* If this handle indicates we're only to look in the
* first object check whether we're done.
*/
return ((Sym *)0);
continue;
continue;
return (sym);
}
/*
* If we're unable to locate the symbol and this link-map still
* has pending lazy dependencies, start loading them in an
* attempt to exhaust the search. Note that as we're already
* traversing a dynamic linked list of link-maps there's no
* need for elf_lazy_find_sym() to descend the link-maps itself.
*/
int lazy = 0;
continue;
continue;
lazy = 1;
return (sym);
}
/*
* If no global, lazy loadable dependencies are found,
* then none exist for this link-map list. Pending lazy
* loadable objects may still exist for non-local
* objects that are associated with this link-map list,
* which is why we entered this fallback. Tag this
* link-map list to prevent further searching for lazy
* dependencies.
*/
if (lazy == 0)
}
} else {
/*
* Traverse the dlopen() handle for the presently loaded
* link-maps.
*/
continue;
return (sym);
return ((Sym *)0);
}
/*
* If we're unable to locate the symbol and this link-map still
* has pending lazy dependencies, start loading them in an
* attempt to exhaust the search.
*/
int lazy = 0;
continue;
lazy = 1;
return (sym);
}
/*
* If no lazy loadable dependencies are found, then
* none exist for this handle. Pending lazy loadable
* objects may still exist for the associated link-map
* list, which is why we entered this fallback. Tag
* this handle to prevent further searching for lazy
* dependencies.
*/
if (lazy == 0)
}
}
return ((Sym *)0);
}
/*
* Core dlsym activity. Selects symbol lookup method from handle.
*/
void *
{
sl.sl_rsymndx = 0;
/*
* Standard relocations are evaluated using the symbol index of the
* associated relocation symbol. This index provides for loading
* any lazy dependency and establishing a direct binding if necessary.
* If a dlsym() operation originates from an object that contains a
* symbol table entry for the same name, then establish the symbol
* index so that any dependency requirements can be triggered.
*/
}
}
/*
* If this handle is RTLD_NEXT determine whether a lazy load
* from the caller might provide the next object. This mimics
* the lazy loading initialization normally carried out by
* lookup_sym(), however here, we must do this up-front, as
* lookup_sym() will be used to inspect the next object.
*/
if (sl.sl_rsymndx) {
/* LINTED */
(void) elf_lazy_load(clmp,
/*
* Clear the symbol index, so as not to confuse
* lookup_sym() of the next object.
*/
sl.sl_rsymndx = 0;
}
/*
* If the handle is RTLD_NEXT start searching in the next link
* map from the callers. Determine permissions from the
* present link map. Indicate to lookup_sym() that we're on an
* RTLD_NEXT request so that it will use the callers link map to
* start any possible lazy dependency loading.
*/
if (nlmp == 0)
return (0);
/*
* If the handle is RTLD_SELF start searching from the caller.
*/
} else if (handle == RTLD_DEFAULT) {
/*
* If the handle is RTLD_DEFAULT mimic the standard symbol
* lookup as would be triggered by a relocation.
*/
} else if (handle == RTLD_PROBE) {
/*
* If the handle is RTLD_PROBE, mimic the standard symbol
* lookup as would be triggered by a relocation, however do
* not fall back to a lazy loading rescan if the symbol can't be
* found within the currently loaded objects. Note, a lazy
* loaded dependency required by the caller might still get
* loaded to satisfy this request, but no exhaustive lazy load
* rescan is carried out.
*/
} else {
/*
* Look in the shared object specified by the handle and in all
* of its dependencies.
*/
}
if (sym) {
/* LINTED */
&sb_flags);
}
return ((void *)addr);
} else
return (0);
}
/*
* Internal dlsym activity. Called from user level or directly for internal
* symbol lookup.
*/
void *
{
void *error;
/*
* While looking for symbols it's quite possible that additional objects
* get loaded from lazy loading. These objects will have been added to
* the same link-map list as those objects on the handle. Remember this
* list for later investigation.
*/
else {
else {
break;
}
}
}
/*
* Cache the error message, as Java tends to fall through this
* code many times.
*/
if (nosym_str == 0)
}
return (error);
}
/*
* Argument checking for dlsym. Only called via external entry.
*/
static void *
{
/*
* Verify the arguments.
*/
if (name == 0) {
return (0);
}
return (0);
}
}
/*
* External entry for dlsym(). On success, returns the address of the specified
* symbol. On error returns a null.
*/
void *
{
int entry;
void *addr;
if (dlmp)
if (entry)
return (addr);
}
/*
* Core dladdr activity.
*/
static void
{
/*
* Set up generic information and any defaults.
*/
/*
* Determine the nearest symbol to this address.
*/
}
/*
* External entry for dladdr(3dl) and dladdr1(3dl). Returns an information
* structure that reflects the symbol closest to the address specified.
*/
int
{
/*
* Use our calling technique to determine what object is associated
* with the supplied address. If a caller can't be determined,
* indicate the failure.
*/
error = 0;
} else {
error = 1;
}
if (entry)
leave(0);
return (error);
}
int
{
/*
* Validate any flags.
*/
if (flags) {
int request;
(request != RTLD_DL_LINKMAP)) {
flags);
return (0);
}
if (info == 0) {
return (0);
}
}
/*
* Use our calling technique to determine what object is associated
* with the supplied address. If a caller can't be determined,
* indicate the failure.
*/
error = 0;
} else {
error = 1;
}
if (entry)
leave(0);
return (error);
}
/*
* Core dldump activity.
*/
static int
{
/*
* Verify any arguments first.
*/
return (1);
}
/*
* If an input file is specified make sure its one of our dependencies
* on the main link-map list. Note, this has really all evolved for
* crle(), which uses libcrle.so on an alternative link-map to trigger
* dumping objects from the main link-map list. If we ever want to
* dump objects from alternative link-maps, this model is going to
* have to be revisited.
*/
if (ipath) {
ipath);
return (1);
}
return (1);
}
ipath);
return (1);
}
} else
/*
* If the object being dump'ed isn't fixed identify its mapping.
*/
/*
* As rt_dldump() will effectively lazy load the necessary support
* libraries, make sure ld.so.1 is initialized for plt relocations.
*/
if (elf_rtld_load() == 0)
return (0);
/*
* Dump the required image.
*/
}
/*
* External entry for dldump(3c). Returns 0 on success, non-zero otherwise.
*/
int
{
if (entry)
return (error);
}
/*
* get_linkmap_id() translates Lm_list * pointers to the Link_map id as used by
* the rtld_db and dlmopen() interfaces. It checks to see if the Link_map is
* one of the primary ones and if so returns it's special token:
* LM_ID_BASE
* LM_ID_LDSO
*
* If it's not one of the primary link_map id's it will instead returns a
* pointer to the Lm_list structure which uniquely identifies the Link_map.
*/
{
return (LM_ID_BASE);
return (LM_ID_LDSO);
}
/*
* Extract information for a dlopen() handle.
*/
static int
{
if ((request > RTLD_DI_MAX) || (p == 0)) {
return (-1);
}
/*
* Return configuration cache name and address.
*/
if (request == RTLD_DI_CONFIGADDR) {
return (-1);
}
return (0);
}
/*
* Return profiled object name (used by ldprof audit library).
*/
if (request == RTLD_DI_PROFILENAME) {
if (profile_name == 0) {
return (-1);
}
*(const char **)p = profile_name;
return (0);
}
if (request == RTLD_DI_PROFILEOUT) {
/*
* If a profile destination directory hasn't been specified
* provide a default.
*/
if (profile_out == 0)
*(const char **)p = profile_out;
return (0);
}
/*
* Obtain or establish a termination signal.
*/
if (request == RTLD_DI_GETSIGNAL) {
*(int *)p = killsig;
return (0);
}
if (request == RTLD_DI_SETSIGNAL) {
int sig = *(int *)p;
/*
* Determine whether the signal is in range.
*/
(void) sigfillset(&set);
return (-1);
}
return (0);
}
/*
* For any other request a link-map is required. Verify the handle.
*/
else {
if (!hdl_validate(ghp)) {
return (-1);
}
}
/*
* Obtain the process arguments, environment and auxv. Note, as the
* environment can be modified by the user (putenv(3c)), reinitialize
* the environment pointer on each request.
*/
if (request == RTLD_DI_ARGSINFO) {
return (0);
}
/*
* Return Lmid_t of the Link-Map list that the specified object is
* loaded on.
*/
if (request == RTLD_DI_LMID) {
return (0);
}
/*
* Return a pointer to the Link-Map structure associated with the
* specified object.
*/
if (request == RTLD_DI_LINKMAP) {
return (0);
}
/*
* Return search path information, or the size of the buffer required
* to store the information.
*/
char *strs;
info = (Dl_serinfo *)p;
/*
* Traverse search path entries for this object.
*/
continue;
/*
* If configuration information exists, it's possible
* this path has been identified as non-existent, if so
* ignore it.
*/
continue;
}
/*
* Keep track of search path count and total info size.
*/
if (cnt++)
size += sizeof (Dl_serpath);
if (request == RTLD_DI_SERINFOSIZE)
continue;
/*
* If we're filling in search path information, confirm
* there's sufficient space.
*/
return (-1);
}
return (-1);
}
/*
* Append the path to the information buffer.
*/
path++;
}
/*
* If we're here to size the search buffer fill it in.
*/
if (request == RTLD_DI_SERINFOSIZE) {
}
}
/*
* Return the origin of the object associated with this link-map.
* Basically return the dirname(1) of the objects fullpath.
*/
if (request == RTLD_DI_ORIGIN) {
char *str = (char *)p;
*str = '\0';
return (0);
}
return (0);
}
/*
* External entry for dlinfo(3dl).
*/
int
{
if (entry)
return (error);
}