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.
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.
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]
23 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
45 * The following prevent us from having to include ctype.h which defines these
46 * functions as macros which reference the __ctype[] array. Go through .plt's
47 * to get to these functions in libc rather than have every invocation of ld
48 * have to suffer the R_SPARC_COPY overhead of the __ctype[] array.
50 extern int isspace(int);
53 * We examine ELF objects, and archives containing ELF objects, in order
54 * to determine the ELFCLASS of the resulting object and/or the linker to be
55 * used. We want to avoid the overhead of libelf for this, at least until
56 * we are certain that we need it, so we start by reading bytes from
57 * the beginning of the file. This type defines the buffer used to read
58 * these initial bytes.
60 * A plain ELF object will start with an ELF header, whereas an archive
61 * starts with a magic string (ARMAG) that is SARMAG bytes long. Any valid
62 * ELF file or archive will contain more bytes than this buffer, so any
63 * file shorter than this can be safely assummed not to be of interest.
65 * The ELF header for ELFCLASS32 and ELFCLASS64 are identical up through the
66 * the e_version field, and all the information we require is found in this
67 * common prefix. Furthermore, this cannot change, as the layout of an ELF
68 * header is fixed by the ELF ABI. Hence, the ehdr part of this union is
69 * not a full ELF header, but only the class-independent prefix that we need.
71 * As this is a raw (non-libelf) read, we are responsible for handling any
72 * byte order difference between the object and the system running this
73 * program when we read any datum larger than a byte (i.e. e_machine) from
77 struct { /* Must match start of ELFxx_Ehdr in <sys/elf.h> */
78 uchar_t e_ident
[EI_NIDENT
]; /* ident bytes */
79 Half e_type
; /* file type */
80 Half e_machine
; /* target machine */
87 * Print a message to stdout
90 veprintf(Lm_list
*lml
, Error error
, const char *format
, va_list args
)
92 static const char *strings
[ERR_NUM
];
95 * For error types we issue a prefix for, make sure the necessary
96 * string has been internationalized and is ready.
100 if (strings
[ERR_WARNING_NF
] == NULL
)
101 strings
[ERR_WARNING_NF
] = MSG_INTL(MSG_ERR_WARNING
);
104 if (strings
[ERR_WARNING
] == NULL
)
105 strings
[ERR_WARNING
] = MSG_INTL(MSG_ERR_WARNING
);
108 if (strings
[ERR_GUIDANCE
] == NULL
)
109 strings
[ERR_GUIDANCE
] = MSG_INTL(MSG_ERR_GUIDANCE
);
112 if (strings
[ERR_FATAL
] == NULL
)
113 strings
[ERR_FATAL
] = MSG_INTL(MSG_ERR_FATAL
);
116 if (strings
[ERR_ELF
] == NULL
)
117 strings
[ERR_ELF
] = MSG_INTL(MSG_ERR_ELF
);
120 /* If strings[] element for our error type is non-NULL, issue prefix */
121 if (strings
[error
] != NULL
) {
122 (void) fputs(MSG_ORIG(MSG_STR_LDDIAG
), stderr
);
123 (void) fputs(strings
[error
], stderr
);
126 (void) vfprintf(stderr
, format
, args
);
127 if (error
== ERR_ELF
) {
130 if ((elferr
= elf_errno()) != 0)
131 (void) fprintf(stderr
, MSG_ORIG(MSG_STR_ELFDIAG
),
134 (void) fprintf(stderr
, MSG_ORIG(MSG_STR_NL
));
135 (void) fflush(stderr
);
140 * Print a message to stdout
144 eprintf(Lm_list
*lml
, Error error
, const char *format
, ...)
148 va_start(args
, format
);
149 veprintf(lml
, error
, format
, args
);
155 * Examine the first object in an archive to determine its ELFCLASS
159 * fd - Open file descriptor for file
160 * elf - libelf ELF descriptor
161 * class_ret, mach_ret - Address of variables to receive ELFCLASS
165 * On success, *class_ret and *mach_ret are filled in, and True (1)
166 * is returned. On failure, False (0) is returned.
169 archive(int fd
, Elf
*elf
, uchar_t
*class_ret
, Half
*mach_ret
)
171 Elf_Cmd cmd
= ELF_C_READ
;
177 * Process each item within the archive until we find the first
178 * ELF object, or alternatively another archive to recurse into.
179 * Stop after analyzing the first plain object found.
181 while (!found
&& ((_elf
= elf_begin(fd
, cmd
, elf
)) != NULL
)) {
182 if ((arhdr
= elf_getarhdr(_elf
)) == NULL
)
184 if (*arhdr
->ar_name
!= '/') {
185 switch (elf_kind(_elf
)) {
187 found
= archive(fd
, _elf
, class_ret
, mach_ret
);
190 if (gelf_getclass(_elf
) == ELFCLASS64
) {
193 if ((ehdr
= elf64_getehdr(_elf
)) ==
196 *class_ret
= ehdr
->e_ident
[EI_CLASS
];
197 *mach_ret
= ehdr
->e_machine
;
201 if ((ehdr
= elf32_getehdr(_elf
)) ==
204 *class_ret
= ehdr
->e_ident
[EI_CLASS
];
205 *mach_ret
= ehdr
->e_machine
;
212 cmd
= elf_next(_elf
);
213 (void) elf_end(_elf
);
221 * - ELFCLASS of resulting object (class)
222 * - Whether user specified class of the linker (ldclass)
223 * - ELF machine type of resulting object (m_mach)
225 * In order of priority, we determine this information as follows:
227 * - Command line options (-32, -64, -z altexec64, -z target).
228 * - From the first plain object seen on the command line. (This is
229 * by far the most common case.)
230 * - From the first object contained within the first archive
231 * on the command line.
232 * - If all else fails, we assume a 32-bit object for the native machine.
235 * argc, argv - Command line argument vector
236 * class_ret - Address of variable to receive ELFCLASS of output object
237 * ldclass_ret - Address of variable to receive ELFCLASS of
238 * linker to use. This will be ELFCLASS32/ELFCLASS64 if one
239 * is explicitly specified, and ELFCLASSNONE otherwise.
240 * ELFCLASSNONE therefore means that we should use the best
241 * link-editor that the system/kernel will allow.
244 process_args(int argc
, char **argv
, uchar_t
*class_ret
, uchar_t
*ldclass_ret
,
247 uchar_t ldclass
= ELFCLASSNONE
, class = ELFCLASSNONE
, ar_class
;
248 Half mach32
= EM_NONE
, mach64
= EM_NONE
, ar_mach
;
252 * In general, libld.so is responsible for processing the
253 * command line options. The exception to this are those options
254 * that contain information about which linker to run and the
255 * class/machine of the output object. We examine the options
256 * here looking for the following:
258 * -32 Produce an ELFCLASS32 object. This is the default, so
259 * -32 is only needed when linking entirely from archives,
260 * and the first archive contains a mix of 32 and 64-bit
261 * objects, and the first object in that archive is 64-bit.
262 * We do not expect this option to get much use, but it
263 * ensures that the user can handle any situation.
265 * -64 Produce an ELFCLASS64 object. (Note that this will
266 * indirectly cause the use of the 64-bit linker if
267 * the system is 64-bit capable). The most common need
268 * for this option is when linking a filter object entirely
269 * from a mapfile. The less common case is when linking
270 * entirely from archives, and the first archive contains
271 * a mix of 32 and 64-bit objects, and the first object
272 * in that archive is 32-bit.
275 * Use the 64-bit linker regardless of the class
276 * of the output object.
279 * Produce output object for the specified platform.
280 * This option is needed when producing an object
281 * for a non-native target entirely from a mapfile,
282 * or when linking entirely from an archive containing
283 * objects for multiple targets, and the first object
284 * in the archive is not for the desired target.
286 * If we've already processed an object and we find -32/-64, and
287 * the object is of the wrong class, we have an error condition.
288 * We ignore it here, and let it fall through to libld, where the
289 * proper diagnosis and error message will occur.
294 while ((c
= ld_getopt(0, optind
, argc
, argv
)) != -1) {
297 if (strncmp(optarg
, MSG_ORIG(MSG_ARG_TWO
),
298 MSG_ARG_TWO_SIZE
) == 0)
303 if (strncmp(optarg
, MSG_ORIG(MSG_ARG_FOUR
),
304 MSG_ARG_FOUR_SIZE
) == 0)
311 if (strncmp(optarg
, MSG_ORIG(MSG_ARG_ALTEXEC64
),
312 MSG_ARG_ALTEXEC64_SIZE
) == 0) {
313 ldclass
= ELFCLASS64
;
317 /* -z target=platform */
318 if (strncmp(optarg
, MSG_ORIG(MSG_ARG_TARGET
),
319 MSG_ARG_TARGET_SIZE
) == 0) {
320 char *pstr
= optarg
+ MSG_ARG_TARGET_SIZE
;
323 MSG_ORIG(MSG_TARG_SPARC
)) == 0) {
326 } else if (strcasecmp(pstr
,
327 MSG_ORIG(MSG_TARG_X86
)) == 0) {
331 eprintf(0, ERR_FATAL
,
332 MSG_INTL(MSG_ERR_BADTARG
), pstr
);
341 * Continue to look for the first ELF object to determine the class of
342 * objects to operate on. At the same time, look for the first archive
343 * of ELF objects --- if no plain ELF object is specified, the type
344 * of the first ELF object in the first archive will be used. If
345 * there is no object, and no archive, then we fall back to a 32-bit
346 * object for the native machine.
348 for (; optind
< argc
; optind
++) {
353 * If we detect some more options return to getopt().
354 * Checking argv[optind][1] against null prevents a forever
355 * loop if an unadorned `-' argument is passed to us.
357 if (argv
[optind
][0] == '-') {
358 if (argv
[optind
][1] == '\0')
365 * If we've already determined the object class and
366 * machine type, continue to the next argument. Only
367 * the first object contributes to this decision, and
368 * there's no value to opening or examing the subsequent
369 * ones. We do need to keep going though, because there
370 * may be additional options that might affect our
371 * class/machine decision.
373 if ((class != ELFCLASSNONE
) && (mach32
!= EM_NONE
))
377 * Open the file and determine if it is an object. We are
378 * looking for ELF objects, or archives of ELF objects.
380 * Plain objects are simple, and are the common case, so
381 * we examine them directly and avoid the map-unmap-map
382 * that would occur if we used libelf. Archives are too
383 * complex to be worth accessing directly, so if we identify
384 * an archive, we use libelf on it and accept the cost.
386 if ((fd
= open(argv
[optind
], O_RDONLY
)) == -1) {
389 eprintf(0, ERR_FATAL
, MSG_INTL(MSG_SYS_OPEN
),
390 argv
[optind
], strerror(err
));
394 if (pread(fd
, &hdr
, sizeof (hdr
), 0) != sizeof (hdr
)) {
399 if ((hdr
.ehdr
.e_ident
[EI_MAG0
] == ELFMAG0
) &&
400 (hdr
.ehdr
.e_ident
[EI_MAG1
] == ELFMAG1
) &&
401 (hdr
.ehdr
.e_ident
[EI_MAG2
] == ELFMAG2
) &&
402 (hdr
.ehdr
.e_ident
[EI_MAG3
] == ELFMAG3
)) {
403 if (class == ELFCLASSNONE
) {
404 class = hdr
.ehdr
.e_ident
[EI_CLASS
];
405 if ((class != ELFCLASS32
) &&
406 (class != ELFCLASS64
))
407 class = ELFCLASSNONE
;
410 if (mach32
== EM_NONE
) {
412 uchar_t
*one_p
= (uchar_t
*)&one
;
415 ld_elfdata
= (one_p
[0] == 1) ?
416 ELFDATA2LSB
: ELFDATA2MSB
;
418 * Both the 32 and 64-bit versions get the
419 * type from the object. If the user has
420 * asked for an inconsistant class/machine
421 * combination, libld will catch it.
424 (ld_elfdata
== hdr
.ehdr
.e_ident
[EI_DATA
]) ?
426 BSWAP_HALF(hdr
.ehdr
.e_machine
);
428 } else if (!ar_found
&&
429 (memcmp(&hdr
.armag
, ARMAG
, SARMAG
) == 0)) {
432 (void) elf_version(EV_CURRENT
);
433 if ((elf
= elf_begin(fd
, ELF_C_READ
, NULL
)) == NULL
) {
437 if (elf_kind(elf
) == ELF_K_AR
)
439 archive(fd
, elf
, &ar_class
, &ar_mach
);
447 * ELFCLASS of output object: If we did not establish a class from a
448 * command option, or from the first plain object, then use the class
449 * from the first archive, and failing that, default to 32-bit.
451 if (class == ELFCLASSNONE
)
452 class = ar_found
? ar_class
: ELFCLASS32
;
455 /* ELFCLASS of link-editor to use */
456 *ldclass_ret
= ldclass
;
459 * Machine type of output object: If we did not establish a machine
460 * type from the command line, or from the first plain object, then
461 * use the machine established by the first archive, and failing that,
462 * use the native machine.
464 *mach
= (class == ELFCLASS64
) ? mach64
: mach32
;
465 if (*mach
== EM_NONE
)
469 *mach
= (class == ELFCLASS64
) ? M_MACH_64
: M_MACH_32
;
475 * Process an LD_OPTIONS environment string. This routine is first called to
476 * count the number of options, and second to initialize a new argument array
480 process_ldoptions(char *str
, char **nargv
)
486 * Walk the environment string processing any arguments that are
487 * separated by white space.
489 while (*str
!= '\0') {
492 * If a new argument array has been provided, terminate
493 * the original environment string, and initialize the
494 * appropriate argument array entry.
502 while (isspace(*str
))
510 * If a new argument array has been provided, initialize the
511 * final argument array entry.
522 * Determine whether an LD_OPTIONS environment variable is set, and if so,
523 * prepend environment string as a series of options to the argv array.
526 prepend_ldoptions(int *argcp
, char ***argvp
)
529 char **nargv
, *ld_options
;
532 if ((ld_options
= getenv(MSG_ORIG(MSG_LD_OPTIONS
))) == NULL
)
536 * Get rid of any leading white space, and make sure the environment
539 while (isspace(*ld_options
))
541 if (ld_options
[0] == '\0')
545 * Prevent modification of actual environment strings.
547 if ((ld_options
= strdup(ld_options
)) == NULL
) {
549 eprintf(0, ERR_FATAL
, MSG_INTL(MSG_SYS_ALLOC
), strerror(err
));
554 * Determine the number of options provided.
556 nargc
= process_ldoptions(ld_options
, NULL
);
559 * Allocate a new argv array big enough to hold the new options from
560 * the environment string and the old argv options.
562 if ((nargv
= malloc((nargc
+ *argcp
+ 1) * sizeof (char *))) == NULL
) {
564 eprintf(0, ERR_FATAL
, MSG_INTL(MSG_SYS_ALLOC
), strerror(err
));
569 * Initialize first element of new argv array to be the first element
570 * of the old argv array (ie. calling programs name). Then add the new
571 * args obtained from the environment.
574 nargv
[nargc
++] = (*argvp
)[0];
575 nargc
+= process_ldoptions(ld_options
, &nargv
[nargc
]);
578 * Now add the original argv array (skipping argv[0]) to the end of the
579 * new argv array, and re-vector argc and argv to reference this new
582 for (count
= 1; count
< *argcp
; count
++, nargc
++)
583 nargv
[nargc
] = (*argvp
)[count
];
594 * Check to see if there is a LD_ALTEXEC=<path to alternate ld> in the
595 * environment. If so, first null the environment variable out, and then
596 * exec() the binary pointed to by the environment variable, passing the same
597 * arguments as the originating process. This mechanism permits using
598 * alternate link-editors (debugging/developer copies) even in complex build
602 ld_altexec(char **argv
, char **envp
)
608 for (str
= envp
; *str
; str
++) {
609 if (strncmp(*str
, MSG_ORIG(MSG_LD_ALTEXEC
),
610 MSG_LD_ALTEXEC_SIZE
) == 0) {
616 * If LD_ALTEXEC isn't set, return to continue executing the present
623 * Get a pointer to the actual string. If it's a null entry, return.
625 execstr
= strdup(*str
+ MSG_LD_ALTEXEC_SIZE
);
626 if (*execstr
== '\0')
630 * Null out the LD_ALTEXEC= environment entry.
632 (*str
)[MSG_LD_ALTEXEC_SIZE
] = '\0';
635 * Set argv[0] to point to our new linker
640 * And attempt to execute it.
642 (void) execve(execstr
, argv
, envp
);
645 * If the exec() fails, return a failure indication.
648 eprintf(0, ERR_FATAL
, MSG_INTL(MSG_SYS_EXEC
), execstr
,
654 main(int argc
, char **argv
, char **envp
)
657 uchar_t
class, ldclass
, checkclass
;
663 (void) setlocale(LC_MESSAGES
, MSG_ORIG(MSG_STR_EMPTY
));
664 (void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS
));
667 * Execute an alternate linker if the LD_ALTEXEC environment variable is
668 * set. If a specified alternative could not be found, bail.
670 if (ld_altexec(argv
, envp
))
674 * Check the LD_OPTIONS environment variable, and if present prepend
675 * the arguments specified to the command line argument list.
677 if (prepend_ldoptions(&argc
, &argv
))
681 * Examine the command arguments to determine:
683 * - link-editor class
686 if (process_args(argc
, argv
, &class, &ldclass
, &mach
))
690 * Unless a 32-bit link-editor was explicitly requested, try
691 * to exec the 64-bit version.
693 if (ldclass
!= ELFCLASS32
)
694 checkclass
= conv_check_native(oargv
, envp
);
697 * If an attempt to exec the 64-bit link-editor fails:
698 * - Bail if the 64-bit linker was explicitly requested
699 * - Continue quietly if the 64-bit linker was not requested.
700 * This is undoubtedly due to hardware/kernel limitations,
701 * and therefore represents the best we can do. Note that
702 * the 32-bit linker is capable of linking anything the
703 * 64-bit version is, subject to a 4GB limit on memory, and
706 if ((ldclass
== ELFCLASS64
) && (checkclass
!= ELFCLASS64
)) {
707 eprintf(0, ERR_FATAL
, MSG_INTL(MSG_SYS_64
));
711 /* Call the libld entry point for the specified ELFCLASS */
712 if (class == ELFCLASS64
)
713 return (ld64_main(argc
, argv
, mach
));
715 return (ld32_main(argc
, argv
, mach
));
719 * We supply this function for the msg module
724 return (gettext(MSG_ORIG(mid
)));