/*
* 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
*/
/*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <fcntl.h>
#include <libintl.h>
#include <locale.h>
#include <fcntl.h>
#include <ar.h>
#include <gelf.h>
#include "conv.h"
#include "libld.h"
#include "machdep.h"
#include "msg.h"
/*
* The following prevent us from having to include ctype.h which defines these
* functions as macros which reference the __ctype[] array. Go through .plt's
* to get to these functions in libc rather than have every invocation of ld
* have to suffer the R_SPARC_COPY overhead of the __ctype[] array.
*/
extern int isspace(int);
/*
* We examine ELF objects, and archives containing ELF objects, in order
* used. We want to avoid the overhead of libelf for this, at least until
* we are certain that we need it, so we start by reading bytes from
* the beginning of the file. This type defines the buffer used to read
* these initial bytes.
*
* A plain ELF object will start with an ELF header, whereas an archive
* starts with a magic string (ARMAG) that is SARMAG bytes long. Any valid
* ELF file or archive will contain more bytes than this buffer, so any
* file shorter than this can be safely assummed not to be of interest.
*
* The ELF header for ELFCLASS32 and ELFCLASS64 are identical up through the
* the e_version field, and all the information we require is found in this
* common prefix. Furthermore, this cannot change, as the layout of an ELF
* header is fixed by the ELF ABI. Hence, the ehdr part of this union is
* not a full ELF header, but only the class-independent prefix that we need.
*
* As this is a raw (non-libelf) read, we are responsible for handling any
* byte order difference between the object and the system running this
* program when we read any datum larger than a byte (i.e. e_machine) from
* this header.
*/
typedef union {
} ehdr;
} FILE_HDR;
/*
* Print a message to stdout
*/
void
{
#if defined(lint)
/*
* The lml argument is only meaningful for diagnostics sent to ld.so.1.
* Supress the lint error by making a dummy assignment.
*/
lml = 0;
#endif
/*
* For error types we issue a prefix for, make sure the necessary
* string has been internationalized and is ready.
*/
switch (error) {
case ERR_WARNING_NF:
break;
case ERR_WARNING:
break;
case ERR_GUIDANCE:
break;
case ERR_FATAL:
break;
case ERR_ELF:
}
/* If strings[] element for our error type is non-NULL, issue prefix */
}
int elferr;
elf_errmsg(elferr));
}
}
/*
* Print a message to stdout
*/
/* VARARGS3 */
void
{
}
/*
* Examine the first object in an archive to determine its ELFCLASS
* and machine type.
*
* entry:
* fd - Open file descriptor for file
* elf - libelf ELF descriptor
* class_ret, mach_ret - Address of variables to receive ELFCLASS
* and machine type.
*
* exit:
* On success, *class_ret and *mach_ret are filled in, and True (1)
* is returned. On failure, False (0) is returned.
*/
static int
{
int found = 0;
/*
* Process each item within the archive until we find the first
* ELF object, or alternatively another archive to recurse into.
* Stop after analyzing the first plain object found.
*/
return (0);
case ELF_K_AR:
break;
case ELF_K_ELF:
NULL)
break;
} else {
NULL)
break;
}
found = 1;
break;
}
}
}
return (found);
}
/*
* Determine:
* - ELFCLASS of resulting object (class)
* - Whether user specified class of the linker (ldclass)
* - ELF machine type of resulting object (m_mach)
*
* In order of priority, we determine this information as follows:
*
* - Command line options (-32, -64, -z altexec64, -z target).
* - From the first plain object seen on the command line. (This is
* by far the most common case.)
* - From the first object contained within the first archive
* on the command line.
* - If all else fails, we assume a 32-bit object for the native machine.
*
* entry:
* argc, argv - Command line argument vector
* class_ret - Address of variable to receive ELFCLASS of output object
* ldclass_ret - Address of variable to receive ELFCLASS of
* linker to use. This will be ELFCLASS32/ELFCLASS64 if one
* is explicitly specified, and ELFCLASSNONE otherwise.
* ELFCLASSNONE therefore means that we should use the best
*/
static int
{
int c, ar_found = 0;
/*
* In general, libld.so is responsible for processing the
* command line options. The exception to this are those options
* that contain information about which linker to run and the
* here looking for the following:
*
* -32 Produce an ELFCLASS32 object. This is the default, so
* -32 is only needed when linking entirely from archives,
* and the first archive contains a mix of 32 and 64-bit
* objects, and the first object in that archive is 64-bit.
* We do not expect this option to get much use, but it
* ensures that the user can handle any situation.
*
* -64 Produce an ELFCLASS64 object. (Note that this will
* indirectly cause the use of the 64-bit linker if
* the system is 64-bit capable). The most common need
* for this option is when linking a filter object entirely
* from a mapfile. The less common case is when linking
* entirely from archives, and the first archive contains
* a mix of 32 and 64-bit objects, and the first object
* in that archive is 32-bit.
*
* -z altexec64
* Use the 64-bit linker regardless of the class
* of the output object.
*
* -z target=platform
* Produce output object for the specified platform.
* This option is needed when producing an object
* for a non-native target entirely from a mapfile,
* or when linking entirely from an archive containing
* objects for multiple targets, and the first object
* in the archive is not for the desired target.
*
* If we've already processed an object and we find -32/-64, and
* the object is of the wrong class, we have an error condition.
* We ignore it here, and let it fall through to libld, where the
* proper diagnosis and error message will occur.
*/
opterr = 0;
optind = 1;
switch (c) {
case '3':
MSG_ARG_TWO_SIZE) == 0)
class = ELFCLASS32;
break;
case '6':
MSG_ARG_FOUR_SIZE) == 0)
class = ELFCLASS64;
break;
case 'z':
#if !defined(_LP64)
/* -z altexec64 */
MSG_ARG_ALTEXEC64_SIZE) == 0) {
break;
}
#endif
/* -z target=platform */
MSG_ARG_TARGET_SIZE) == 0) {
if (strcasecmp(pstr,
MSG_ORIG(MSG_TARG_SPARC)) == 0) {
mach64 = EM_SPARCV9;
} else if (strcasecmp(pstr,
MSG_ORIG(MSG_TARG_X86)) == 0) {
} else {
return (1);
}
}
break;
}
}
/*
* Continue to look for the first ELF object to determine the class of
* objects to operate on. At the same time, look for the first archive
* of ELF objects --- if no plain ELF object is specified, the type
* of the first ELF object in the first archive will be used. If
* there is no object, and no archive, then we fall back to a 32-bit
* object for the native machine.
*/
int fd;
/*
* If we detect some more options return to getopt().
* Checking argv[optind][1] against null prevents a forever
* loop if an unadorned `-' argument is passed to us.
*/
continue;
else
goto getmore;
}
/*
* If we've already determined the object class and
* machine type, continue to the next argument. Only
* the first object contributes to this decision, and
* there's no value to opening or examing the subsequent
* ones. We do need to keep going though, because there
* may be additional options that might affect our
*/
continue;
/*
* Open the file and determine if it is an object. We are
* looking for ELF objects, or archives of ELF objects.
*
* Plain objects are simple, and are the common case, so
* we examine them directly and avoid the map-unmap-map
* that would occur if we used libelf. Archives are too
* complex to be worth accessing directly, so if we identify
* an archive, we use libelf on it and accept the cost.
*/
return (1);
}
continue;
}
if (class == ELFCLASSNONE) {
if ((class != ELFCLASS32) &&
(class != ELFCLASS64))
}
int ld_elfdata;
/*
* Both the 32 and 64-bit versions get the
* type from the object. If the user has
* combination, libld will catch it.
*/
}
} else if (!ar_found &&
(void) elf_version(EV_CURRENT);
continue;
}
ar_found =
}
}
/*
* ELFCLASS of output object: If we did not establish a class from a
* command option, or from the first plain object, then use the class
* from the first archive, and failing that, default to 32-bit.
*/
if (class == ELFCLASSNONE)
/* ELFCLASS of link-editor to use */
*ldclass_ret = ldclass;
/*
* Machine type of output object: If we did not establish a machine
* type from the command line, or from the first plain object, then
* use the machine established by the first archive, and failing that,
* use the native machine.
*/
if (ar_found)
else
return (0);
}
/*
* Process an LD_OPTIONS environment string. This routine is first called to
* count the number of options, and second to initialize a new argument array
* with each option.
*/
static int
{
int argc = 0;
/*
* Walk the environment string processing any arguments that are
* separated by white space.
*/
while (*str != '\0') {
/*
* If a new argument array has been provided, terminate
* the original environment string, and initialize the
* appropriate argument array entry.
*/
if (nargv) {
*str++ = '\0';
}
argc++;
str++;
} else
str++;
}
/*
* If a new argument array has been provided, initialize the
* final argument array entry.
*/
if (nargv)
argc++;
}
return (argc);
}
/*
* Determine whether an LD_OPTIONS environment variable is set, and if so,
* prepend environment string as a series of options to the argv array.
*/
static int
{
int nargc;
return (0);
/*
* Get rid of any leading white space, and make sure the environment
* string has size.
*/
while (isspace(*ld_options))
ld_options++;
if (ld_options[0] == '\0')
return (0);
/*
* Prevent modification of actual environment strings.
*/
return (1);
}
/*
* Determine the number of options provided.
*/
/*
* Allocate a new argv array big enough to hold the new options from
* the environment string and the old argv options.
*/
return (1);
}
/*
* Initialize first element of new argv array to be the first element
* of the old argv array (ie. calling programs name). Then add the new
* args obtained from the environment.
*/
nargc = 0;
/*
* Now add the original argv array (skipping argv[0]) to the end of the
* new argv array, and re-vector argc and argv to reference this new
* array
*/
return (0);
}
/*
* Check to see if there is a LD_ALTEXEC=<path to alternate ld> in the
* environment. If so, first null the environment variable out, and then
* exec() the binary pointed to by the environment variable, passing the same
* arguments as the originating process. This mechanism permits using
* environments.
*/
static int
{
char *execstr;
char **str;
int err;
MSG_LD_ALTEXEC_SIZE) == 0) {
break;
}
}
/*
* If LD_ALTEXEC isn't set, return to continue executing the present
* link-editor.
*/
if (*str == 0)
return (0);
/*
* Get a pointer to the actual string. If it's a null entry, return.
*/
if (*execstr == '\0')
return (0);
/*
* Null out the LD_ALTEXEC= environment entry.
*/
/*
* Set argv[0] to point to our new linker
*/
/*
* And attempt to execute it.
*/
/*
* If the exec() fails, return a failure indication.
*/
return (1);
}
int
{
/*
* Establish locale.
*/
/*
* Execute an alternate linker if the LD_ALTEXEC environment variable is
* set. If a specified alternative could not be found, bail.
*/
return (1);
/*
* Check the LD_OPTIONS environment variable, and if present prepend
* the arguments specified to the command line argument list.
*/
return (1);
/*
* Examine the command arguments to determine:
* - object class
* - link-editor class
* - target machine
*/
return (1);
/*
* Unless a 32-bit link-editor was explicitly requested, try
* to exec the 64-bit version.
*/
if (ldclass != ELFCLASS32)
/*
* If an attempt to exec the 64-bit link-editor fails:
* - Bail if the 64-bit linker was explicitly requested
* - Continue quietly if the 64-bit linker was not requested.
* and therefore represents the best we can do. Note that
* the 32-bit linker is capable of linking anything the
* 64-bit version is, subject to a 4GB limit on memory, and
* 2GB object size.
*/
return (1);
}
/* Call the libld entry point for the specified ELFCLASS */
if (class == ELFCLASS64)
else
}
/*
* We supply this function for the msg module
*/
const char *
{
}