ldd.c revision df4628cb18cef0a7960608d573d5a9b6cc9e29d5
/*
* 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"
/*
* Print the list of shared objects required by a dynamic executable or shared
* object.
*
* usage is: ldd [-d | -r] [-c] [-e envar] [-i] [-f] [-L] [-l] [-s]
* [-U | -u] [-v] [-w] file(s)
*
* ldd opens the file and verifies the information in the elf header.
* If the file is a dynamic executable, we set up some environment variables
* and exec(2) the file. If the file is a shared object, we preload the
* file with a dynamic executable stub. The runtime linker (ld.so.1) actually
* provides the diagnostic output, according to the environment variables set.
*
* If neither -d nor -r is specified, we set only LD_TRACE_LOADED_OBJECTS_[AE].
* The runtime linker will print the pathnames of all dynamic objects it
* loads, and then exit. Note that we distiguish between ELF and AOUT objects
* when setting this environment variable - AOUT executables cause the mapping
* of sbcp, the dependencies of which the user isn't interested in.
*
* If -d or -r is specified, we also set LD_WARN=1; the runtime linker will
* perform its normal relocations and issue warning messages for unresolved
* references. It will then exit.
* If -r is specified, we set LD_BIND_NOW=1, so that the runtime linker
* will perform all relocations, otherwise (under -d) the runtime linker
* will not perform PLT (function) type relocations.
*
* If -c is specified we also set LD_NOCONFIG=1, thus disabling any
* configuration file use.
*
* If -e is specified the associated environment variable is set for the
* child process that will produce ldd's diagnostics.
*
* If -i is specified, we set LD_INIT=1. The order of inititialization
* sections to be executed is printed. We also set LD_WARN=1.
*
* If -f is specified, we will run ldd as root on executables that have
* directory. By default we will not let this happen.
*
* If -l is specified it generates a warning for any auxiliary filter not found.
* Prior to 2.8 this forced any filters to load (all) their filtees. This is
* now the default, however missing auxiliary filters don't generate any error
* diagniostic. See also -L.
*
* If -L is specified we revert to lazy loading, thus any filtee or lazy
* dependency loading is deferred until relocations cause loading. Without
* this option we set LD_LOADFLTR=1, thus forcing any filters to load (all)
* their filtees, and LD_NOLAZYLOAD=1 thus forcing immediate processing of
* any lazy loaded dependencies.
*
* If -s is specified we also set LD_TRACE_SEARCH_PATH=1, thus enabling
* the runtime linker to indicate the search algorithm used.
*
* If -v is specified we also set LD_VERBOSE=1, thus enabling the runtime
* linker to indicate all object dependencies (not just the first object
* loaded) together with any versionig requirements.
*
* If -U or -u is specified unused dependencies are detected. -u causes
* LD_UNUSED=1 to be set, which causes dependencies that are unused within the
* process to be detected. -U causes LD_UNREF=1 to be set, which causes
* unreferenced objects, and unreferenced cyclic dependencies to be detected.
* These options assert that at least -d is set as relocation references are
* what determine an objects use.
*
* If -w is specified, no unresolved weak references are allowed. -w causes
* LD_NOUNRESWEAK=1 to be set. By default, an unresolved weak reference is
* allowed, and a "0" is written to the relocation offset. The -w option
* disables this default. Any weak references that can not be resolved result
* in relocation error messages.
*/
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <_libelf.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#include <locale.h>
#include <errno.h>
#include <signal.h>
#include "machdep.h"
#include "sgs.h"
#include "conv.h"
#include "a.out.h"
#include "msg.h"
static int aout_check(int, char *, char *, int, int);
static int run(int, char *, char *, const char *, int);
/*
* Define all environment variable strings. The character following the "="
* will be written to, to disable or enable the associated feature.
*/
static char bind[] = "LD_BIND_NOW= ",
load_elf[] = "LD_TRACE_LOADED_OBJECTS_E= ",
load_aout[] = "LD_TRACE_LOADED_OBJECTS_A= ",
path[] = "LD_TRACE_SEARCH_PATHS= ",
verb[] = "LD_VERBOSE= ",
warn[] = "LD_WARN= ",
conf[] = "LD_NOCONFIG= ",
fltr[] = "LD_LOADFLTR= ",
lazy[] = "LD_NOLAZYLOAD=1",
init[] = "LD_INIT= ",
uref[] = "LD_UNREF= ",
used[] = "LD_UNUSED= ",
weak[] = "LD_NOUNRESWEAK= ";
static char *load;
/*
* Append an item to the specified list, and return a pointer to the list
* node created.
*/
Listnode *
{
return (0);
else {
}
return (lnp);
}
int
{
/*
* Establish locale.
*/
/*
* verify command line syntax and process arguments
*/
opterr = 0; /* disable getopt error mesg */
switch (var) {
case 'c' : /* enable config search */
cflag = 1;
break;
case 'd' : /* perform data relocations */
dflag = 1;
if (rflag)
error++;
break;
case 'e' :
cname);
exit(1);
}
break;
case 'f' :
fflag = 1;
break;
case 'L' :
Lflag = 1;
break;
case 'l' :
lflag = 1;
break;
case 'i' : /* print the order of .init */
iflag = 1;
break;
case 'r' : /* perform all relocations */
rflag = 1;
if (dflag)
error++;
break;
case 's' : /* enable search path output */
sflag = 1;
break;
case 'U' : /* list unreferenced */
if (uflag)
error++;
break;
case 'u' : /* list unused dependencies */
uflag = 1;
if (Uflag)
error++;
break;
case 'v' : /* enable verbose output */
vflag = 1;
break;
case 'w' : /* disable unresolved weak */
break;
default :
error++;
break;
}
if (error)
break;
}
if (error) {
exit(1);
}
/*
* Determine if any of the LD_PRELOAD family is already set in the
* environment, if so we'll continue to analyze each object with the
* appropriate setting.
*/
(*prefile_32 == '\0')) {
}
(*prefile_64 == '\0')) {
}
(*prefile == '\0')) {
}
/*
* Determine if any environment requests are for the LD_PRELOAD family,
* and if so override any environment settings we've established above.
*/
MSG_LD_PRELOAD_32_SIZE)) == 0) {
prefile_32 = str;
continue;
}
MSG_LD_PRELOAD_64_SIZE)) == 0) {
prefile_64 = str;
continue;
}
MSG_LD_PRELOAD_SIZE)) == 0) {
continue;
}
}
/*
* Set the appropriate relocation environment variables (Note unsetting
* the environment variables is done just in case the user already
* has these in their environment ... sort of thing the test folks
* would do :-)
*/
'\0';
/*
* coordinate libelf's version information
*/
exit(1);
}
/*
* Loop through remaining arguments. Note that from here on there
* are no exit conditions so that we can process a list of files,
* any error condition is retained for a final exit status.
*/
/*
* Open file (do this before checking access so that we can
* provide the user with better diagnostics).
*/
error = 1;
continue;
}
/*
* Get the files elf descriptor and process it as an elf or
* a.out (4.x) file.
*/
case ELF_K_AR :
error = 1;
break;
case ELF_K_COFF:
error = 1;
break;
case ELF_K_ELF:
error = 1;
break;
default:
/*
* This is either an unknown file or an aout format
*/
error = 1;
break;
}
}
return (error);
}
static int
{
return (ELFCLASS32);
#if defined(__sparc)
(conv_sys_eclass() == ELFCLASS64))
return (ELFCLASS64);
(conv_sys_eclass() == ELFCLASS64))
return (ELFCLASS64);
#endif
return (ELFCLASSNONE);
}
static int
{
/*
* verify information in file header
*/
return (1);
}
/*
* check class and encoding
*/
return (1);
}
/*
* check type
*/
return (1);
}
return (1);
}
return (1);
}
}
/*
* Check that the file is executable. Dynamic executables must be
* executable to be exec'ed. Shared objects need not be executable to
* be mapped with a dynamic executable, however, by convention they're
* supposed to be executable.
*/
return (1);
}
fname);
}
/*
* Determine whether we have a dynamic section or interpretor.
*/
break;
return (1);
}
dynamic = 1;
continue;
}
continue;
interp = 1;
/*
* If fflag is not set, and euid == root, and the interpreter
* allow ldd to execute the image. This prevents someone
* creating a `trojan horse' by substituting their own
* interpreter that could preform privileged operations
* when ldd is against it.
*/
char *interpreter;
/*
* Does the interpreter live under a trusted directory.
*/
MSG_PTH_USRLIB_SIZE) != 0) &&
MSG_PTH_LIB_SIZE) != 0) &&
MSG_PTH_ETCLIB_SIZE) != 0)) {
return (1);
}
}
}
/*
* Catch the case of a static executable (ie, an ET_EXEC that has a set
* of program headers but no PT_DYNAMIC).
*/
fname);
return (1);
}
/*
* If there is a dynamic section, then check for the DF_1_NOHDR
* flag, and bail if it is present. Those objects are created using
* the ?N mapfile option: The ELF header and program headers are
* not mapped as part of the first segment, and virtual addresses
* are computed without them. If ldd tries to interpret such
* a file, it will become confused and generate bad output or
* crash. Such objects are always special purpose files (like an OS
* kernel) --- files for which the ldd operation doesn't make sense.
*/
fname);
return (1);
}
/*
* Run the required program (shared and relocatable objects require the
* use of lddstub).
*/
else
}
static int
{
int err;
return (1);
}
sizeof (struct exec)) {
return (1);
}
return (1);
}
fname);
return (1);
}
return (1);
}
/*
* Run the required program.
*/
ELFCLASS32));
} else {
ELFCLASS32));
}
}
/*
* Run the required program, setting the preload and trace environment
* variables accordingly.
*/
static int
{
const char *preload = 0;
return (1);
}
if (pid) { /* parent */
;
fname);
status = 1;
fname);
status = 1;
}
} else { /* child */
char *str;
/*
* When using ldd(1) to analyze a shared object we preload the
* shared object with lddstub. Any additional preload
* requirements are added after the object being analyzed, this
* allows us to skip the first object but produce diagnostics
* for each other preloaded object.
*/
char *str;
if (*str == '/') {
break;
}
/*
* Determine which preload files and preload environment
* variable to use.
*/
if (class == ELFCLASS64) {
files = prefile_64;
}
} else {
files = prefile_32;
}
}
cname);
exit(1);
}
cname);
exit(1);
}
/*
* The pointer "load" has be assigned to load_elf[] or
* load_aout[]. Use the size of load_elf[] as the size
* of load_aout[] is the same.
*/
} else
/*
* Establish new environment variables to affect the child
* process.
*/
exit(1);
}
/*
* Establish explicit environment requires (but don't override
* any preload request established to process a shared object).
*/
size = 0;
if (preload) {
if (size == 0)
continue;
}
}
cname);
exit(1);
}
}
/*
* Execute the object and let ld.so.1 do the rest.
*/
if (nfile > 1)
fname);
_exit(0);
/* NOTREACHED */
}
}
return (status);
}
const char *
{
}