Home | History | Annotate | Download | only in common
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 
     22 /*
     23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 #include	<stdio.h>
     28 #include	<stdlib.h>
     29 #include	<unistd.h>
     30 #include	<stdarg.h>
     31 #include	<string.h>
     32 #include	<strings.h>
     33 #include	<errno.h>
     34 #include	<fcntl.h>
     35 #include	<libintl.h>
     36 #include	<locale.h>
     37 #include	<fcntl.h>
     38 #include	<ar.h>
     39 #include	<gelf.h>
     40 #include	"conv.h"
     41 #include	"libld.h"
     42 #include	"machdep.h"
     43 #include	"msg.h"
     44 
     45 /*
     46  * The following prevent us from having to include ctype.h which defines these
     47  * functions as macros which reference the __ctype[] array.  Go through .plt's
     48  * to get to these functions in libc rather than have every invocation of ld
     49  * have to suffer the R_SPARC_COPY overhead of the __ctype[] array.
     50  */
     51 extern int	isspace(int);
     52 
     53 /*
     54  * We examine ELF objects, and archives containing ELF objects, in order
     55  * to determine the ELFCLASS of the resulting object and/or the linker to be
     56  * used. We want to avoid the overhead of libelf for this, at least until
     57  * we are certain that we need it, so we start by reading bytes from
     58  * the beginning of the file. This type defines the buffer used to read
     59  * these initial bytes.
     60  *
     61  * A plain ELF object will start with an ELF header, whereas an archive
     62  * starts with a magic string (ARMAG) that is SARMAG bytes long. Any valid
     63  * ELF file or archive will contain more bytes than this buffer, so any
     64  * file shorter than this can be safely assummed not to be of interest.
     65  *
     66  * The ELF header for ELFCLASS32 and ELFCLASS64 are identical up through the
     67  * the e_version field, and all the information we require is found in this
     68  * common prefix. Furthermore, this cannot change, as the layout of an ELF
     69  * header is fixed by the ELF ABI. Hence, the ehdr part of this union is
     70  * not a full ELF header, but only the class-independent prefix that we need.
     71  *
     72  * As this is a raw (non-libelf) read, we are responsible for handling any
     73  * byte order difference between the object and the system running this
     74  * program when we read any datum larger than a byte (i.e. e_machine) from
     75  * this header.
     76  */
     77 typedef union {
     78 	struct {	/* Must match start of ELFxx_Ehdr in <sys/elf.h> */
     79 		uchar_t		e_ident[EI_NIDENT];	/* ident bytes */
     80 		Half		e_type;			/* file type */
     81 		Half		e_machine;		/* target machine */
     82 	} ehdr;
     83 	char			armag[SARMAG];
     84 } FILE_HDR;
     85 
     86 
     87 /*
     88  * Print a message to stdout
     89  */
     90 /* VARARGS3 */
     91 void
     92 eprintf(Lm_list *lml, Error error, const char *format, ...)
     93 {
     94 	va_list			args;
     95 	static const char	*strings[ERR_NUM] = { MSG_ORIG(MSG_STR_EMPTY) };
     96 
     97 #if	defined(lint)
     98 	/*
     99 	 * The lml argument is only meaningful for diagnostics sent to ld.so.1.
    100 	 * Supress the lint error by making a dummy assignment.
    101 	 */
    102 	lml = 0;
    103 #endif
    104 	if (error > ERR_NONE) {
    105 		if (error == ERR_WARNING) {
    106 			if (strings[ERR_WARNING] == 0)
    107 				strings[ERR_WARNING] =
    108 				    MSG_INTL(MSG_ERR_WARNING);
    109 		} else if (error == ERR_FATAL) {
    110 			if (strings[ERR_FATAL] == 0)
    111 				strings[ERR_FATAL] = MSG_INTL(MSG_ERR_FATAL);
    112 		} else if (error == ERR_ELF) {
    113 			if (strings[ERR_ELF] == 0)
    114 				strings[ERR_ELF] = MSG_INTL(MSG_ERR_ELF);
    115 		}
    116 		(void) fputs(MSG_ORIG(MSG_STR_LDDIAG), stderr);
    117 	}
    118 	(void) fputs(strings[error], stderr);
    119 
    120 	va_start(args, format);
    121 	(void) vfprintf(stderr, format, args);
    122 	if (error == ERR_ELF) {
    123 		int	elferr;
    124 
    125 		if ((elferr = elf_errno()) != 0)
    126 			(void) fprintf(stderr, MSG_ORIG(MSG_STR_ELFDIAG),
    127 			    elf_errmsg(elferr));
    128 	}
    129 	(void) fprintf(stderr, MSG_ORIG(MSG_STR_NL));
    130 	(void) fflush(stderr);
    131 	va_end(args);
    132 }
    133 
    134 
    135 /*
    136  * Examine the first object in an archive to determine its ELFCLASS
    137  * and machine type.
    138  *
    139  * entry:
    140  *	fd - Open file descriptor for file
    141  *	elf - libelf ELF descriptor
    142  *	class_ret, mach_ret - Address of variables to receive ELFCLASS
    143  *		and machine type.
    144  *
    145  * exit:
    146  *	On success, *class_ret and *mach_ret are filled in, and True (1)
    147  *	is returned. On failure, False (0) is returned.
    148  */
    149 static int
    150 archive(int fd, Elf *elf, uchar_t *class_ret, Half *mach_ret)
    151 {
    152 	Elf_Cmd		cmd = ELF_C_READ;
    153 	Elf_Arhdr	*arhdr;
    154 	Elf		*_elf = NULL;
    155 	int		found = 0;
    156 
    157 	/*
    158 	 * Process each item within the archive until we find the first
    159 	 * ELF object, or alternatively another archive to recurse into.
    160 	 * Stop after analyzing the first plain object found.
    161 	 */
    162 	while (!found && ((_elf = elf_begin(fd, cmd, elf)) != NULL)) {
    163 		if ((arhdr = elf_getarhdr(_elf)) == NULL)
    164 			return (0);
    165 		if (*arhdr->ar_name != '/') {
    166 			switch (elf_kind(_elf)) {
    167 			case ELF_K_AR:
    168 				found = archive(fd, _elf, class_ret, mach_ret);
    169 				break;
    170 			case ELF_K_ELF:
    171 				if (gelf_getclass(_elf) == ELFCLASS64) {
    172 					Elf64_Ehdr *ehdr;
    173 
    174 					if ((ehdr = elf64_getehdr(_elf)) ==
    175 					    NULL)
    176 						break;
    177 					*class_ret = ehdr->e_ident[EI_CLASS];
    178 					*mach_ret = ehdr->e_machine;
    179 				} else {
    180 					Elf32_Ehdr *ehdr;
    181 
    182 					if ((ehdr = elf32_getehdr(_elf)) ==
    183 					    NULL)
    184 						break;
    185 					*class_ret = ehdr->e_ident[EI_CLASS];
    186 					*mach_ret = ehdr->e_machine;
    187 				}
    188 				found = 1;
    189 				break;
    190 			}
    191 		}
    192 
    193 		cmd = elf_next(_elf);
    194 		(void) elf_end(_elf);
    195 	}
    196 
    197 	return (found);
    198 }
    199 
    200 /*
    201  * Determine:
    202  *	- ELFCLASS of resulting object (class)
    203  *	- Whether user specified class of the linker (ldclass)
    204  *	- ELF machine type of resulting object (m_mach)
    205  *
    206  * In order of priority, we determine this information as follows:
    207  *
    208  * -	Command line options (-32, -64, -z altexec64, -z target).
    209  * -	From the first plain object seen on the command line. (This is
    210  *	by far the most common case.)
    211  * -	From the first object contained within the first archive
    212  *	on the command line.
    213  * -	If all else fails, we assume a 32-bit object for the native machine.
    214  *
    215  * entry:
    216  *	argc, argv - Command line argument vector
    217  *	class_ret - Address of variable to receive ELFCLASS of output object
    218  *	ldclass_ret - Address of variable to receive ELFCLASS of
    219  *		linker to use. This will be ELFCLASS32/ELFCLASS64 if one
    220  *		is explicitly specified, and ELFCLASSNONE otherwise.
    221  *		ELFCLASSNONE therefore means that we should use the best
    222  *		link-editor that the system/kernel will allow.
    223  */
    224 static int
    225 process_args(int argc, char **argv, uchar_t *class_ret, uchar_t *ldclass_ret,
    226     Half *mach)
    227 {
    228 	uchar_t	ldclass = ELFCLASSNONE, class = ELFCLASSNONE, ar_class;
    229 	Half	mach32 = EM_NONE, mach64 = EM_NONE, ar_mach;
    230 	int	c, ar_found = 0;
    231 
    232 	/*
    233 	 * In general, libld.so is responsible for processing the
    234 	 * command line options. The exception to this are those options
    235 	 * that contain information about which linker to run and the
    236 	 * class/machine of the output object. We examine the options
    237 	 * here looking for the following:
    238 	 *
    239 	 *	-32	Produce an ELFCLASS32 object. This is the default, so
    240 	 *		-32 is only needed when linking entirely from archives,
    241 	 *		and the first archive contains a mix of 32 and 64-bit
    242 	 *		objects, and the first object in that archive is 64-bit.
    243 	 *		We do not expect this option to get much use, but it
    244 	 *		ensures that the user can handle any situation.
    245 	 *
    246 	 *	-64	Produce an ELFCLASS64 object. (Note that this will
    247 	 *		indirectly cause the use of the 64-bit linker if
    248 	 *		the system is 64-bit capable). The most common need
    249 	 *		for this option is when linking a filter object entirely
    250 	 *		from a mapfile. The less common case is when linking
    251 	 *		entirely from archives, and the first archive contains
    252 	 *		a mix of 32 and 64-bit objects, and the first object
    253 	 *		in that archive is 32-bit.
    254 	 *
    255 	 *	-z altexec64
    256 	 *		Use the 64-bit linker regardless of the class
    257 	 *		of the output object.
    258 	 *
    259 	 *	-z target=platform
    260 	 *		Produce output object for the specified platform.
    261 	 *		This option is needed when producing an object
    262 	 *		for a non-native target entirely from a mapfile,
    263 	 *		or when linking entirely from an archive containing
    264 	 *		objects for multiple targets, and the first object
    265 	 *		in the archive is not for the desired target.
    266 	 *
    267 	 * If we've already processed an object and we find -32/-64, and
    268 	 * the object is of the wrong class, we have an error condition.
    269 	 * We ignore it here, and let it fall through to libld, where the
    270 	 * proper diagnosis and error message will occur.
    271 	 */
    272 	opterr = 0;
    273 	optind = 1;
    274 getmore:
    275 	while ((c = ld_getopt(0, optind, argc, argv)) != -1) {
    276 		switch (c) {
    277 		case '3':
    278 			if (strncmp(optarg, MSG_ORIG(MSG_ARG_TWO),
    279 			    MSG_ARG_TWO_SIZE) == 0)
    280 				class = ELFCLASS32;
    281 			break;
    282 
    283 		case '6':
    284 			if (strncmp(optarg, MSG_ORIG(MSG_ARG_FOUR),
    285 			    MSG_ARG_FOUR_SIZE) == 0)
    286 				class = ELFCLASS64;
    287 			break;
    288 
    289 		case 'z':
    290 #if	!defined(_LP64)
    291 			/* -z altexec64 */
    292 			if (strncmp(optarg, MSG_ORIG(MSG_ARG_ALTEXEC64),
    293 			    MSG_ARG_ALTEXEC64_SIZE) == 0) {
    294 				ldclass = ELFCLASS64;
    295 				break;
    296 			}
    297 #endif
    298 			/* -z target=platform */
    299 			if (strncmp(optarg, MSG_ORIG(MSG_ARG_TARGET),
    300 			    MSG_ARG_TARGET_SIZE) == 0) {
    301 				char *pstr = optarg + MSG_ARG_TARGET_SIZE;
    302 
    303 				if (strcasecmp(pstr,
    304 				    MSG_ORIG(MSG_TARG_SPARC)) == 0) {
    305 					mach32 = EM_SPARC;
    306 					mach64 = EM_SPARCV9;
    307 				} else if (strcasecmp(pstr,
    308 				    MSG_ORIG(MSG_TARG_X86)) == 0) {
    309 					mach32 = EM_386;
    310 					mach64 = EM_AMD64;
    311 				} else {
    312 					eprintf(0, ERR_FATAL,
    313 					    MSG_INTL(MSG_ERR_BADTARG), pstr);
    314 					return (1);
    315 				}
    316 			}
    317 			break;
    318 		}
    319 	}
    320 
    321 	/*
    322 	 * Continue to look for the first ELF object to determine the class of
    323 	 * objects to operate on. At the same time, look for the first archive
    324 	 * of ELF objects --- if no plain ELF object is specified, the type
    325 	 * of the first ELF object in the first archive will be used. If
    326 	 * there is no object, and no archive, then we fall back to a 32-bit
    327 	 * object for the native machine.
    328 	 */
    329 	for (; optind < argc; optind++) {
    330 		int		fd;
    331 		FILE_HDR	hdr;
    332 
    333 		/*
    334 		 * If we detect some more options return to getopt().
    335 		 * Checking argv[optind][1] against null prevents a forever
    336 		 * loop if an unadorned `-' argument is passed to us.
    337 		 */
    338 		if (argv[optind][0] == '-') {
    339 			if (argv[optind][1] == '\0')
    340 				continue;
    341 			else
    342 				goto getmore;
    343 		}
    344 
    345 		/*
    346 		 * If we've already determined the object class and
    347 		 * machine type, continue to the next argument. Only
    348 		 * the first object contributes to this decision, and
    349 		 * there's no value to opening or examing the subsequent
    350 		 * ones. We do need to keep going though, because there
    351 		 * may be additional options that might affect our
    352 		 * class/machine decision.
    353 		 */
    354 		if ((class != ELFCLASSNONE) && (mach32 != EM_NONE))
    355 			continue;
    356 
    357 		/*
    358 		 * Open the file and determine if it is an object. We are
    359 		 * looking for ELF objects, or archives of ELF objects.
    360 		 *
    361 		 * Plain objects are simple, and are the common case, so
    362 		 * we examine them directly and avoid the map-unmap-map
    363 		 * that would occur if we used libelf. Archives are too
    364 		 * complex to be worth accessing directly, so if we identify
    365 		 * an archive, we use libelf on it and accept the cost.
    366 		 */
    367 		if ((fd = open(argv[optind], O_RDONLY)) == -1) {
    368 			int err = errno;
    369 
    370 			eprintf(0, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
    371 			    argv[optind], strerror(err));
    372 			return (1);
    373 		}
    374 
    375 		if (pread(fd, &hdr, sizeof (hdr), 0) != sizeof (hdr)) {
    376 			(void) close(fd);
    377 			continue;
    378 		}
    379 
    380 		if ((hdr.ehdr.e_ident[EI_MAG0] == ELFMAG0) &&
    381 		    (hdr.ehdr.e_ident[EI_MAG1] == ELFMAG1) &&
    382 		    (hdr.ehdr.e_ident[EI_MAG2] == ELFMAG2) &&
    383 		    (hdr.ehdr.e_ident[EI_MAG3] == ELFMAG3)) {
    384 			if (class == ELFCLASSNONE) {
    385 				class = hdr.ehdr.e_ident[EI_CLASS];
    386 				if ((class != ELFCLASS32) &&
    387 				    (class != ELFCLASS64))
    388 					class = ELFCLASSNONE;
    389 			}
    390 
    391 			if (mach32 == EM_NONE) {
    392 				int	one = 1;
    393 				uchar_t	*one_p = (uchar_t *)&one;
    394 				int	ld_elfdata;
    395 
    396 				ld_elfdata = (one_p[0] == 1) ?
    397 				    ELFDATA2LSB : ELFDATA2MSB;
    398 				/*
    399 				 * Both the 32 and 64-bit versions get the
    400 				 * type from the object. If the user has
    401 				 * asked for an inconsistant class/machine
    402 				 * combination, libld will catch it.
    403 				 */
    404 				mach32 = mach64 =
    405 				    (ld_elfdata == hdr.ehdr.e_ident[EI_DATA]) ?
    406 				    hdr.ehdr.e_machine :
    407 				    BSWAP_HALF(hdr.ehdr.e_machine);
    408 			}
    409 		} else if (!ar_found &&
    410 		    (memcmp(&hdr.armag, ARMAG, SARMAG) == 0)) {
    411 			Elf	*elf;
    412 
    413 			(void) elf_version(EV_CURRENT);
    414 			if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
    415 				(void) close(fd);
    416 				continue;
    417 			}
    418 			if (elf_kind(elf) == ELF_K_AR)
    419 				ar_found =
    420 				    archive(fd, elf, &ar_class, &ar_mach);
    421 			(void) elf_end(elf);
    422 		}
    423 
    424 		(void) close(fd);
    425 	}
    426 
    427 	/*
    428 	 * ELFCLASS of output object: If we did not establish a class from a
    429 	 * command option, or from the first plain object, then use the class
    430 	 * from the first archive, and failing that, default to 32-bit.
    431 	 */
    432 	if (class == ELFCLASSNONE)
    433 		class = ar_found ? ar_class : ELFCLASS32;
    434 	*class_ret = class;
    435 
    436 	/* ELFCLASS of link-editor to use */
    437 	*ldclass_ret = ldclass;
    438 
    439 	/*
    440 	 * Machine type of output object: If we did not establish a machine
    441 	 * type from the command line, or from the first plain object, then
    442 	 * use the machine established by the first archive, and failing that,
    443 	 * use the native machine.
    444 	 */
    445 	*mach = (class == ELFCLASS64) ? mach64 : mach32;
    446 	if (*mach == EM_NONE)
    447 		if (ar_found)
    448 			*mach = ar_mach;
    449 		else
    450 			*mach = (class == ELFCLASS64) ? M_MACH_64 : M_MACH_32;
    451 
    452 	return (0);
    453 }
    454 
    455 /*
    456  * Process an LD_OPTIONS environment string.  This routine is first called to
    457  * count the number of options, and second to initialize a new argument array
    458  * with each option.
    459  */
    460 static int
    461 process_ldoptions(char *str, char **nargv)
    462 {
    463 	int	argc = 0;
    464 	char	*arg = str;
    465 
    466 	/*
    467 	 * Walk the environment string processing any arguments that are
    468 	 * separated by white space.
    469 	 */
    470 	while (*str != '\0') {
    471 		if (isspace(*str)) {
    472 			/*
    473 			 * If a new argument array has been provided, terminate
    474 			 * the original environment string, and initialize the
    475 			 * appropriate argument array entry.
    476 			 */
    477 			if (nargv) {
    478 				*str++ = '\0';
    479 				nargv[argc] = arg;
    480 			}
    481 
    482 			argc++;
    483 			while (isspace(*str))
    484 				str++;
    485 			arg = str;
    486 		} else
    487 			str++;
    488 	}
    489 	if (arg != str) {
    490 		/*
    491 		 * If a new argument array has been provided, initialize the
    492 		 * final argument array entry.
    493 		 */
    494 		if (nargv)
    495 			nargv[argc] = arg;
    496 		argc++;
    497 	}
    498 
    499 	return (argc);
    500 }
    501 
    502 /*
    503  * Determine whether an LD_OPTIONS environment variable is set, and if so,
    504  * prepend environment string as a series of options to the argv array.
    505  */
    506 static int
    507 prepend_ldoptions(int *argcp, char ***argvp)
    508 {
    509 	int	nargc;
    510 	char	**nargv, *ld_options;
    511 	int	err, count;
    512 
    513 	if ((ld_options = getenv(MSG_ORIG(MSG_LD_OPTIONS))) == NULL)
    514 		return (0);
    515 
    516 	/*
    517 	 * Get rid of any leading white space, and make sure the environment
    518 	 * string has size.
    519 	 */
    520 	while (isspace(*ld_options))
    521 		ld_options++;
    522 	if (*ld_options == '\0')
    523 		return (0);
    524 
    525 	/*
    526 	 * Prevent modification of actual environment strings.
    527 	 */
    528 	if ((ld_options = strdup(ld_options)) == NULL) {
    529 		err = errno;
    530 		eprintf(0, ERR_FATAL, MSG_INTL(MSG_SYS_ALLOC), strerror(err));
    531 		return (1);
    532 	}
    533 
    534 	/*
    535 	 * Determine the number of options provided.
    536 	 */
    537 	nargc = process_ldoptions(ld_options, NULL);
    538 
    539 	/*
    540 	 * Allocate a new argv array big enough to hold the new options from
    541 	 * the environment string and the old argv options.
    542 	 */
    543 	if ((nargv = malloc((nargc + *argcp + 1) * sizeof (char *))) == NULL) {
    544 		err = errno;
    545 		eprintf(0, ERR_FATAL, MSG_INTL(MSG_SYS_ALLOC), strerror(err));
    546 		return (1);
    547 	}
    548 
    549 	/*
    550 	 * Initialize first element of new argv array to be the first element
    551 	 * of the old argv array (ie. calling programs name).  Then add the new
    552 	 * args obtained from the environment.
    553 	 */
    554 	nargc = 0;
    555 	nargv[nargc++] = (*argvp)[0];
    556 	nargc += process_ldoptions(ld_options, &nargv[nargc]);
    557 
    558 	/*
    559 	 * Now add the original argv array (skipping argv[0]) to the end of the
    560 	 * new argv array, and re-vector argc and argv to reference this new
    561 	 * array
    562 	 */
    563 	for (count = 1; count < *argcp; count++, nargc++)
    564 		nargv[nargc] = (*argvp)[count];
    565 
    566 	nargv[nargc] = NULL;
    567 
    568 	*argcp = nargc;
    569 	*argvp = nargv;
    570 
    571 	return (0);
    572 }
    573 
    574 /*
    575  * Check to see if there is a LD_ALTEXEC=<path to alternate ld> in the
    576  * environment.  If so, first null the environment variable out, and then
    577  * exec() the binary pointed to by the environment variable, passing the same
    578  * arguments as the originating process.  This mechanism permits using
    579  * alternate link-editors (debugging/developer copies) even in complex build
    580  * environments.
    581  */
    582 static int
    583 ld_altexec(char **argv, char **envp)
    584 {
    585 	char	*execstr;
    586 	char	**str;
    587 	int	err;
    588 
    589 	for (str = envp; *str; str++) {
    590 		if (strncmp(*str, MSG_ORIG(MSG_LD_ALTEXEC),
    591 		    MSG_LD_ALTEXEC_SIZE) == 0) {
    592 			break;
    593 		}
    594 	}
    595 
    596 	/*
    597 	 * If LD_ALTEXEC isn't set, return to continue executing the present
    598 	 * link-editor.
    599 	 */
    600 	if (*str == 0)
    601 		return (0);
    602 
    603 	/*
    604 	 * Get a pointer to the actual string.  If it's a null entry, return.
    605 	 */
    606 	execstr = strdup(*str + MSG_LD_ALTEXEC_SIZE);
    607 	if (*execstr == '\0')
    608 		return (0);
    609 
    610 	/*
    611 	 * Null out the LD_ALTEXEC= environment entry.
    612 	 */
    613 	(*str)[MSG_LD_ALTEXEC_SIZE] = '\0';
    614 
    615 	/*
    616 	 * Set argv[0] to point to our new linker
    617 	 */
    618 	argv[0] = execstr;
    619 
    620 	/*
    621 	 * And attempt to execute it.
    622 	 */
    623 	(void) execve(execstr, argv, envp);
    624 
    625 	/*
    626 	 * If the exec() fails, return a failure indication.
    627 	 */
    628 	err = errno;
    629 	eprintf(0, ERR_FATAL, MSG_INTL(MSG_SYS_EXEC), execstr,
    630 	    strerror(err));
    631 	return (1);
    632 }
    633 
    634 int
    635 main(int argc, char **argv, char **envp)
    636 {
    637 	char		**oargv = argv;
    638 	uchar_t 	class, ldclass, checkclass;
    639 	Half		mach;
    640 
    641 	/*
    642 	 * Establish locale.
    643 	 */
    644 	(void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY));
    645 	(void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS));
    646 
    647 	/*
    648 	 * Execute an alternate linker if the LD_ALTEXEC environment variable is
    649 	 * set.  If a specified alternative could not be found, bail.
    650 	 */
    651 	if (ld_altexec(argv, envp))
    652 		return (1);
    653 
    654 	/*
    655 	 * Check the LD_OPTIONS environment variable, and if present prepend
    656 	 * the arguments specified to the command line argument list.
    657 	 */
    658 	if (prepend_ldoptions(&argc, &argv))
    659 		return (1);
    660 
    661 	/*
    662 	 * Examine the command arguments to determine:
    663 	 *	- object class
    664 	 *	- link-editor class
    665 	 *	- target machine
    666 	 */
    667 	if (process_args(argc, argv, &class, &ldclass, &mach))
    668 		return (1);
    669 
    670 	/*
    671 	 * Unless a 32-bit link-editor was explicitly requested, try
    672 	 * to exec the 64-bit version.
    673 	 */
    674 	if (ldclass != ELFCLASS32)
    675 		checkclass = conv_check_native(oargv, envp);
    676 
    677 	/*
    678 	 * If an attempt to exec the 64-bit link-editor fails:
    679 	 * -	Bail if the 64-bit linker was explicitly requested
    680 	 * -	Continue quietly if the 64-bit linker was not requested.
    681 	 *	This is undoubtedly due to hardware/kernel limitations,
    682 	 *	and therefore represents the best we can do. Note that
    683 	 *	the 32-bit linker is capable of linking anything the
    684 	 *	64-bit version is, subject to a 4GB limit on memory, and
    685 	 *	2GB object size.
    686 	 */
    687 	if ((ldclass == ELFCLASS64) && (checkclass != ELFCLASS64)) {
    688 		eprintf(0, ERR_FATAL, MSG_INTL(MSG_SYS_64));
    689 		return (1);
    690 	}
    691 
    692 	/* Call the libld entry point for the specified ELFCLASS */
    693 	if (class == ELFCLASS64)
    694 		return (ld64_main(argc, argv, mach));
    695 	else
    696 		return (ld32_main(argc, argv, mach));
    697 }
    698 
    699 /*
    700  * We supply this function for the msg module
    701  */
    702 const char *
    703 _ld_msg(Msg mid)
    704 {
    705 	return (gettext(MSG_ORIG(mid)));
    706 }
    707