ld.c revision ba2be53024c0b999e74ba9adcd7d80fec5df8c57
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#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 "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);
/*
* Print a message to stdout
*/
/* VARARGS3 */
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
if (error == ERR_WARNING) {
if (strings[ERR_WARNING] == 0)
}
}
int elferr;
elf_errmsg(elferr));
}
}
/*
* Determine:
* - ELFCLASS of resulting object (aoutclass)
* - Whether we need the 32 or 64-bit libld (ldclass)
* - ELF machine type of resulting object (m_mach)
*/
static int
{
#if defined(_LP64)
#else
#endif
int c;
/*
* 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:
*
* -64
* Produce an ELFCLASS64 object. Use the 64-bit linker.
*
* -zaltexec64
* Use the 64-bit linker regardless of the class
* of the output object.
*
* -z target=platform
* Produce output object for the specified platform.
*
* The -64 and -ztarget options are used when the only input to
* ld() is a mapfile or archive, and a 64-bit or non-native output
* object is required.
*
* If we've already processed a 32-bit object and we find -64, we have
* an error condition, but let this fall through to libld to obtain the
* default error message.
*/
opterr = 0;
switch (c) {
case '6':
MSG_ARG_FOUR_SIZE) == 0)
aclass = ELFCLASS64;
break;
case 'z':
#if !defined(_LP64)
/* -z altexec64 */
MSG_ARG_ALTEXEC64_SIZE) == 0) {
lclass = ELFCLASS64;
break;
}
#endif
/* -z target=platform */
MSG_ARG_TARGET_SIZE) == 0) {
char *pstr =
if (strcasecmp(pstr,
MSG_ORIG(MSG_TARG_SPARC)) == 0) {
mach64 = EM_SPARCV9;
} else if (strcasecmp(pstr,
MSG_ORIG(MSG_TARG_X86)) == 0) {
} else {
pstr);
return (1);
}
}
break;
default:
break;
}
}
/*
* Continue to look for the first ELF object to determine the class of
* objects to operate on.
*/
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 the files ELF class.
*/
return (1);
}
/*
* Note that we read an entire 32-bit ELF header struct
* here, even though we have yet to determine that the
* file is an ELF object or that it is ELFCLASS32. We
* do this because:
* - Any valid ELF object of any class must
* have at least this number of bytes in it,
* since an ELF header is manditory, and since
* a 32-bit header is smaller than a 64-bit one.
* - The 32 and 64-bit ELF headers are identical
* up through the e_version field, so we can
* obtain the e_machine value of a 64-bit
* object via the e_machine value we read into
* the 32-bit version. This cannot change, because
* the layout of an ELF header is fixed by the ABI.
*
* Note however that we do have to worry about the byte
* order difference between the object and the system
* running this program when we read the e_machine value,
* since it is a multi-byte value;
*/
if (aclass == ELFCLASSNONE) {
if ((aclass != ELFCLASS32) &&
(aclass != ELFCLASS64))
}
int one = 1;
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.
*/
}
}
}
/*
* If we couldn't establish a class, default to 32-bit.
*/
if (aclass == ELFCLASSNONE)
aclass = ELFCLASS32;
if (lclass == ELFCLASSNONE)
lclass = ELFCLASS32;
/*
* Use the machine type that goes with the class we've determined.
* If we didn't find a usable machine type, use the native
* machine.
*/
return (0);
}
/*
* Prepend environment string as a series of options to the argv array.
*/
static int
{
int nargc; /* new argc */
char **nargv; /* new argv */
int count;
/*
* Get rid of leading white space, and make sure the string has size.
*/
while (isspace(*ld_options))
ld_options++;
if (*ld_options == '\0')
return (1);
nargc = 0;
/*
* Walk the environment string counting any arguments that are
* separated by white space.
*/
while (*string != '\0') {
nargc++;
string++;
} else
string++;
}
nargc++;
/*
* Allocate a new argv array big enough to hold the new options from
* the environment string and the old argv options.
*/
return (0);
}
/*
* 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;
while (*string != '\0') {
nargc++;
*string++ = '\0';
string++;
} else
string++;
}
nargc++;
}
/*
* Now add the original argv array (skipping argv[0]) to the end of the
* new argv array, and overwrite the old argc and argv.
*/
nargc++;
}
return (1);
}
/*
* 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
{
/*
* XX64 -- Strip "-Wl," from the head of each argument. This is to
* accommodate awkwardness in passing ld arguments to gcc while
* maintaining the structure of the OSNet build environment's Makefiles.
*/
{
int i;
char *p;
for (i = 0; i < argc; i++) {
p = argv[i];
argv[i] = (p += 4);
}
}
/*
* 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.
*/
/*
* Prevent modification of actual environment strings.
*/
return (1);
}
/*
* Examine the command arguments to determine:
* - object class
* - link-editor class
* - target machine
*/
return (1);
/*
* If we're processing 64-bit objects, or the user specifically asked
* for a 64-bit link-editor, determine if a 64-bit ld() can be executed.
* Bail if a 64-bit ld() was explicitly asked for, but one could not be
* found.
*/
return (1);
}
/*
* Reset the getopt(3c) error message flag, and call the generic entry
* point using the appropriate class.
*/
if (aoutclass == ELFCLASS64)
else
}
/*
* Exported interfaces required by our dependencies. libld and friends bind to
* the different implementations of these provided by either ld or ld.so.1.
*/
const char *
{
}