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