dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / sgs / ld / common / ld.c
blobf30b830d013739c7d84fd3d1ba8a09b35fa3f015
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <libintl.h>
35 #include <locale.h>
36 #include <fcntl.h>
37 #include <ar.h>
38 #include <gelf.h>
39 #include "conv.h"
40 #include "libld.h"
41 #include "machdep.h"
42 #include "msg.h"
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
74 * this header.
76 typedef union {
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 */
81 } ehdr;
82 char armag[SARMAG];
83 } FILE_HDR;
87 * Print a message to stdout
89 void
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.
98 switch (error) {
99 case ERR_WARNING_NF:
100 if (strings[ERR_WARNING_NF] == NULL)
101 strings[ERR_WARNING_NF] = MSG_INTL(MSG_ERR_WARNING);
102 break;
103 case ERR_WARNING:
104 if (strings[ERR_WARNING] == NULL)
105 strings[ERR_WARNING] = MSG_INTL(MSG_ERR_WARNING);
106 break;
107 case ERR_GUIDANCE:
108 if (strings[ERR_GUIDANCE] == NULL)
109 strings[ERR_GUIDANCE] = MSG_INTL(MSG_ERR_GUIDANCE);
110 break;
111 case ERR_FATAL:
112 if (strings[ERR_FATAL] == NULL)
113 strings[ERR_FATAL] = MSG_INTL(MSG_ERR_FATAL);
114 break;
115 case ERR_ELF:
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) {
128 int elferr;
130 if ((elferr = elf_errno()) != 0)
131 (void) fprintf(stderr, MSG_ORIG(MSG_STR_ELFDIAG),
132 elf_errmsg(elferr));
134 (void) fprintf(stderr, MSG_ORIG(MSG_STR_NL));
135 (void) fflush(stderr);
140 * Print a message to stdout
142 /* VARARGS3 */
143 void
144 eprintf(Lm_list *lml, Error error, const char *format, ...)
146 va_list args;
148 va_start(args, format);
149 veprintf(lml, error, format, args);
150 va_end(args);
155 * Examine the first object in an archive to determine its ELFCLASS
156 * and machine type.
158 * entry:
159 * fd - Open file descriptor for file
160 * elf - libelf ELF descriptor
161 * class_ret, mach_ret - Address of variables to receive ELFCLASS
162 * and machine type.
164 * exit:
165 * On success, *class_ret and *mach_ret are filled in, and True (1)
166 * is returned. On failure, False (0) is returned.
168 static int
169 archive(int fd, Elf *elf, uchar_t *class_ret, Half *mach_ret)
171 Elf_Cmd cmd = ELF_C_READ;
172 Elf_Arhdr *arhdr;
173 Elf *_elf = NULL;
174 int found = 0;
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)
183 return (0);
184 if (*arhdr->ar_name != '/') {
185 switch (elf_kind(_elf)) {
186 case ELF_K_AR:
187 found = archive(fd, _elf, class_ret, mach_ret);
188 break;
189 case ELF_K_ELF:
190 if (gelf_getclass(_elf) == ELFCLASS64) {
191 Elf64_Ehdr *ehdr;
193 if ((ehdr = elf64_getehdr(_elf)) ==
194 NULL)
195 break;
196 *class_ret = ehdr->e_ident[EI_CLASS];
197 *mach_ret = ehdr->e_machine;
198 } else {
199 Elf32_Ehdr *ehdr;
201 if ((ehdr = elf32_getehdr(_elf)) ==
202 NULL)
203 break;
204 *class_ret = ehdr->e_ident[EI_CLASS];
205 *mach_ret = ehdr->e_machine;
207 found = 1;
208 break;
212 cmd = elf_next(_elf);
213 (void) elf_end(_elf);
216 return (found);
220 * Determine:
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.
234 * entry:
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.
243 static int
244 process_args(int argc, char **argv, uchar_t *class_ret, uchar_t *ldclass_ret,
245 Half *mach)
247 uchar_t ldclass = ELFCLASSNONE, class = ELFCLASSNONE, ar_class;
248 Half mach32 = EM_NONE, mach64 = EM_NONE, ar_mach;
249 int c, ar_found = 0;
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.
274 * -z altexec64
275 * Use the 64-bit linker regardless of the class
276 * of the output object.
278 * -z target=platform
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.
291 opterr = 0;
292 optind = 1;
293 getmore:
294 while ((c = ld_getopt(0, optind, argc, argv)) != -1) {
295 switch (c) {
296 case '3':
297 if (strncmp(optarg, MSG_ORIG(MSG_ARG_TWO),
298 MSG_ARG_TWO_SIZE) == 0)
299 class = ELFCLASS32;
300 break;
302 case '6':
303 if (strncmp(optarg, MSG_ORIG(MSG_ARG_FOUR),
304 MSG_ARG_FOUR_SIZE) == 0)
305 class = ELFCLASS64;
306 break;
308 case 'z':
309 #if !defined(_LP64)
310 /* -z altexec64 */
311 if (strncmp(optarg, MSG_ORIG(MSG_ARG_ALTEXEC64),
312 MSG_ARG_ALTEXEC64_SIZE) == 0) {
313 ldclass = ELFCLASS64;
314 break;
316 #endif
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;
322 if (strcasecmp(pstr,
323 MSG_ORIG(MSG_TARG_SPARC)) == 0) {
324 mach32 = EM_SPARC;
325 mach64 = EM_SPARCV9;
326 } else if (strcasecmp(pstr,
327 MSG_ORIG(MSG_TARG_X86)) == 0) {
328 mach32 = EM_386;
329 mach64 = EM_AMD64;
330 } else {
331 eprintf(0, ERR_FATAL,
332 MSG_INTL(MSG_ERR_BADTARG), pstr);
333 return (1);
336 break;
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++) {
349 int fd;
350 FILE_HDR hdr;
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')
359 continue;
360 else
361 goto getmore;
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))
374 continue;
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) {
387 int err = errno;
389 eprintf(0, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
390 argv[optind], strerror(err));
391 return (1);
394 if (pread(fd, &hdr, sizeof (hdr), 0) != sizeof (hdr)) {
395 (void) close(fd);
396 continue;
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) {
411 int one = 1;
412 uchar_t *one_p = (uchar_t *)&one;
413 int ld_elfdata;
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.
423 mach32 = mach64 =
424 (ld_elfdata == hdr.ehdr.e_ident[EI_DATA]) ?
425 hdr.ehdr.e_machine :
426 BSWAP_HALF(hdr.ehdr.e_machine);
428 } else if (!ar_found &&
429 (memcmp(&hdr.armag, ARMAG, SARMAG) == 0)) {
430 Elf *elf;
432 (void) elf_version(EV_CURRENT);
433 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
434 (void) close(fd);
435 continue;
437 if (elf_kind(elf) == ELF_K_AR)
438 ar_found =
439 archive(fd, elf, &ar_class, &ar_mach);
440 (void) elf_end(elf);
443 (void) close(fd);
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;
453 *class_ret = class;
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)
466 if (ar_found)
467 *mach = ar_mach;
468 else
469 *mach = (class == ELFCLASS64) ? M_MACH_64 : M_MACH_32;
471 return (0);
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
477 * with each option.
479 static int
480 process_ldoptions(char *str, char **nargv)
482 int argc = 0;
483 char *arg = str;
486 * Walk the environment string processing any arguments that are
487 * separated by white space.
489 while (*str != '\0') {
490 if (isspace(*str)) {
492 * If a new argument array has been provided, terminate
493 * the original environment string, and initialize the
494 * appropriate argument array entry.
496 if (nargv) {
497 *str++ = '\0';
498 nargv[argc] = arg;
501 argc++;
502 while (isspace(*str))
503 str++;
504 arg = str;
505 } else
506 str++;
508 if (arg != str) {
510 * If a new argument array has been provided, initialize the
511 * final argument array entry.
513 if (nargv)
514 nargv[argc] = arg;
515 argc++;
518 return (argc);
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.
525 static int
526 prepend_ldoptions(int *argcp, char ***argvp)
528 int nargc;
529 char **nargv, *ld_options;
530 int err, count;
532 if ((ld_options = getenv(MSG_ORIG(MSG_LD_OPTIONS))) == NULL)
533 return (0);
536 * Get rid of any leading white space, and make sure the environment
537 * string has size.
539 while (isspace(*ld_options))
540 ld_options++;
541 if (ld_options[0] == '\0')
542 return (0);
545 * Prevent modification of actual environment strings.
547 if ((ld_options = strdup(ld_options)) == NULL) {
548 err = errno;
549 eprintf(0, ERR_FATAL, MSG_INTL(MSG_SYS_ALLOC), strerror(err));
550 return (1);
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) {
563 err = errno;
564 eprintf(0, ERR_FATAL, MSG_INTL(MSG_SYS_ALLOC), strerror(err));
565 return (1);
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.
573 nargc = 0;
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
580 * array
582 for (count = 1; count < *argcp; count++, nargc++)
583 nargv[nargc] = (*argvp)[count];
585 nargv[nargc] = NULL;
587 *argcp = nargc;
588 *argvp = nargv;
590 return (0);
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
599 * environments.
601 static int
602 ld_altexec(char **argv, char **envp)
604 char *execstr;
605 char **str;
606 int err;
608 for (str = envp; *str; str++) {
609 if (strncmp(*str, MSG_ORIG(MSG_LD_ALTEXEC),
610 MSG_LD_ALTEXEC_SIZE) == 0) {
611 break;
616 * If LD_ALTEXEC isn't set, return to continue executing the present
617 * link-editor.
619 if (*str == 0)
620 return (0);
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')
627 return (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
637 argv[0] = execstr;
640 * And attempt to execute it.
642 (void) execve(execstr, argv, envp);
645 * If the exec() fails, return a failure indication.
647 err = errno;
648 eprintf(0, ERR_FATAL, MSG_INTL(MSG_SYS_EXEC), execstr,
649 strerror(err));
650 return (1);
654 main(int argc, char **argv, char **envp)
656 char **oargv = argv;
657 uchar_t class, ldclass, checkclass;
658 Half mach;
661 * Establish locale.
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))
671 return (1);
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))
678 return (1);
681 * Examine the command arguments to determine:
682 * - object class
683 * - link-editor class
684 * - target machine
686 if (process_args(argc, argv, &class, &ldclass, &mach))
687 return (1);
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
704 * 2GB object size.
706 if ((ldclass == ELFCLASS64) && (checkclass != ELFCLASS64)) {
707 eprintf(0, ERR_FATAL, MSG_INTL(MSG_SYS_64));
708 return (1);
711 /* Call the libld entry point for the specified ELFCLASS */
712 if (class == ELFCLASS64)
713 return (ld64_main(argc, argv, mach));
714 else
715 return (ld32_main(argc, argv, mach));
719 * We supply this function for the msg module
721 const char *
722 _ld_msg(Msg mid)
724 return (gettext(MSG_ORIG(mid)));