external.c revision 09ce0d4acf1a79c720d7e54b60e87cbfa0f1b2d6
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Implementation of all external interfaces between ld.so.1 and libc.
*
* This file started as a set of routines that provided synchronization and
* locking operations using calls to libthread. libthread has merged with libc
* under the Unified Process Model (UPM), and things have gotten a lot simpler.
* This file continues to establish and redirect various events within ld.so.1
* to interfaces within libc.
*
* Until libc is loaded and relocated, any external interfaces are captured
* locally. Each link-map list maintains its own set of external vectors, as
* each link-map list typically provides its own libc. Although this per-link-
* map list vectoring provides a degree of flexibility, there is a protocol
* expected when calling various libc interfaces.
*
* i. Any new alternative link-map list should call CI_THRINIT, and then call
* CI_TLS_MODADD to register any TLS for each object of that link-map list
* (this item is labeled i. as auditors can be the first objects loaded,
* and they exist on their own lik-map list).
*
* ii. For the primary link-map list, CI_TLS_STATMOD must be called first to
* register any static TLS. This routine is called regardless of there
* being any TLS, as this routine also establishes the link-map list as the
* primary list and fixes the association of uberdata). CI_THRINIT should
* then be called.
*
* iii. Any objects added to an existing link-map list (primary or alternative)
* should call CI_TLS_MODADD to register any additional TLS.
*
* These events are established by:
*
* i. Typically, libc is loaded as part of the primary dependencies of any
* link-map list (since the Unified Process Model (UPM), libc can't be
* lazily loaded). To minimize the possibility of loading and registering
* objects, and then tearing them down (because of a relocation error),
* external vectors are established as part of load_completion(). This
* routine is called on completion of any operation that can cause objects
* to be loaded. This point of control insures the objects have been fully
* analyzed and relocated, and moved to their controlling link-map list.
* The external vectors are established prior to any .inits being fired.
*
* ii. Calls to CI_THRINIT, and CI_TLS_MODADD also occur as part of
* load_completion(). CI_THRINIT is only called once for each link-map
* control list.
*
* iii. Calls to CI_TLS_STATMOD, and CI_THRINIT occur for the primary link-map
* list in the final stages of setup().
*
* The interfaces provide by libc can be divided into two families. The first
* family consists of those interfaces that should be called from the link-map
* list. It's possible that these interfaces convey state concerning the
* link-map list they are part of:
*
* CI_ATEXIT
* CI TLS_MODADD
* CI_TLS_MODREM
* CI_TLS_STATMOD
* CI_THRINIT
*
* The second family are global in nature, that is, the link-map list from
* which they are called provides no state information. In fact, for
* CI_BIND_GUARD, the calling link-map isn't even known. The link-map can only
* be deduced after ld.so.1's global lock has been obtained. Therefore, the
* following interfaces are also maintained as global:
*
* CI_LCMESSAGES
* CI_BIND_GUARD
* CI_BIND_CLEAR
* CI_THR_SELF
*
* Note, it is possible that these global interfaces are obtained from an
* alternative link-map list that gets torn down because of a processing
* failure (unlikely, because the link-map list components must be analyzed
* and relocated prior to load_completion(), but perhaps the tear down is still
* a possibility). Thus the global interfaces may have to be replaced. Once
* the interfaces have been obtained from the primary link-map, they can
* remain fixed, as the primary link-map isn't going to go anywhere.
*
* The last wrinkle in the puzzle is what happens if an alternative link-map
* is loaded with no libc dependency? In this case, the alternative objects
* can not call CI_THRINIT, can not be allowed to use TLS, and will not receive
* any atexit processing.
*
* The history of these external interfaces is defined by their version:
*
* TI_VERSION == 1
* all rt_mutex_lock/rt_mutex_unlock calls were vectored.
* lwp/libthread that provided signal blocking via bind_guard/bind_clear).
*
* TI_VERSION == 2
* Under this model only libthreads bind_guard/bind_clear and thr_self
* interfaces were used. Both libthreads blocked signals under the
* bind_guard/bind_clear interfaces. Lower level locking is derived
* from internally bound _lwp_ interfaces. This removes recursive
* problems encountered when obtaining locking interfaces from libthread.
* condition variables for controlling thread concurrency (allows access
* to objects only after their .init has completed).
*
* NOTE, the TI_VERSION indicated the ti_interface version number, where the
* ti_interface was a large vector of functions passed to both libc (to override
* these interfaces.
*
* CI_VERSION == 1
* Introduced with CI_VERSION & CI_ATEXIT
*
* CI_VERSION == 2 (Solaris 8 update 2).
* Added support for CI_LCMESSAGES
*
* CI_VERSION == 3 (Solaris 9).
* Added the following versions to the CI table:
*
* CI_BIND_GUARD, CI_BIND_CLEAR, CI_THR_SELF
* CI_TLS_MODADD, CI_TLS_MOD_REMOVE, CI_TLS_STATMOD
*
* This version introduced the DT_SUNW_RTLDINFO structure as a mechanism
* to handshake with ld.so.1.
*
* CI_VERSION == 4 (Solaris 10).
* process model. libc now initializes the current thread pointer from
* this interface (and no longer relies on the INITFIRST flag - which
* others have started to camp out on).
*
* Release summary:
*
* Solaris 8 CI_ATEXIT via _ld_libc()
* TI_* via _ld_concurrency()
*
* Solaris 9 CI_ATEXIT and CI_LCMESSAGES via _ld_libc()
* CI_* via RTLDINFO and _ld_libc() - new libthread
* TI_* via _ld_concurrency() - old libthread
*
* Solaris 10 CI_ATEXIT and CI_LCMESSAGES via _ld_libc()
* CI_* via RTLDINFO and _ld_libc() - new libthread
*/
#include <synch.h>
#include <signal.h>
#include <thread.h>
#include <synch.h>
#include <strings.h>
#include <stdio.h>
#include <debug.h>
#include <libc_int.h>
#include "_elf.h"
#include "_rtld.h"
/*
* This interface provides the unified process model communication between
* ld.so.1 and libc. This interface can be called a number of times:
*
* - Initially, this interface is called to process RTLDINFO. This data
* structure is typically provided by libc, and contains the address of
* libc interfaces that must be called to initialize threads information.
*
* - _ld_libc(), this interface can also be called by libc at process
* initialization, after libc has been loaded and relocated, but before
* control has been passed to any user code (.init's or main()). This
* call provides additional libc interface information that ld.so.1 must
* call during process execution.
*
* - _ld_libc() can also be called by libc during process execution to
* re-establish interfaces such as the locale.
*/
static void
{
return;
/*
* Once the process is active, ensure we grab a lock.
*/
if (rtld_flags & RT_FL_APPLIC)
char *gptr;
continue;
/*
* Maintain all interfaces on a per-link-map basis. Note, for
* most interfaces, only the first interface is used for any
* link-map list. This prevents accidents with developers who
* manage to load two different versions of libc.
*/
continue;
}
/*
* Process any interfaces that must be maintained on a global
* basis.
*/
switch (tag) {
case CI_ATEXIT:
break;
case CI_LCMESSAGES:
/*
* At startup, ld.so.1 can establish a locale from one
* of the locale family of environment variables (see
* ld_str_env() and readenv_user()). During process
* execution the locale can also be changed by the user.
* This interface is called from libc should the locale
* be modified. Presently, only one global locale is
* maintained for all link-map lists, and only objects
* on the primrary link-map may change this locale.
*/
/*
* If we've obtained a message locale (typically
* supplied via libc's setlocale()), then
* register the locale for use in dgettext() so
* as to reestablish the locale for ld.so.1's
* messages.
*/
if (gptr) {
}
/*
* Clear any cached messages.
*/
}
break;
case CI_BIND_GUARD:
case CI_BIND_CLEAR:
case CI_THR_SELF:
/*
* If the global vector is unset, or this is the primary
* link-map, set the global vector.
*/
/* FALLTHROUGH */
case CI_TLS_MODADD:
case CI_TLS_MODREM:
case CI_TLS_STATMOD:
case CI_THRINIT:
threaded++;
break;
case CI_VERSION:
if ((rtld_flags2 & RT_FL2_RTLDSEEN) == 0) {
int version;
#if defined(CI_V_FIVE)
}
#endif
break;
/*
* We might have seen an auditor which is not
* dependent on libc. Such an auditor's link
* map list has LML_FLG_HOLDLOCK set. This
* lock needs to be dropped. Refer to
* audit_setup() in audit.c.
*/
if ((rtld_flags2 & RT_FL2_HASAUDIT) == 0)
break;
/*
* Yes, we did. Take care of them.
*/
}
}
}
break;
default:
break;
}
}
if (threaded) {
/*
* If a version of libc gives us only a subset of the TLS
* interfaces, it's confused and we discard the whole lot.
*/
}
/*
* Indicate that we're now thread capable.
*/
}
if (entry)
}
/*
* At this point we know we have a set of objects that have been fully analyzed
* and relocated. Prior to the next major step of running .init sections (ie.
* running user code), retrieve any RTLDINFO interfaces.
*/
int
{
}
/*
* Perform some sanity checks. If we have TLS requirements we better
* have the associated external interfaces.
*/
return (0);
}
return (1);
}
/*
* Provide an interface for libc to communicate additional interface
* information.
*/
void
{
}
static int bindmask = 0;
int
rt_bind_guard(int flags)
{
int (*fptr)(int);
int bindflag;
} else {
return (1);
}
return (0);
}
}
int
rt_bind_clear(int flags)
{
int (*fptr)(int);
int bindflag;
} else {
if (bindflag == 0)
return (bindmask);
else {
return (0);
}
}
}
/*
* Make sure threads have been initialized. This interface is called once for
* each link-map list.
*/
void
{
void (*fptr)(void);
if ((fptr =
(*fptr)();
(void) enter(thr_flg_reenter);
}
}
{
return ((*fptr)());
return (1);
}
int
{
}
int
{
}
/*
* Mutex interfaces to resolve references from any objects extracted from
* libc_pic.a. Note, as ld.so.1 is essentially single threaded these can be
* noops.
*/
/* ARGSUSED */
int
{
return (0);
}
/* ARGSUSED */
int
{
return (0);
}
/* ARGSUSED */
int
{
return (0);
}
/* ARGSUSED */
int
{
return (0);
}
/*
* This is needed to satisfy sysconf() (case _SC_THREAD_STACK_MIN)
*/
{
#ifdef _LP64
return (8 * 1024);
#else
return (4 * 1024);
#endif
}
/*
* munmap() calls this. It is unnecessary in the dynamic linker.
*/
/* ARGSUSED */
void
{
}
/*
* The following functions are cancellation points in libc.
* They are called from other functions in libc that we extract
* and use directly. We don't do cancellation while we are in
* the dynamic linker, so we redefine these to call the primitive,
* non-cancellation interfaces.
*/
int
{
extern int __close(int);
}
int
{
extern int __fcntl(int, int, ...);
}
int
{
extern int __open(const char *, int, ...);
}
int
{
extern int __openat(int, const char *, int, ...);
}
{
}
{
}