10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51618Srie * Common Development and Distribution License (the "License").
61618Srie * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211618Srie
220Sstevel@tonic-gate /*
23*13074SAli.Bahrami@Oracle.COM * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <stdlib.h>
280Sstevel@tonic-gate #include <unistd.h>
290Sstevel@tonic-gate #include <stdarg.h>
300Sstevel@tonic-gate #include <string.h>
316206Sab196087 #include <strings.h>
320Sstevel@tonic-gate #include <errno.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate #include <libintl.h>
350Sstevel@tonic-gate #include <locale.h>
360Sstevel@tonic-gate #include <fcntl.h>
379646SAli.Bahrami@Sun.COM #include <ar.h>
389646SAli.Bahrami@Sun.COM #include <gelf.h>
390Sstevel@tonic-gate #include "conv.h"
400Sstevel@tonic-gate #include "libld.h"
416206Sab196087 #include "machdep.h"
420Sstevel@tonic-gate #include "msg.h"
430Sstevel@tonic-gate
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate * The following prevent us from having to include ctype.h which defines these
460Sstevel@tonic-gate * functions as macros which reference the __ctype[] array. Go through .plt's
470Sstevel@tonic-gate * to get to these functions in libc rather than have every invocation of ld
480Sstevel@tonic-gate * have to suffer the R_SPARC_COPY overhead of the __ctype[] array.
490Sstevel@tonic-gate */
500Sstevel@tonic-gate extern int isspace(int);
510Sstevel@tonic-gate
521618Srie /*
539646SAli.Bahrami@Sun.COM * We examine ELF objects, and archives containing ELF objects, in order
549646SAli.Bahrami@Sun.COM * to determine the ELFCLASS of the resulting object and/or the linker to be
559646SAli.Bahrami@Sun.COM * used. We want to avoid the overhead of libelf for this, at least until
569646SAli.Bahrami@Sun.COM * we are certain that we need it, so we start by reading bytes from
579646SAli.Bahrami@Sun.COM * the beginning of the file. This type defines the buffer used to read
589646SAli.Bahrami@Sun.COM * these initial bytes.
599646SAli.Bahrami@Sun.COM *
609646SAli.Bahrami@Sun.COM * A plain ELF object will start with an ELF header, whereas an archive
619646SAli.Bahrami@Sun.COM * starts with a magic string (ARMAG) that is SARMAG bytes long. Any valid
629646SAli.Bahrami@Sun.COM * ELF file or archive will contain more bytes than this buffer, so any
639646SAli.Bahrami@Sun.COM * file shorter than this can be safely assummed not to be of interest.
649646SAli.Bahrami@Sun.COM *
659646SAli.Bahrami@Sun.COM * The ELF header for ELFCLASS32 and ELFCLASS64 are identical up through the
669646SAli.Bahrami@Sun.COM * the e_version field, and all the information we require is found in this
679646SAli.Bahrami@Sun.COM * common prefix. Furthermore, this cannot change, as the layout of an ELF
689646SAli.Bahrami@Sun.COM * header is fixed by the ELF ABI. Hence, the ehdr part of this union is
699646SAli.Bahrami@Sun.COM * not a full ELF header, but only the class-independent prefix that we need.
709646SAli.Bahrami@Sun.COM *
719646SAli.Bahrami@Sun.COM * As this is a raw (non-libelf) read, we are responsible for handling any
729646SAli.Bahrami@Sun.COM * byte order difference between the object and the system running this
739646SAli.Bahrami@Sun.COM * program when we read any datum larger than a byte (i.e. e_machine) from
749646SAli.Bahrami@Sun.COM * this header.
759646SAli.Bahrami@Sun.COM */
769646SAli.Bahrami@Sun.COM typedef union {
779646SAli.Bahrami@Sun.COM struct { /* Must match start of ELFxx_Ehdr in <sys/elf.h> */
789646SAli.Bahrami@Sun.COM uchar_t e_ident[EI_NIDENT]; /* ident bytes */
799646SAli.Bahrami@Sun.COM Half e_type; /* file type */
809646SAli.Bahrami@Sun.COM Half e_machine; /* target machine */
819646SAli.Bahrami@Sun.COM } ehdr;
829646SAli.Bahrami@Sun.COM char armag[SARMAG];
839646SAli.Bahrami@Sun.COM } FILE_HDR;
849646SAli.Bahrami@Sun.COM
859646SAli.Bahrami@Sun.COM
869646SAli.Bahrami@Sun.COM /*
871618Srie * Print a message to stdout
881618Srie */
891618Srie void
veprintf(Lm_list * lml,Error error,const char * format,va_list args)90*13074SAli.Bahrami@Oracle.COM veprintf(Lm_list *lml, Error error, const char *format, va_list args)
911618Srie {
92*13074SAli.Bahrami@Oracle.COM static const char *strings[ERR_NUM];
931618Srie
941618Srie #if defined(lint)
951618Srie /*
961618Srie * The lml argument is only meaningful for diagnostics sent to ld.so.1.
971618Srie * Supress the lint error by making a dummy assignment.
981618Srie */
991618Srie lml = 0;
1001618Srie #endif
101*13074SAli.Bahrami@Oracle.COM /*
102*13074SAli.Bahrami@Oracle.COM * For error types we issue a prefix for, make sure the necessary
103*13074SAli.Bahrami@Oracle.COM * string has been internationalized and is ready.
104*13074SAli.Bahrami@Oracle.COM */
105*13074SAli.Bahrami@Oracle.COM switch (error) {
106*13074SAli.Bahrami@Oracle.COM case ERR_WARNING_NF:
107*13074SAli.Bahrami@Oracle.COM if (strings[ERR_WARNING_NF] == NULL)
108*13074SAli.Bahrami@Oracle.COM strings[ERR_WARNING_NF] = MSG_INTL(MSG_ERR_WARNING);
109*13074SAli.Bahrami@Oracle.COM break;
110*13074SAli.Bahrami@Oracle.COM case ERR_WARNING:
111*13074SAli.Bahrami@Oracle.COM if (strings[ERR_WARNING] == NULL)
112*13074SAli.Bahrami@Oracle.COM strings[ERR_WARNING] = MSG_INTL(MSG_ERR_WARNING);
113*13074SAli.Bahrami@Oracle.COM break;
114*13074SAli.Bahrami@Oracle.COM case ERR_GUIDANCE:
115*13074SAli.Bahrami@Oracle.COM if (strings[ERR_GUIDANCE] == NULL)
116*13074SAli.Bahrami@Oracle.COM strings[ERR_GUIDANCE] = MSG_INTL(MSG_ERR_GUIDANCE);
117*13074SAli.Bahrami@Oracle.COM break;
118*13074SAli.Bahrami@Oracle.COM case ERR_FATAL:
119*13074SAli.Bahrami@Oracle.COM if (strings[ERR_FATAL] == NULL)
120*13074SAli.Bahrami@Oracle.COM strings[ERR_FATAL] = MSG_INTL(MSG_ERR_FATAL);
121*13074SAli.Bahrami@Oracle.COM break;
122*13074SAli.Bahrami@Oracle.COM case ERR_ELF:
123*13074SAli.Bahrami@Oracle.COM if (strings[ERR_ELF] == NULL)
124*13074SAli.Bahrami@Oracle.COM strings[ERR_ELF] = MSG_INTL(MSG_ERR_ELF);
125*13074SAli.Bahrami@Oracle.COM }
126*13074SAli.Bahrami@Oracle.COM
127*13074SAli.Bahrami@Oracle.COM /* If strings[] element for our error type is non-NULL, issue prefix */
128*13074SAli.Bahrami@Oracle.COM if (strings[error] != NULL) {
1291618Srie (void) fputs(MSG_ORIG(MSG_STR_LDDIAG), stderr);
130*13074SAli.Bahrami@Oracle.COM (void) fputs(strings[error], stderr);
1311618Srie }
1321618Srie
1331618Srie (void) vfprintf(stderr, format, args);
1341618Srie if (error == ERR_ELF) {
1351618Srie int elferr;
1361618Srie
1371618Srie if ((elferr = elf_errno()) != 0)
1381618Srie (void) fprintf(stderr, MSG_ORIG(MSG_STR_ELFDIAG),
1391618Srie elf_errmsg(elferr));
1401618Srie }
1411618Srie (void) fprintf(stderr, MSG_ORIG(MSG_STR_NL));
1421618Srie (void) fflush(stderr);
143*13074SAli.Bahrami@Oracle.COM }
144*13074SAli.Bahrami@Oracle.COM
145*13074SAli.Bahrami@Oracle.COM
146*13074SAli.Bahrami@Oracle.COM /*
147*13074SAli.Bahrami@Oracle.COM * Print a message to stdout
148*13074SAli.Bahrami@Oracle.COM */
149*13074SAli.Bahrami@Oracle.COM /* VARARGS3 */
150*13074SAli.Bahrami@Oracle.COM void
eprintf(Lm_list * lml,Error error,const char * format,...)151*13074SAli.Bahrami@Oracle.COM eprintf(Lm_list *lml, Error error, const char *format, ...)
152*13074SAli.Bahrami@Oracle.COM {
153*13074SAli.Bahrami@Oracle.COM va_list args;
154*13074SAli.Bahrami@Oracle.COM
155*13074SAli.Bahrami@Oracle.COM va_start(args, format);
156*13074SAli.Bahrami@Oracle.COM veprintf(lml, error, format, args);
1571618Srie va_end(args);
1581618Srie }
1591618Srie
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate /*
1629646SAli.Bahrami@Sun.COM * Examine the first object in an archive to determine its ELFCLASS
1639646SAli.Bahrami@Sun.COM * and machine type.
1649646SAli.Bahrami@Sun.COM *
1659646SAli.Bahrami@Sun.COM * entry:
1669646SAli.Bahrami@Sun.COM * fd - Open file descriptor for file
1679646SAli.Bahrami@Sun.COM * elf - libelf ELF descriptor
1689646SAli.Bahrami@Sun.COM * class_ret, mach_ret - Address of variables to receive ELFCLASS
1699646SAli.Bahrami@Sun.COM * and machine type.
1709646SAli.Bahrami@Sun.COM *
1719646SAli.Bahrami@Sun.COM * exit:
1729646SAli.Bahrami@Sun.COM * On success, *class_ret and *mach_ret are filled in, and True (1)
1739646SAli.Bahrami@Sun.COM * is returned. On failure, False (0) is returned.
1740Sstevel@tonic-gate */
1750Sstevel@tonic-gate static int
archive(int fd,Elf * elf,uchar_t * class_ret,Half * mach_ret)1769646SAli.Bahrami@Sun.COM archive(int fd, Elf *elf, uchar_t *class_ret, Half *mach_ret)
1779646SAli.Bahrami@Sun.COM {
1789646SAli.Bahrami@Sun.COM Elf_Cmd cmd = ELF_C_READ;
1799646SAli.Bahrami@Sun.COM Elf_Arhdr *arhdr;
1809646SAli.Bahrami@Sun.COM Elf *_elf = NULL;
1819646SAli.Bahrami@Sun.COM int found = 0;
1829646SAli.Bahrami@Sun.COM
1839646SAli.Bahrami@Sun.COM /*
1849646SAli.Bahrami@Sun.COM * Process each item within the archive until we find the first
1859646SAli.Bahrami@Sun.COM * ELF object, or alternatively another archive to recurse into.
1869646SAli.Bahrami@Sun.COM * Stop after analyzing the first plain object found.
1879646SAli.Bahrami@Sun.COM */
1889646SAli.Bahrami@Sun.COM while (!found && ((_elf = elf_begin(fd, cmd, elf)) != NULL)) {
1899646SAli.Bahrami@Sun.COM if ((arhdr = elf_getarhdr(_elf)) == NULL)
1909646SAli.Bahrami@Sun.COM return (0);
1919646SAli.Bahrami@Sun.COM if (*arhdr->ar_name != '/') {
1929646SAli.Bahrami@Sun.COM switch (elf_kind(_elf)) {
1939646SAli.Bahrami@Sun.COM case ELF_K_AR:
1949646SAli.Bahrami@Sun.COM found = archive(fd, _elf, class_ret, mach_ret);
1959646SAli.Bahrami@Sun.COM break;
1969646SAli.Bahrami@Sun.COM case ELF_K_ELF:
1979646SAli.Bahrami@Sun.COM if (gelf_getclass(_elf) == ELFCLASS64) {
1989646SAli.Bahrami@Sun.COM Elf64_Ehdr *ehdr;
1999646SAli.Bahrami@Sun.COM
2009646SAli.Bahrami@Sun.COM if ((ehdr = elf64_getehdr(_elf)) ==
2019646SAli.Bahrami@Sun.COM NULL)
2029646SAli.Bahrami@Sun.COM break;
2039646SAli.Bahrami@Sun.COM *class_ret = ehdr->e_ident[EI_CLASS];
2049646SAli.Bahrami@Sun.COM *mach_ret = ehdr->e_machine;
2059646SAli.Bahrami@Sun.COM } else {
2069646SAli.Bahrami@Sun.COM Elf32_Ehdr *ehdr;
2079646SAli.Bahrami@Sun.COM
2089646SAli.Bahrami@Sun.COM if ((ehdr = elf32_getehdr(_elf)) ==
2099646SAli.Bahrami@Sun.COM NULL)
2109646SAli.Bahrami@Sun.COM break;
2119646SAli.Bahrami@Sun.COM *class_ret = ehdr->e_ident[EI_CLASS];
2129646SAli.Bahrami@Sun.COM *mach_ret = ehdr->e_machine;
2139646SAli.Bahrami@Sun.COM }
2149646SAli.Bahrami@Sun.COM found = 1;
2159646SAli.Bahrami@Sun.COM break;
2169646SAli.Bahrami@Sun.COM }
2179646SAli.Bahrami@Sun.COM }
2189646SAli.Bahrami@Sun.COM
2199646SAli.Bahrami@Sun.COM cmd = elf_next(_elf);
2209646SAli.Bahrami@Sun.COM (void) elf_end(_elf);
2219646SAli.Bahrami@Sun.COM }
2229646SAli.Bahrami@Sun.COM
2239646SAli.Bahrami@Sun.COM return (found);
2249646SAli.Bahrami@Sun.COM }
2259646SAli.Bahrami@Sun.COM
2269646SAli.Bahrami@Sun.COM /*
2279646SAli.Bahrami@Sun.COM * Determine:
2289646SAli.Bahrami@Sun.COM * - ELFCLASS of resulting object (class)
2299646SAli.Bahrami@Sun.COM * - Whether user specified class of the linker (ldclass)
2309646SAli.Bahrami@Sun.COM * - ELF machine type of resulting object (m_mach)
2319646SAli.Bahrami@Sun.COM *
2329646SAli.Bahrami@Sun.COM * In order of priority, we determine this information as follows:
2339646SAli.Bahrami@Sun.COM *
2349646SAli.Bahrami@Sun.COM * - Command line options (-32, -64, -z altexec64, -z target).
2359646SAli.Bahrami@Sun.COM * - From the first plain object seen on the command line. (This is
2369646SAli.Bahrami@Sun.COM * by far the most common case.)
2379646SAli.Bahrami@Sun.COM * - From the first object contained within the first archive
2389646SAli.Bahrami@Sun.COM * on the command line.
2399646SAli.Bahrami@Sun.COM * - If all else fails, we assume a 32-bit object for the native machine.
2409646SAli.Bahrami@Sun.COM *
2419646SAli.Bahrami@Sun.COM * entry:
2429646SAli.Bahrami@Sun.COM * argc, argv - Command line argument vector
2439646SAli.Bahrami@Sun.COM * class_ret - Address of variable to receive ELFCLASS of output object
2449646SAli.Bahrami@Sun.COM * ldclass_ret - Address of variable to receive ELFCLASS of
2459646SAli.Bahrami@Sun.COM * linker to use. This will be ELFCLASS32/ELFCLASS64 if one
2469646SAli.Bahrami@Sun.COM * is explicitly specified, and ELFCLASSNONE otherwise.
2479646SAli.Bahrami@Sun.COM * ELFCLASSNONE therefore means that we should use the best
2489646SAli.Bahrami@Sun.COM * link-editor that the system/kernel will allow.
2499646SAli.Bahrami@Sun.COM */
2509646SAli.Bahrami@Sun.COM static int
process_args(int argc,char ** argv,uchar_t * class_ret,uchar_t * ldclass_ret,Half * mach)2519646SAli.Bahrami@Sun.COM process_args(int argc, char **argv, uchar_t *class_ret, uchar_t *ldclass_ret,
2526206Sab196087 Half *mach)
2530Sstevel@tonic-gate {
2549646SAli.Bahrami@Sun.COM uchar_t ldclass = ELFCLASSNONE, class = ELFCLASSNONE, ar_class;
2559646SAli.Bahrami@Sun.COM Half mach32 = EM_NONE, mach64 = EM_NONE, ar_mach;
2569646SAli.Bahrami@Sun.COM int c, ar_found = 0;
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate /*
2596206Sab196087 * In general, libld.so is responsible for processing the
2606206Sab196087 * command line options. The exception to this are those options
2616206Sab196087 * that contain information about which linker to run and the
2626206Sab196087 * class/machine of the output object. We examine the options
2636206Sab196087 * here looking for the following:
2646206Sab196087 *
2659646SAli.Bahrami@Sun.COM * -32 Produce an ELFCLASS32 object. This is the default, so
2669646SAli.Bahrami@Sun.COM * -32 is only needed when linking entirely from archives,
2679646SAli.Bahrami@Sun.COM * and the first archive contains a mix of 32 and 64-bit
2689646SAli.Bahrami@Sun.COM * objects, and the first object in that archive is 64-bit.
2699646SAli.Bahrami@Sun.COM * We do not expect this option to get much use, but it
2709646SAli.Bahrami@Sun.COM * ensures that the user can handle any situation.
2719646SAli.Bahrami@Sun.COM *
2729646SAli.Bahrami@Sun.COM * -64 Produce an ELFCLASS64 object. (Note that this will
2739646SAli.Bahrami@Sun.COM * indirectly cause the use of the 64-bit linker if
2749646SAli.Bahrami@Sun.COM * the system is 64-bit capable). The most common need
2759646SAli.Bahrami@Sun.COM * for this option is when linking a filter object entirely
2769646SAli.Bahrami@Sun.COM * from a mapfile. The less common case is when linking
2779646SAli.Bahrami@Sun.COM * entirely from archives, and the first archive contains
2789646SAli.Bahrami@Sun.COM * a mix of 32 and 64-bit objects, and the first object
2799646SAli.Bahrami@Sun.COM * in that archive is 32-bit.
2800Sstevel@tonic-gate *
2817636SRod.Evans@Sun.COM * -z altexec64
2826206Sab196087 * Use the 64-bit linker regardless of the class
2836206Sab196087 * of the output object.
2846206Sab196087 *
2856206Sab196087 * -z target=platform
2866206Sab196087 * Produce output object for the specified platform.
2879646SAli.Bahrami@Sun.COM * This option is needed when producing an object
2889646SAli.Bahrami@Sun.COM * for a non-native target entirely from a mapfile,
2899646SAli.Bahrami@Sun.COM * or when linking entirely from an archive containing
2909646SAli.Bahrami@Sun.COM * objects for multiple targets, and the first object
2919646SAli.Bahrami@Sun.COM * in the archive is not for the desired target.
2926206Sab196087 *
2939646SAli.Bahrami@Sun.COM * If we've already processed an object and we find -32/-64, and
2949646SAli.Bahrami@Sun.COM * the object is of the wrong class, we have an error condition.
2959646SAli.Bahrami@Sun.COM * We ignore it here, and let it fall through to libld, where the
2969646SAli.Bahrami@Sun.COM * proper diagnosis and error message will occur.
2970Sstevel@tonic-gate */
2980Sstevel@tonic-gate opterr = 0;
2997636SRod.Evans@Sun.COM optind = 1;
3007636SRod.Evans@Sun.COM getmore:
3017636SRod.Evans@Sun.COM while ((c = ld_getopt(0, optind, argc, argv)) != -1) {
3020Sstevel@tonic-gate switch (c) {
3039646SAli.Bahrami@Sun.COM case '3':
3049646SAli.Bahrami@Sun.COM if (strncmp(optarg, MSG_ORIG(MSG_ARG_TWO),
3059646SAli.Bahrami@Sun.COM MSG_ARG_TWO_SIZE) == 0)
3069646SAli.Bahrami@Sun.COM class = ELFCLASS32;
3079646SAli.Bahrami@Sun.COM break;
3089646SAli.Bahrami@Sun.COM
3097636SRod.Evans@Sun.COM case '6':
3107636SRod.Evans@Sun.COM if (strncmp(optarg, MSG_ORIG(MSG_ARG_FOUR),
3117636SRod.Evans@Sun.COM MSG_ARG_FOUR_SIZE) == 0)
3129646SAli.Bahrami@Sun.COM class = ELFCLASS64;
3137636SRod.Evans@Sun.COM break;
3146206Sab196087
3157636SRod.Evans@Sun.COM case 'z':
3167636SRod.Evans@Sun.COM #if !defined(_LP64)
3177636SRod.Evans@Sun.COM /* -z altexec64 */
3187636SRod.Evans@Sun.COM if (strncmp(optarg, MSG_ORIG(MSG_ARG_ALTEXEC64),
3197636SRod.Evans@Sun.COM MSG_ARG_ALTEXEC64_SIZE) == 0) {
3209646SAli.Bahrami@Sun.COM ldclass = ELFCLASS64;
3217636SRod.Evans@Sun.COM break;
3227636SRod.Evans@Sun.COM }
3237636SRod.Evans@Sun.COM #endif
3247636SRod.Evans@Sun.COM /* -z target=platform */
3257636SRod.Evans@Sun.COM if (strncmp(optarg, MSG_ORIG(MSG_ARG_TARGET),
3267636SRod.Evans@Sun.COM MSG_ARG_TARGET_SIZE) == 0) {
3277636SRod.Evans@Sun.COM char *pstr = optarg + MSG_ARG_TARGET_SIZE;
3286206Sab196087
3297636SRod.Evans@Sun.COM if (strcasecmp(pstr,
3307636SRod.Evans@Sun.COM MSG_ORIG(MSG_TARG_SPARC)) == 0) {
3317636SRod.Evans@Sun.COM mach32 = EM_SPARC;
3327636SRod.Evans@Sun.COM mach64 = EM_SPARCV9;
3337636SRod.Evans@Sun.COM } else if (strcasecmp(pstr,
3347636SRod.Evans@Sun.COM MSG_ORIG(MSG_TARG_X86)) == 0) {
3357636SRod.Evans@Sun.COM mach32 = EM_386;
3367636SRod.Evans@Sun.COM mach64 = EM_AMD64;
3377636SRod.Evans@Sun.COM } else {
3387636SRod.Evans@Sun.COM eprintf(0, ERR_FATAL,
3397636SRod.Evans@Sun.COM MSG_INTL(MSG_ERR_BADTARG), pstr);
3407636SRod.Evans@Sun.COM return (1);
3416206Sab196087 }
3427636SRod.Evans@Sun.COM }
3437636SRod.Evans@Sun.COM break;
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate /*
3482647Srie * Continue to look for the first ELF object to determine the class of
3499646SAli.Bahrami@Sun.COM * objects to operate on. At the same time, look for the first archive
3509646SAli.Bahrami@Sun.COM * of ELF objects --- if no plain ELF object is specified, the type
3519646SAli.Bahrami@Sun.COM * of the first ELF object in the first archive will be used. If
3529646SAli.Bahrami@Sun.COM * there is no object, and no archive, then we fall back to a 32-bit
3539646SAli.Bahrami@Sun.COM * object for the native machine.
3540Sstevel@tonic-gate */
3550Sstevel@tonic-gate for (; optind < argc; optind++) {
3560Sstevel@tonic-gate int fd;
3579646SAli.Bahrami@Sun.COM FILE_HDR hdr;
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate /*
3600Sstevel@tonic-gate * If we detect some more options return to getopt().
3610Sstevel@tonic-gate * Checking argv[optind][1] against null prevents a forever
3620Sstevel@tonic-gate * loop if an unadorned `-' argument is passed to us.
3630Sstevel@tonic-gate */
3640Sstevel@tonic-gate if (argv[optind][0] == '-') {
3650Sstevel@tonic-gate if (argv[optind][1] == '\0')
3660Sstevel@tonic-gate continue;
3670Sstevel@tonic-gate else
3680Sstevel@tonic-gate goto getmore;
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate
3712647Srie /*
3726206Sab196087 * If we've already determined the object class and
3736206Sab196087 * machine type, continue to the next argument. Only
3746206Sab196087 * the first object contributes to this decision, and
3756206Sab196087 * there's no value to opening or examing the subsequent
3766206Sab196087 * ones. We do need to keep going though, because there
3776206Sab196087 * may be additional options that might affect our
3786206Sab196087 * class/machine decision.
3792647Srie */
3809646SAli.Bahrami@Sun.COM if ((class != ELFCLASSNONE) && (mach32 != EM_NONE))
3812647Srie continue;
3822647Srie
3832647Srie /*
3849646SAli.Bahrami@Sun.COM * Open the file and determine if it is an object. We are
3859646SAli.Bahrami@Sun.COM * looking for ELF objects, or archives of ELF objects.
3869646SAli.Bahrami@Sun.COM *
3879646SAli.Bahrami@Sun.COM * Plain objects are simple, and are the common case, so
3889646SAli.Bahrami@Sun.COM * we examine them directly and avoid the map-unmap-map
3899646SAli.Bahrami@Sun.COM * that would occur if we used libelf. Archives are too
3909646SAli.Bahrami@Sun.COM * complex to be worth accessing directly, so if we identify
3919646SAli.Bahrami@Sun.COM * an archive, we use libelf on it and accept the cost.
3922647Srie */
3930Sstevel@tonic-gate if ((fd = open(argv[optind], O_RDONLY)) == -1) {
3940Sstevel@tonic-gate int err = errno;
3950Sstevel@tonic-gate
3961618Srie eprintf(0, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
3970Sstevel@tonic-gate argv[optind], strerror(err));
3982647Srie return (1);
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate
4019646SAli.Bahrami@Sun.COM if (pread(fd, &hdr, sizeof (hdr), 0) != sizeof (hdr)) {
4029646SAli.Bahrami@Sun.COM (void) close(fd);
4039646SAli.Bahrami@Sun.COM continue;
4049646SAli.Bahrami@Sun.COM }
4059646SAli.Bahrami@Sun.COM
4069646SAli.Bahrami@Sun.COM if ((hdr.ehdr.e_ident[EI_MAG0] == ELFMAG0) &&
4079646SAli.Bahrami@Sun.COM (hdr.ehdr.e_ident[EI_MAG1] == ELFMAG1) &&
4089646SAli.Bahrami@Sun.COM (hdr.ehdr.e_ident[EI_MAG2] == ELFMAG2) &&
4099646SAli.Bahrami@Sun.COM (hdr.ehdr.e_ident[EI_MAG3] == ELFMAG3)) {
4109646SAli.Bahrami@Sun.COM if (class == ELFCLASSNONE) {
4119646SAli.Bahrami@Sun.COM class = hdr.ehdr.e_ident[EI_CLASS];
4129646SAli.Bahrami@Sun.COM if ((class != ELFCLASS32) &&
4139646SAli.Bahrami@Sun.COM (class != ELFCLASS64))
4149646SAli.Bahrami@Sun.COM class = ELFCLASSNONE;
4156206Sab196087 }
4166206Sab196087
4176206Sab196087 if (mach32 == EM_NONE) {
4186206Sab196087 int one = 1;
4196206Sab196087 uchar_t *one_p = (uchar_t *)&one;
4206206Sab196087 int ld_elfdata;
4216206Sab196087
4226206Sab196087 ld_elfdata = (one_p[0] == 1) ?
4236206Sab196087 ELFDATA2LSB : ELFDATA2MSB;
4246206Sab196087 /*
4256206Sab196087 * Both the 32 and 64-bit versions get the
4266206Sab196087 * type from the object. If the user has
4276206Sab196087 * asked for an inconsistant class/machine
4286206Sab196087 * combination, libld will catch it.
4296206Sab196087 */
4306206Sab196087 mach32 = mach64 =
4319646SAli.Bahrami@Sun.COM (ld_elfdata == hdr.ehdr.e_ident[EI_DATA]) ?
4329646SAli.Bahrami@Sun.COM hdr.ehdr.e_machine :
4339646SAli.Bahrami@Sun.COM BSWAP_HALF(hdr.ehdr.e_machine);
4346206Sab196087 }
4359646SAli.Bahrami@Sun.COM } else if (!ar_found &&
4369646SAli.Bahrami@Sun.COM (memcmp(&hdr.armag, ARMAG, SARMAG) == 0)) {
4379646SAli.Bahrami@Sun.COM Elf *elf;
4389646SAli.Bahrami@Sun.COM
4399646SAli.Bahrami@Sun.COM (void) elf_version(EV_CURRENT);
4409646SAli.Bahrami@Sun.COM if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
4419646SAli.Bahrami@Sun.COM (void) close(fd);
4429646SAli.Bahrami@Sun.COM continue;
4439646SAli.Bahrami@Sun.COM }
4449646SAli.Bahrami@Sun.COM if (elf_kind(elf) == ELF_K_AR)
4459646SAli.Bahrami@Sun.COM ar_found =
4469646SAli.Bahrami@Sun.COM archive(fd, elf, &ar_class, &ar_mach);
4479646SAli.Bahrami@Sun.COM (void) elf_end(elf);
4480Sstevel@tonic-gate }
4496206Sab196087
4500Sstevel@tonic-gate (void) close(fd);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate /*
4549646SAli.Bahrami@Sun.COM * ELFCLASS of output object: If we did not establish a class from a
4559646SAli.Bahrami@Sun.COM * command option, or from the first plain object, then use the class
4569646SAli.Bahrami@Sun.COM * from the first archive, and failing that, default to 32-bit.
4570Sstevel@tonic-gate */
4589646SAli.Bahrami@Sun.COM if (class == ELFCLASSNONE)
4599646SAli.Bahrami@Sun.COM class = ar_found ? ar_class : ELFCLASS32;
4609646SAli.Bahrami@Sun.COM *class_ret = class;
4616206Sab196087
4629646SAli.Bahrami@Sun.COM /* ELFCLASS of link-editor to use */
4639646SAli.Bahrami@Sun.COM *ldclass_ret = ldclass;
4640Sstevel@tonic-gate
4656206Sab196087 /*
4669646SAli.Bahrami@Sun.COM * Machine type of output object: If we did not establish a machine
4679646SAli.Bahrami@Sun.COM * type from the command line, or from the first plain object, then
4689646SAli.Bahrami@Sun.COM * use the machine established by the first archive, and failing that,
4699646SAli.Bahrami@Sun.COM * use the native machine.
4706206Sab196087 */
4719646SAli.Bahrami@Sun.COM *mach = (class == ELFCLASS64) ? mach64 : mach32;
4726206Sab196087 if (*mach == EM_NONE)
4739646SAli.Bahrami@Sun.COM if (ar_found)
4749646SAli.Bahrami@Sun.COM *mach = ar_mach;
4759646SAli.Bahrami@Sun.COM else
4769646SAli.Bahrami@Sun.COM *mach = (class == ELFCLASS64) ? M_MACH_64 : M_MACH_32;
4776206Sab196087
4782647Srie return (0);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate /*
4827636SRod.Evans@Sun.COM * Process an LD_OPTIONS environment string. This routine is first called to
4837636SRod.Evans@Sun.COM * count the number of options, and second to initialize a new argument array
4847636SRod.Evans@Sun.COM * with each option.
4850Sstevel@tonic-gate */
4860Sstevel@tonic-gate static int
process_ldoptions(char * str,char ** nargv)4877636SRod.Evans@Sun.COM process_ldoptions(char *str, char **nargv)
4880Sstevel@tonic-gate {
4897636SRod.Evans@Sun.COM int argc = 0;
4907636SRod.Evans@Sun.COM char *arg = str;
4910Sstevel@tonic-gate
4920Sstevel@tonic-gate /*
4937636SRod.Evans@Sun.COM * Walk the environment string processing any arguments that are
4947636SRod.Evans@Sun.COM * separated by white space.
4957636SRod.Evans@Sun.COM */
4967636SRod.Evans@Sun.COM while (*str != '\0') {
4977636SRod.Evans@Sun.COM if (isspace(*str)) {
4987636SRod.Evans@Sun.COM /*
4997636SRod.Evans@Sun.COM * If a new argument array has been provided, terminate
5007636SRod.Evans@Sun.COM * the original environment string, and initialize the
5017636SRod.Evans@Sun.COM * appropriate argument array entry.
5027636SRod.Evans@Sun.COM */
5037636SRod.Evans@Sun.COM if (nargv) {
5047636SRod.Evans@Sun.COM *str++ = '\0';
5057636SRod.Evans@Sun.COM nargv[argc] = arg;
5067636SRod.Evans@Sun.COM }
5077636SRod.Evans@Sun.COM
5087636SRod.Evans@Sun.COM argc++;
5097636SRod.Evans@Sun.COM while (isspace(*str))
5107636SRod.Evans@Sun.COM str++;
5117636SRod.Evans@Sun.COM arg = str;
5127636SRod.Evans@Sun.COM } else
5137636SRod.Evans@Sun.COM str++;
5147636SRod.Evans@Sun.COM }
5157636SRod.Evans@Sun.COM if (arg != str) {
5167636SRod.Evans@Sun.COM /*
5177636SRod.Evans@Sun.COM * If a new argument array has been provided, initialize the
5187636SRod.Evans@Sun.COM * final argument array entry.
5197636SRod.Evans@Sun.COM */
5207636SRod.Evans@Sun.COM if (nargv)
5217636SRod.Evans@Sun.COM nargv[argc] = arg;
5227636SRod.Evans@Sun.COM argc++;
5237636SRod.Evans@Sun.COM }
5247636SRod.Evans@Sun.COM
5257636SRod.Evans@Sun.COM return (argc);
5267636SRod.Evans@Sun.COM }
5277636SRod.Evans@Sun.COM
5287636SRod.Evans@Sun.COM /*
5297636SRod.Evans@Sun.COM * Determine whether an LD_OPTIONS environment variable is set, and if so,
5307636SRod.Evans@Sun.COM * prepend environment string as a series of options to the argv array.
5317636SRod.Evans@Sun.COM */
5327636SRod.Evans@Sun.COM static int
prepend_ldoptions(int * argcp,char *** argvp)5337636SRod.Evans@Sun.COM prepend_ldoptions(int *argcp, char ***argvp)
5347636SRod.Evans@Sun.COM {
5357636SRod.Evans@Sun.COM int nargc;
5367636SRod.Evans@Sun.COM char **nargv, *ld_options;
5377636SRod.Evans@Sun.COM int err, count;
5387636SRod.Evans@Sun.COM
5397636SRod.Evans@Sun.COM if ((ld_options = getenv(MSG_ORIG(MSG_LD_OPTIONS))) == NULL)
5407636SRod.Evans@Sun.COM return (0);
5417636SRod.Evans@Sun.COM
5427636SRod.Evans@Sun.COM /*
54310792SRod.Evans@Sun.COM * Get rid of any leading white space, and make sure the environment
54410792SRod.Evans@Sun.COM * string has size.
54510792SRod.Evans@Sun.COM */
54610792SRod.Evans@Sun.COM while (isspace(*ld_options))
54710792SRod.Evans@Sun.COM ld_options++;
54812029SRod.Evans@Sun.COM if (ld_options[0] == '\0')
54910792SRod.Evans@Sun.COM return (0);
55010792SRod.Evans@Sun.COM
55110792SRod.Evans@Sun.COM /*
5527636SRod.Evans@Sun.COM * Prevent modification of actual environment strings.
5537636SRod.Evans@Sun.COM */
5547636SRod.Evans@Sun.COM if ((ld_options = strdup(ld_options)) == NULL) {
5557636SRod.Evans@Sun.COM err = errno;
5567636SRod.Evans@Sun.COM eprintf(0, ERR_FATAL, MSG_INTL(MSG_SYS_ALLOC), strerror(err));
5577636SRod.Evans@Sun.COM return (1);
5587636SRod.Evans@Sun.COM }
5597636SRod.Evans@Sun.COM
5607636SRod.Evans@Sun.COM /*
5617636SRod.Evans@Sun.COM * Determine the number of options provided.
5620Sstevel@tonic-gate */
5637636SRod.Evans@Sun.COM nargc = process_ldoptions(ld_options, NULL);
5640Sstevel@tonic-gate
5650Sstevel@tonic-gate /*
5660Sstevel@tonic-gate * Allocate a new argv array big enough to hold the new options from
5670Sstevel@tonic-gate * the environment string and the old argv options.
5680Sstevel@tonic-gate */
5697636SRod.Evans@Sun.COM if ((nargv = malloc((nargc + *argcp + 1) * sizeof (char *))) == NULL) {
5707636SRod.Evans@Sun.COM err = errno;
5711618Srie eprintf(0, ERR_FATAL, MSG_INTL(MSG_SYS_ALLOC), strerror(err));
5727636SRod.Evans@Sun.COM return (1);
5731618Srie }
5740Sstevel@tonic-gate
5750Sstevel@tonic-gate /*
5760Sstevel@tonic-gate * Initialize first element of new argv array to be the first element
5770Sstevel@tonic-gate * of the old argv array (ie. calling programs name). Then add the new
5780Sstevel@tonic-gate * args obtained from the environment.
5790Sstevel@tonic-gate */
5800Sstevel@tonic-gate nargc = 0;
5817636SRod.Evans@Sun.COM nargv[nargc++] = (*argvp)[0];
5827636SRod.Evans@Sun.COM nargc += process_ldoptions(ld_options, &nargv[nargc]);
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate /*
5850Sstevel@tonic-gate * Now add the original argv array (skipping argv[0]) to the end of the
5867636SRod.Evans@Sun.COM * new argv array, and re-vector argc and argv to reference this new
5877636SRod.Evans@Sun.COM * array
5880Sstevel@tonic-gate */
5897636SRod.Evans@Sun.COM for (count = 1; count < *argcp; count++, nargc++)
5900Sstevel@tonic-gate nargv[nargc] = (*argvp)[count];
5917636SRod.Evans@Sun.COM
5927636SRod.Evans@Sun.COM nargv[nargc] = NULL;
5937636SRod.Evans@Sun.COM
5947636SRod.Evans@Sun.COM *argcp = nargc;
5950Sstevel@tonic-gate *argvp = nargv;
5960Sstevel@tonic-gate
5977636SRod.Evans@Sun.COM return (0);
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate
6000Sstevel@tonic-gate /*
6011618Srie * Check to see if there is a LD_ALTEXEC=<path to alternate ld> in the
6021618Srie * environment. If so, first null the environment variable out, and then
6031618Srie * exec() the binary pointed to by the environment variable, passing the same
6041618Srie * arguments as the originating process. This mechanism permits using
6051618Srie * alternate link-editors (debugging/developer copies) even in complex build
6061618Srie * environments.
6070Sstevel@tonic-gate */
6082647Srie static int
ld_altexec(char ** argv,char ** envp)6090Sstevel@tonic-gate ld_altexec(char **argv, char **envp)
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate char *execstr;
6120Sstevel@tonic-gate char **str;
6132647Srie int err;
6142647Srie
6150Sstevel@tonic-gate for (str = envp; *str; str++) {
6160Sstevel@tonic-gate if (strncmp(*str, MSG_ORIG(MSG_LD_ALTEXEC),
6170Sstevel@tonic-gate MSG_LD_ALTEXEC_SIZE) == 0) {
6180Sstevel@tonic-gate break;
6190Sstevel@tonic-gate }
6200Sstevel@tonic-gate }
6210Sstevel@tonic-gate
6220Sstevel@tonic-gate /*
6232647Srie * If LD_ALTEXEC isn't set, return to continue executing the present
6242647Srie * link-editor.
6252647Srie */
6262647Srie if (*str == 0)
6272647Srie return (0);
6282647Srie
6292647Srie /*
6302647Srie * Get a pointer to the actual string. If it's a null entry, return.
6310Sstevel@tonic-gate */
6320Sstevel@tonic-gate execstr = strdup(*str + MSG_LD_ALTEXEC_SIZE);
6330Sstevel@tonic-gate if (*execstr == '\0')
6342647Srie return (0);
6352647Srie
6360Sstevel@tonic-gate /*
6370Sstevel@tonic-gate * Null out the LD_ALTEXEC= environment entry.
6380Sstevel@tonic-gate */
6390Sstevel@tonic-gate (*str)[MSG_LD_ALTEXEC_SIZE] = '\0';
6400Sstevel@tonic-gate
6410Sstevel@tonic-gate /*
6420Sstevel@tonic-gate * Set argv[0] to point to our new linker
6430Sstevel@tonic-gate */
6440Sstevel@tonic-gate argv[0] = execstr;
6450Sstevel@tonic-gate
6460Sstevel@tonic-gate /*
6470Sstevel@tonic-gate * And attempt to execute it.
6480Sstevel@tonic-gate */
6490Sstevel@tonic-gate (void) execve(execstr, argv, envp);
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate /*
6522647Srie * If the exec() fails, return a failure indication.
6530Sstevel@tonic-gate */
6542647Srie err = errno;
6552647Srie eprintf(0, ERR_FATAL, MSG_INTL(MSG_SYS_EXEC), execstr,
6562647Srie strerror(err));
6572647Srie return (1);
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate int
main(int argc,char ** argv,char ** envp)6610Sstevel@tonic-gate main(int argc, char **argv, char **envp)
6620Sstevel@tonic-gate {
6637636SRod.Evans@Sun.COM char **oargv = argv;
6649646SAli.Bahrami@Sun.COM uchar_t class, ldclass, checkclass;
6656206Sab196087 Half mach;
6660Sstevel@tonic-gate
6670Sstevel@tonic-gate /*
6680Sstevel@tonic-gate * Establish locale.
6690Sstevel@tonic-gate */
6700Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY));
6710Sstevel@tonic-gate (void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS));
6720Sstevel@tonic-gate
6730Sstevel@tonic-gate /*
6742647Srie * Execute an alternate linker if the LD_ALTEXEC environment variable is
6752647Srie * set. If a specified alternative could not be found, bail.
6760Sstevel@tonic-gate */
6772647Srie if (ld_altexec(argv, envp))
6782647Srie return (1);
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate /*
6810Sstevel@tonic-gate * Check the LD_OPTIONS environment variable, and if present prepend
6820Sstevel@tonic-gate * the arguments specified to the command line argument list.
6830Sstevel@tonic-gate */
6847636SRod.Evans@Sun.COM if (prepend_ldoptions(&argc, &argv))
6857636SRod.Evans@Sun.COM return (1);
6860Sstevel@tonic-gate
6870Sstevel@tonic-gate /*
6886206Sab196087 * Examine the command arguments to determine:
6896206Sab196087 * - object class
6906206Sab196087 * - link-editor class
6916206Sab196087 * - target machine
6920Sstevel@tonic-gate */
6939646SAli.Bahrami@Sun.COM if (process_args(argc, argv, &class, &ldclass, &mach))
6940Sstevel@tonic-gate return (1);
6950Sstevel@tonic-gate
6960Sstevel@tonic-gate /*
6979646SAli.Bahrami@Sun.COM * Unless a 32-bit link-editor was explicitly requested, try
6989646SAli.Bahrami@Sun.COM * to exec the 64-bit version.
6990Sstevel@tonic-gate */
7009646SAli.Bahrami@Sun.COM if (ldclass != ELFCLASS32)
7012647Srie checkclass = conv_check_native(oargv, envp);
7022647Srie
7039646SAli.Bahrami@Sun.COM /*
7049646SAli.Bahrami@Sun.COM * If an attempt to exec the 64-bit link-editor fails:
7059646SAli.Bahrami@Sun.COM * - Bail if the 64-bit linker was explicitly requested
7069646SAli.Bahrami@Sun.COM * - Continue quietly if the 64-bit linker was not requested.
7079646SAli.Bahrami@Sun.COM * This is undoubtedly due to hardware/kernel limitations,
7089646SAli.Bahrami@Sun.COM * and therefore represents the best we can do. Note that
7099646SAli.Bahrami@Sun.COM * the 32-bit linker is capable of linking anything the
7109646SAli.Bahrami@Sun.COM * 64-bit version is, subject to a 4GB limit on memory, and
7119646SAli.Bahrami@Sun.COM * 2GB object size.
7129646SAli.Bahrami@Sun.COM */
7132647Srie if ((ldclass == ELFCLASS64) && (checkclass != ELFCLASS64)) {
7142647Srie eprintf(0, ERR_FATAL, MSG_INTL(MSG_SYS_64));
7152647Srie return (1);
7162647Srie }
7170Sstevel@tonic-gate
7189646SAli.Bahrami@Sun.COM /* Call the libld entry point for the specified ELFCLASS */
7199646SAli.Bahrami@Sun.COM if (class == ELFCLASS64)
7206206Sab196087 return (ld64_main(argc, argv, mach));
7211618Srie else
7226206Sab196087 return (ld32_main(argc, argv, mach));
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate /*
7269646SAli.Bahrami@Sun.COM * We supply this function for the msg module
7270Sstevel@tonic-gate */
7280Sstevel@tonic-gate const char *
_ld_msg(Msg mid)7290Sstevel@tonic-gate _ld_msg(Msg mid)
7300Sstevel@tonic-gate {
7310Sstevel@tonic-gate return (gettext(MSG_ORIG(mid)));
7320Sstevel@tonic-gate }
733