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