1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 * 21 * Copyright (c) 1988 AT&T 22 * All Rights Reserved 23 * 24 * 25 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved. 26 */ 27 28 /* 29 * Print the list of shared objects required by a dynamic executable or shared 30 * object. 31 * 32 * usage is: ldd [-d | -r] [-c] [-e envar] [-i] [-f] [-L] [-l] [-p] [-s] 33 * [-U | -u] [-v] [-w] file(s) 34 * 35 * ldd opens the file and verifies the information in the elf header. 36 * If the file is a dynamic executable, we set up some environment variables 37 * and exec(2) the file. If the file is a shared object, we preload the 38 * file with a dynamic executable stub. The runtime linker (ld.so.1) actually 39 * provides the diagnostic output, according to the environment variables set. 40 * 41 * If neither -d nor -r is specified, we set only LD_TRACE_LOADED_OBJECTS_[AE]. 42 * The runtime linker will print the pathnames of all dynamic objects it 43 * loads, and then exit. Note that we distiguish between ELF and AOUT objects 44 * when setting this environment variable - AOUT executables cause the mapping 45 * of sbcp, the dependencies of which the user isn't interested in. 46 * 47 * If -d or -r is specified, we also set LD_WARN=1; the runtime linker will 48 * perform its normal relocations and issue warning messages for unresolved 49 * references. It will then exit. 50 * If -r is specified, we set LD_BIND_NOW=1, so that the runtime linker 51 * will perform all relocations, otherwise (under -d) the runtime linker 52 * will not perform PLT (function) type relocations. 53 * 54 * If -c is specified we also set LD_NOCONFIG=1, thus disabling any 55 * configuration file use. 56 * 57 * If -e is specified the associated environment variable is set for the 58 * child process that will produce ldd's diagnostics. 59 * 60 * If -i is specified, we set LD_INIT=1. The order of inititialization 61 * sections to be executed is printed. We also set LD_WARN=1. 62 * 63 * If -f is specified, we will run ldd as root on executables that have 64 * an unsercure runtime linker that does not live under the "/usr/lib" 65 * directory. By default we will not let this happen. 66 * 67 * If -l is specified it generates a warning for any auxiliary filter not found. 68 * Prior to 2.8 this forced any filters to load (all) their filtees. This is 69 * now the default, however missing auxiliary filters don't generate any error 70 * diagniostic. See also -L. 71 * 72 * If -L is specified we revert to lazy loading, thus any filtee or lazy 73 * dependency loading is deferred until relocations cause loading. Without 74 * this option we set LD_LOADFLTR=1, thus forcing any filters to load (all) 75 * their filtees, and LD_NOLAZYLOAD=1 thus forcing immediate processing of 76 * any lazy loaded dependencies. 77 * 78 * If -s is specified we also set LD_TRACE_SEARCH_PATH=1, thus enabling 79 * the runtime linker to indicate the search algorithm used. 80 * 81 * If -v is specified we also set LD_VERBOSE=1, thus enabling the runtime 82 * linker to indicate all object dependencies (not just the first object 83 * loaded) together with any versionig requirements. 84 * 85 * If -U or -u is specified unused dependencies are detected. -u causes 86 * LD_UNUSED=1 to be set, which causes dependencies that are unused within the 87 * process to be detected. -U causes LD_UNREF=1 to be set, which causes 88 * unreferenced objects, and unreferenced cyclic dependencies to be detected. 89 * These options assert that at least -d is set as relocation references are 90 * what determine an objects use. 91 * 92 * If -w is specified, no unresolved weak references are allowed. -w causes 93 * LD_NOUNRESWEAK=1 to be set. By default, an unresolved weak reference is 94 * allowed, and a "0" is written to the relocation offset. The -w option 95 * disables this default. Any weak references that can not be resolved result 96 * in relocation error messages. This option has no use without -r or -d. 97 * 98 * If the -p option is specified, no unresolved PARENT or EXTERN references are 99 * allowed. -p causes LD_NOPAREXT=1 to be set. By default, PARENT and EXTERN 100 * references, which have been explicitly assigned via a mapfile when a shared 101 * object was built, imply that a caller will provide the symbols, and hence 102 * these are not reported as relocation errors. Note, the -p option is asserted 103 * by default when either the -r or -d options are used to inspect a dynamic 104 * executable. This option has no use with a shared object without -r or -d. 105 */ 106 #include <fcntl.h> 107 #include <stdio.h> 108 #include <string.h> 109 #include <_libelf.h> 110 #include <stdlib.h> 111 #include <unistd.h> 112 #include <wait.h> 113 #include <locale.h> 114 #include <errno.h> 115 #include <signal.h> 116 #include "machdep.h" 117 #include "sgs.h" 118 #include "conv.h" 119 #include "a.out.h" 120 #include "msg.h" 121 122 static int elf_check(int, char *, char *, Elf *, int); 123 static int aout_check(int, char *, char *, int, int); 124 static int run(int, char *, char *, const char *, int); 125 126 127 /* 128 * Define all environment variable strings. The character following the "=" 129 * will be written to, to disable or enable the associated feature. 130 */ 131 static char bind[] = "LD_BIND_NOW= ", 132 load_elf[] = "LD_TRACE_LOADED_OBJECTS_E= ", 133 load_aout[] = "LD_TRACE_LOADED_OBJECTS_A= ", 134 path[] = "LD_TRACE_SEARCH_PATHS= ", 135 verb[] = "LD_VERBOSE= ", 136 warn[] = "LD_WARN= ", 137 conf[] = "LD_NOCONFIG= ", 138 fltr[] = "LD_LOADFLTR= ", 139 lazy[] = "LD_NOLAZYLOAD=1", 140 init[] = "LD_INIT= ", 141 uref[] = "LD_UNREF= ", 142 used[] = "LD_UNUSED= ", 143 weak[] = "LD_NOUNRESWEAK= ", 144 nope[] = "LD_NOPAREXT= "; 145 static char *load; 146 147 static const char *prefile_32, *prefile_64, *prefile; 148 static APlist *eopts = NULL; 149 150 int 151 main(int argc, char **argv, char **envp) 152 { 153 char *str, *cname = argv[0]; 154 155 Elf *elf; 156 int cflag = 0, dflag = 0, fflag = 0, iflag = 0, Lflag = 0; 157 int lflag = 0, rflag = 0, sflag = 0, Uflag = 0, uflag = 0; 158 int pflag = 0, vflag = 0, wflag = 0, nfile, var, error = 0; 159 160 Aliste idx; 161 162 /* 163 * If we're on a 64-bit kernel, try to exec a full 64-bit version of 164 * the binary. If successful, conv_check_native() won't return. 165 * 166 * This is done to ensure that ldd can handle objects >2GB. 167 * ldd uses libelf, which is not large file capable. The 168 * 64-bit ldd can handle any sized object. 169 */ 170 (void) conv_check_native(argv, envp); 171 172 /* 173 * Establish locale. 174 */ 175 (void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY)); 176 (void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS)); 177 178 /* 179 * verify command line syntax and process arguments 180 */ 181 opterr = 0; /* disable getopt error mesg */ 182 183 while ((var = getopt(argc, argv, MSG_ORIG(MSG_STR_GETOPT))) != EOF) { 184 switch (var) { 185 case 'c' : /* enable config search */ 186 cflag = 1; 187 break; 188 case 'd' : /* perform data relocations */ 189 dflag = 1; 190 if (rflag) 191 error++; 192 break; 193 case 'e' : 194 if (aplist_append(&eopts, optarg, 10) == NULL) { 195 (void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC), 196 cname); 197 exit(1); 198 } 199 break; 200 case 'f' : 201 fflag = 1; 202 break; 203 case 'L' : 204 Lflag = 1; 205 break; 206 case 'l' : 207 lflag = 1; 208 break; 209 case 'i' : /* print the order of .init */ 210 iflag = 1; 211 break; 212 case 'p' : 213 pflag = 1; /* expose unreferenced */ 214 break; /* parent or externals */ 215 case 'r' : /* perform all relocations */ 216 rflag = 1; 217 if (dflag) 218 error++; 219 break; 220 case 's' : /* enable search path output */ 221 sflag = 1; 222 break; 223 case 'U' : /* list unreferenced */ 224 Uflag = 1; /* dependencies */ 225 if (uflag) 226 error++; 227 break; 228 case 'u' : /* list unused dependencies */ 229 uflag = 1; 230 if (Uflag) 231 error++; 232 break; 233 case 'v' : /* enable verbose output */ 234 vflag = 1; 235 break; 236 case 'w' : /* expose unresolved weak */ 237 wflag = 1; /* references */ 238 break; 239 default : 240 error++; 241 break; 242 } 243 if (error) 244 break; 245 } 246 if (error) { 247 (void) fprintf(stderr, MSG_INTL(MSG_ARG_USAGE), cname); 248 exit(1); 249 } 250 251 /* 252 * Determine if any of the LD_PRELOAD family is already set in the 253 * environment, if so we'll continue to analyze each object with the 254 * appropriate setting. 255 */ 256 if (((prefile_32 = getenv(MSG_ORIG(MSG_LD_PRELOAD_32))) == NULL) || 257 (*prefile_32 == '\0')) { 258 prefile_32 = MSG_ORIG(MSG_STR_EMPTY); 259 } 260 if (((prefile_64 = getenv(MSG_ORIG(MSG_LD_PRELOAD_64))) == NULL) || 261 (*prefile_64 == '\0')) { 262 prefile_64 = MSG_ORIG(MSG_STR_EMPTY); 263 } 264 if (((prefile = getenv(MSG_ORIG(MSG_LD_PRELOAD))) == NULL) || 265 (*prefile == '\0')) { 266 prefile = MSG_ORIG(MSG_STR_EMPTY); 267 } 268 269 /* 270 * Determine if any environment requests are for the LD_PRELOAD family, 271 * and if so override any environment settings we've established above. 272 */ 273 for (APLIST_TRAVERSE(eopts, idx, str)) { 274 if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD_32), 275 MSG_LD_PRELOAD_32_SIZE)) == 0) { 276 str += MSG_LD_PRELOAD_32_SIZE; 277 if ((*str++ == '=') && (*str != '\0')) 278 prefile_32 = str; 279 continue; 280 } 281 if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD_64), 282 MSG_LD_PRELOAD_64_SIZE)) == 0) { 283 str += MSG_LD_PRELOAD_64_SIZE; 284 if ((*str++ == '=') && (*str != '\0')) 285 prefile_64 = str; 286 continue; 287 } 288 if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD), 289 MSG_LD_PRELOAD_SIZE)) == 0) { 290 str += MSG_LD_PRELOAD_SIZE; 291 if ((*str++ == '=') && (*str != '\0')) 292 prefile = str; 293 continue; 294 } 295 } 296 297 /* 298 * Set the appropriate relocation environment variables (Note unsetting 299 * the environment variables is done just in case the user already 300 * has these in their environment ... sort of thing the test folks 301 * would do :-) 302 */ 303 warn[sizeof (warn) - 2] = (dflag || rflag || Uflag || uflag) ? '1' : 304 '\0'; 305 bind[sizeof (bind) - 2] = (rflag) ? '1' : '\0'; 306 path[sizeof (path) - 2] = (sflag) ? '1' : '\0'; 307 verb[sizeof (verb) - 2] = (vflag) ? '1' : '\0'; 308 fltr[sizeof (fltr) - 2] = (Lflag) ? '\0' : (lflag) ? '2' : '1'; 309 init[sizeof (init) - 2] = (iflag) ? '1' : '\0'; 310 conf[sizeof (conf) - 2] = (cflag) ? '1' : '\0'; 311 lazy[sizeof (lazy) - 2] = (Lflag) ? '\0' : '1'; 312 uref[sizeof (uref) - 2] = (Uflag) ? '1' : '\0'; 313 used[sizeof (used) - 2] = (uflag) ? '1' : '\0'; 314 weak[sizeof (weak) - 2] = (wflag) ? '1' : '\0'; 315 nope[sizeof (nope) - 2] = (pflag) ? '1' : '\0'; 316 317 /* 318 * coordinate libelf's version information 319 */ 320 if (elf_version(EV_CURRENT) == EV_NONE) { 321 (void) fprintf(stderr, MSG_INTL(MSG_ELF_LIBELF), cname, 322 EV_CURRENT); 323 exit(1); 324 } 325 326 /* 327 * Loop through remaining arguments. Note that from here on there 328 * are no exit conditions so that we can process a list of files, 329 * any error condition is retained for a final exit status. 330 */ 331 nfile = argc - optind; 332 for (; optind < argc; optind++) { 333 char *fname = argv[optind]; 334 335 /* 336 * Open file (do this before checking access so that we can 337 * provide the user with better diagnostics). 338 */ 339 if ((var = open(fname, O_RDONLY)) == -1) { 340 int err = errno; 341 (void) fprintf(stderr, MSG_INTL(MSG_SYS_OPEN), cname, 342 fname, strerror(err)); 343 error = 1; 344 continue; 345 } 346 347 /* 348 * Get the files elf descriptor and process it as an elf or 349 * a.out (4.x) file. 350 */ 351 elf = elf_begin(var, ELF_C_READ, (Elf *)0); 352 switch (elf_kind(elf)) { 353 case ELF_K_AR : 354 (void) fprintf(stderr, MSG_INTL(MSG_USP_NODYNORSO), 355 cname, fname); 356 error = 1; 357 break; 358 case ELF_K_COFF: 359 (void) fprintf(stderr, MSG_INTL(MSG_USP_UNKNOWN), 360 cname, fname); 361 error = 1; 362 break; 363 case ELF_K_ELF: 364 if (elf_check(nfile, fname, cname, elf, fflag) != NULL) 365 error = 1; 366 break; 367 default: 368 /* 369 * This is either an unknown file or an aout format 370 */ 371 if (aout_check(nfile, fname, cname, var, fflag) != NULL) 372 error = 1; 373 break; 374 } 375 (void) elf_end(elf); 376 (void) close(var); 377 } 378 return (error); 379 } 380 381 382 383 static int 384 elf_check(int nfile, char *fname, char *cname, Elf *elf, int fflag) 385 { 386 Conv_inv_buf_t inv_buf; 387 GElf_Ehdr ehdr; 388 GElf_Phdr phdr; 389 int dynamic = 0, interp = 0, cnt, class; 390 391 /* 392 * verify information in file header 393 */ 394 if (gelf_getehdr(elf, &ehdr) == NULL) { 395 (void) fprintf(stderr, MSG_INTL(MSG_ELF_GETEHDR), 396 cname, fname, elf_errmsg(-1)); 397 return (1); 398 } 399 400 /* 401 * Compatible machine 402 */ 403 if ((ehdr.e_machine != M_MACH_32) && (ehdr.e_machine != M_MACH_64) && 404 (ehdr.e_machine != M_MACHPLUS)) { 405 (void) fprintf(stderr, MSG_INTL(MSG_ELF_MACHTYPE), cname, fname, 406 conv_ehdr_mach(ehdr.e_machine, 0, &inv_buf)); 407 return (1); 408 } 409 410 /* 411 * Compatible encoding (byte order) 412 */ 413 if (ehdr.e_ident[EI_DATA] != M_DATA) { 414 (void) fprintf(stderr, MSG_INTL(MSG_ELF_DATA), cname, fname, 415 conv_ehdr_data(ehdr.e_ident[EI_DATA], 0, &inv_buf)); 416 return (1); 417 } 418 419 /* 420 * Compatible class 421 */ 422 switch (class = ehdr.e_ident[EI_CLASS]) { 423 case ELFCLASS32: 424 /* 425 * If M_MACH is not the same thing as M_MACHPLUS and this 426 * is an M_MACHPLUS object, then the corresponding header 427 * flag must be set. 428 */ 429 if ((ehdr.e_machine != M_MACH) && 430 ((ehdr.e_flags & M_FLAGSPLUS) == 0)) { 431 (void) fprintf(stderr, MSG_INTL(MSG_ELF_MACHFLAGS), 432 cname, fname); 433 return (1); 434 } 435 break; 436 case ELFCLASS64: 437 /* Requires 64-bit kernel */ 438 if (conv_sys_eclass() == ELFCLASS32) { 439 (void) fprintf(stderr, MSG_INTL(MSG_ELF_KCLASS32), 440 cname, fname, conv_ehdr_class(class, 0, &inv_buf)); 441 return (1); 442 } 443 break; 444 default: 445 (void) fprintf(stderr, MSG_INTL(MSG_ELF_CLASS), cname, fname, 446 conv_ehdr_class(class, 0, &inv_buf)); 447 return (1); 448 } 449 450 /* 451 * Object type 452 */ 453 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN) && 454 (ehdr.e_type != ET_REL)) { 455 (void) fprintf(stderr, MSG_INTL(MSG_ELF_BADMAGIC), 456 cname, fname); 457 return (1); 458 } 459 460 /* 461 * Check that the file is executable. Dynamic executables must be 462 * executable to be exec'ed. Shared objects need not be executable to 463 * be mapped with a dynamic executable, however, by convention they're 464 * supposed to be executable. 465 */ 466 if (access(fname, X_OK) != 0) { 467 if (ehdr.e_type == ET_EXEC) { 468 (void) fprintf(stderr, MSG_INTL(MSG_USP_NOTEXEC_1), 469 cname, fname); 470 return (1); 471 } 472 (void) fprintf(stderr, MSG_INTL(MSG_USP_NOTEXEC_2), cname, 473 fname); 474 } 475 476 /* 477 * Determine whether we have a dynamic section or interpretor. 478 */ 479 for (cnt = 0; cnt < (int)ehdr.e_phnum; cnt++) { 480 if (dynamic && interp) 481 break; 482 483 if (gelf_getphdr(elf, cnt, &phdr) == NULL) { 484 (void) fprintf(stderr, MSG_INTL(MSG_ELF_GETPHDR), 485 cname, fname, elf_errmsg(-1)); 486 return (1); 487 } 488 489 if (phdr.p_type == PT_DYNAMIC) { 490 dynamic = 1; 491 continue; 492 } 493 494 if (phdr.p_type != PT_INTERP) 495 continue; 496 497 interp = 1; 498 499 /* 500 * If fflag is not set, and euid == root, and the interpreter 501 * does not live under /lib, /usr/lib or /etc/lib then don't 502 * allow ldd to execute the image. This prevents someone 503 * creating a `trojan horse' by substituting their own 504 * interpreter that could preform privileged operations 505 * when ldd is against it. 506 */ 507 if ((fflag == 0) && (geteuid() == 0) && 508 (strcmp(fname, conv_lddstub(class)) != 0)) { 509 char *interpreter; 510 511 /* 512 * Does the interpreter live under a trusted directory. 513 */ 514 interpreter = elf_getident(elf, 0) + phdr.p_offset; 515 516 if ((strncmp(interpreter, MSG_ORIG(MSG_PTH_USRLIB), 517 MSG_PTH_USRLIB_SIZE) != 0) && 518 (strncmp(interpreter, MSG_ORIG(MSG_PTH_LIB), 519 MSG_PTH_LIB_SIZE) != 0) && 520 (strncmp(interpreter, MSG_ORIG(MSG_PTH_ETCLIB), 521 MSG_PTH_ETCLIB_SIZE) != 0)) { 522 (void) fprintf(stderr, MSG_INTL(MSG_USP_ELFINS), 523 cname, fname, interpreter); 524 return (1); 525 } 526 } 527 } 528 529 /* 530 * Catch the case of a static executable (ie, an ET_EXEC that has a set 531 * of program headers but no PT_DYNAMIC). 532 */ 533 if (ehdr.e_phnum && !dynamic) { 534 (void) fprintf(stderr, MSG_INTL(MSG_USP_NODYNORSO), cname, 535 fname); 536 return (1); 537 } 538 539 /* 540 * If there is a dynamic section, then check for the DF_1_NOHDR 541 * flag, and bail if it is present. Such objects are created using 542 * a mapfile option (?N in the version 1 syntax, or HDR_NOALLOC 543 * otherwise). The ELF header and program headers are 544 * not mapped as part of the first segment, and virtual addresses 545 * are computed without them. If ldd tries to interpret such 546 * a file, it will become confused and generate bad output or 547 * crash. Such objects are always special purpose files (like an OS 548 * kernel) --- files for which the ldd operation doesn't make sense. 549 */ 550 if (dynamic && (_gelf_getdyndtflags_1(elf) & DF_1_NOHDR)) { 551 (void) fprintf(stderr, MSG_INTL(MSG_USP_NOHDR), cname, 552 fname); 553 return (1); 554 } 555 556 load = load_elf; 557 558 /* 559 * Run the required program (shared and relocatable objects require the 560 * use of lddstub). 561 */ 562 if ((ehdr.e_type == ET_EXEC) && interp) 563 return (run(nfile, cname, fname, (const char *)fname, class)); 564 else 565 return (run(nfile, cname, fname, conv_lddstub(class), class)); 566 } 567 568 static int 569 aout_check(int nfile, char *fname, char *cname, int fd, int fflag) 570 { 571 struct exec32 aout; 572 int err; 573 574 if (lseek(fd, 0, SEEK_SET) != 0) { 575 err = errno; 576 (void) fprintf(stderr, MSG_INTL(MSG_SYS_LSEEK), cname, fname, 577 strerror(err)); 578 return (1); 579 } 580 if (read(fd, (char *)&aout, sizeof (aout)) != sizeof (aout)) { 581 err = errno; 582 (void) fprintf(stderr, MSG_INTL(MSG_SYS_READ), cname, fname, 583 strerror(err)); 584 return (1); 585 } 586 if (aout.a_machtype != M_SPARC) { 587 (void) fprintf(stderr, MSG_INTL(MSG_USP_UNKNOWN), cname, fname); 588 return (1); 589 } 590 if (N_BADMAG(aout) || !aout.a_dynamic) { 591 (void) fprintf(stderr, MSG_INTL(MSG_USP_NODYNORSO), cname, 592 fname); 593 return (1); 594 } 595 if (!fflag && (geteuid() == 0)) { 596 (void) fprintf(stderr, MSG_INTL(MSG_USP_AOUTINS), cname, fname); 597 return (1); 598 } 599 600 /* 601 * Run the required program. 602 */ 603 if ((aout.a_magic == ZMAGIC) && (aout.a_entry <= sizeof (aout))) { 604 load = load_elf; 605 return (run(nfile, cname, fname, conv_lddstub(ELFCLASS32), 606 ELFCLASS32)); 607 } else { 608 load = load_aout; 609 return (run(nfile, cname, fname, (const char *)fname, 610 ELFCLASS32)); 611 } 612 } 613 614 615 /* 616 * Run the required program, setting the preload and trace environment 617 * variables accordingly. 618 */ 619 static int 620 run(int nfile, char *cname, char *fname, const char *ename, int class) 621 { 622 const char *preload = 0; 623 int pid, status; 624 625 if ((pid = fork()) == -1) { 626 int err = errno; 627 (void) fprintf(stderr, MSG_INTL(MSG_SYS_FORK), cname, 628 strerror(err)); 629 return (1); 630 } 631 632 if (pid) { /* parent */ 633 while (wait(&status) != pid) 634 ; 635 if (WIFSIGNALED(status) && ((WSIGMASK & status) != SIGPIPE)) { 636 (void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname, 637 fname); 638 (void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC_SIG), 639 (WSIGMASK & status), ((status & WCOREFLG) ? 640 MSG_INTL(MSG_SYS_EXEC_CORE) : 641 MSG_ORIG(MSG_STR_EMPTY))); 642 status = 1; 643 } else if (WHIBYTE(status)) { 644 (void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname, 645 fname); 646 (void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC_STAT), 647 WHIBYTE(status)); 648 status = 1; 649 } 650 } else { /* child */ 651 Aliste idx; 652 char *str; 653 size_t size; 654 655 /* 656 * When using ldd(1) to analyze a shared object we preload the 657 * shared object with lddstub. Any additional preload 658 * requirements are added after the object being analyzed, this 659 * allows us to skip the first object but produce diagnostics 660 * for each other preloaded object. 661 */ 662 if (fname != ename) { 663 char *str; 664 const char *files = prefile; 665 const char *format = MSG_ORIG(MSG_STR_FMT1); 666 667 for (str = fname; *str; str++) 668 if (*str == '/') { 669 format = MSG_ORIG(MSG_STR_FMT2); 670 break; 671 } 672 673 preload = MSG_ORIG(MSG_LD_PRELOAD); 674 675 /* 676 * Determine which preload files and preload environment 677 * variable to use. 678 */ 679 if (class == ELFCLASS64) { 680 if (prefile_64 != MSG_ORIG(MSG_STR_EMPTY)) { 681 files = prefile_64; 682 preload = MSG_ORIG(MSG_LD_PRELOAD_64); 683 } 684 } else { 685 if (prefile_32 != MSG_ORIG(MSG_STR_EMPTY)) { 686 files = prefile_32; 687 preload = MSG_ORIG(MSG_LD_PRELOAD_32); 688 } 689 } 690 691 if ((str = (char *)malloc(strlen(preload) + 692 strlen(fname) + strlen(files) + 5)) == 0) { 693 (void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC), 694 cname); 695 exit(1); 696 } 697 698 (void) sprintf(str, format, preload, fname, files); 699 if (putenv(str) != 0) { 700 (void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED), 701 cname); 702 exit(1); 703 } 704 705 /* 706 * The pointer "load" has be assigned to load_elf[] or 707 * load_aout[]. Use the size of load_elf[] as the size 708 * of load_aout[] is the same. 709 */ 710 load[sizeof (load_elf) - 2] = '2'; 711 } else 712 load[sizeof (load_elf) - 2] = '1'; 713 714 715 /* 716 * Establish new environment variables to affect the child 717 * process. 718 */ 719 if ((putenv(warn) != 0) || (putenv(bind) != 0) || 720 (putenv(path) != 0) || (putenv(verb) != 0) || 721 (putenv(fltr) != 0) || (putenv(conf) != 0) || 722 (putenv(init) != 0) || (putenv(lazy) != 0) || 723 (putenv(uref) != 0) || (putenv(used) != 0) || 724 (putenv(weak) != 0) || (putenv(load) != 0) || 725 (putenv(nope) != 0)) { 726 (void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED), cname); 727 exit(1); 728 } 729 730 /* 731 * Establish explicit environment requires (but don't override 732 * any preload request established to process a shared object). 733 */ 734 size = 0; 735 for (APLIST_TRAVERSE(eopts, idx, str)) { 736 if (preload) { 737 if (size == 0) 738 size = strlen(preload); 739 if ((strncmp(preload, str, size) == 0) && 740 (str[size] == '=')) { 741 continue; 742 } 743 } 744 if (putenv(str) != 0) { 745 (void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED), 746 cname); 747 exit(1); 748 } 749 } 750 751 /* 752 * Execute the object and let ld.so.1 do the rest. 753 */ 754 if (nfile > 1) 755 (void) printf(MSG_ORIG(MSG_STR_FMT3), fname); 756 (void) fflush(stdout); 757 if ((execl(ename, ename, (char *)0)) == -1) { 758 (void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname, 759 fname); 760 perror(ename); 761 _exit(0); 762 /* NOTREACHED */ 763 } 764 } 765 return (status); 766 } 767 768 const char * 769 _ldd_msg(Msg mid) 770 { 771 return (gettext(MSG_ORIG(mid))); 772 } 773