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 22 /* 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Copyright (c) 1988 AT&T 29 * All Rights Reserved 30 */ 31 32 /* 33 * Run time linker common setup. 34 * 35 * Called from _setup to get the process going at startup. 36 */ 37 38 #include <stdlib.h> 39 #include <fcntl.h> 40 #include <stdio.h> 41 #include <sys/types.h> 42 #include <sys/stat.h> 43 #include <sys/mman.h> 44 #include <string.h> 45 #include <stdio.h> 46 #include <unistd.h> 47 #include <dlfcn.h> 48 #include <sys/sysconfig.h> 49 #include <sys/auxv.h> 50 #include <debug.h> 51 #include <conv.h> 52 #include "_rtld.h" 53 #include "_audit.h" 54 #include "_elf.h" 55 #include "_a.out.h" 56 #include "msg.h" 57 58 59 extern int _end, _edata, _etext; 60 extern void _init(void); 61 extern int _brk_unlocked(void *); 62 63 #ifndef SGS_PRE_UNIFIED_PROCESS 64 /* needed for _brk_unlocked() */ 65 void *_nd = &_end; 66 #endif 67 68 /* 69 * Counters that are incremented every time an object is mapped/unmapped. 70 * 71 * Note that exec() will usually map 2 objects before we receive control, 72 * but this can be 1 if ld.so.1 is executed directly. We count one of these 73 * here, and add another as necessary in setup(). 74 */ 75 u_longlong_t cnt_map = 1; 76 u_longlong_t cnt_unmap = 0; 77 78 79 /* 80 * Define for the executable's interpreter. 81 * Usually it is ld.so.1, but for the first release of ICL binaries 82 * it is libc.so.1. We keep this information so that we don't end 83 * up mapping libc twice if it is the interpreter. 84 */ 85 static Interp _interp; 86 87 /* 88 * LD_PRELOAD objects. 89 */ 90 static int 91 preload(const char *str, Rt_map *mlmp, Rt_map **clmp) 92 { 93 Alist *palp = NULL; 94 char *objs, *ptr, *next; 95 Word lmflags = lml_main.lm_flags; 96 int lddstub; 97 98 DBG_CALL(Dbg_util_nl(&lml_main, DBG_NL_STD)); 99 100 if ((objs = strdup(str)) == NULL) 101 return (0); 102 103 /* 104 * Determine if we've been called from lddstub. 105 */ 106 lddstub = (lmflags & LML_FLG_TRC_ENABLE) && 107 (FLAGS1(*clmp) & FL1_RT_LDDSTUB); 108 109 ptr = strtok_r(objs, MSG_ORIG(MSG_STR_DELIMIT), &next); 110 do { 111 Rt_map *nlmp = NULL; 112 uint_t flags; 113 114 DBG_CALL(Dbg_file_preload(&lml_main, ptr)); 115 116 /* 117 * Establish the flags for loading each object. If we're 118 * called via lddstub, then the first preloaded object is the 119 * object being inspected by ldd(1). This object should not be 120 * marked as an interposer, as this object is intended to act 121 * as the target object of the process. 122 */ 123 if (lddstub) 124 flags = FLG_RT_PRELOAD; 125 else 126 flags = (FLG_RT_PRELOAD | FLG_RT_OBJINTPO); 127 128 /* 129 * If this a secure application, then preload errors are 130 * reduced to warnings, as the errors are non-fatal. 131 */ 132 if (rtld_flags & RT_FL_SECURE) 133 rtld_flags2 |= RT_FL2_FTL2WARN; 134 if (expand_paths(*clmp, ptr, &palp, AL_CNT_NEEDED, 135 PD_FLG_EXTLOAD, 0) != 0) 136 nlmp = load_one(&lml_main, ALIST_OFF_DATA, palp, *clmp, 137 MODE(mlmp), flags, 0, NULL); 138 remove_plist(&palp, 0); 139 if (rtld_flags & RT_FL_SECURE) 140 rtld_flags2 &= ~RT_FL2_FTL2WARN; 141 if (nlmp && (bind_one(*clmp, nlmp, BND_NEEDED) == 0)) 142 nlmp = NULL; 143 144 if (lddstub && nlmp) { 145 lddstub = 0; 146 147 /* 148 * Fabricate a binding between the target shared object 149 * and lddstub so that the target object isn't called 150 * out from unused() processing. 151 */ 152 if (lmflags & 153 (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED)) { 154 if (bind_one(*clmp, nlmp, BND_REFER) == 0) 155 nlmp = NULL; 156 } 157 158 /* 159 * By identifying lddstub as the caller, several 160 * confusing ldd() diagnostics get suppressed. These 161 * diagnostics would reveal how the target shared object 162 * was found from lddstub. Now that the real target is 163 * loaded, identify the target as the caller so that all 164 * ldd() diagnostics are enabled for subsequent objects. 165 */ 166 if (nlmp) 167 *clmp = nlmp; 168 } 169 170 /* 171 * If no error occurred with loading this object, indicate that 172 * this link-map list contains an interposer. 173 */ 174 if (nlmp == NULL) { 175 if ((lmflags & LML_FLG_TRC_ENABLE) || 176 (rtld_flags & RT_FL_SECURE)) 177 continue; 178 else 179 return (0); 180 } 181 if (flags & FLG_RT_OBJINTPO) 182 lml_main.lm_flags |= LML_FLG_INTRPOSE; 183 184 } while ((ptr = strtok_r(NULL, 185 MSG_ORIG(MSG_STR_DELIMIT), &next)) != NULL); 186 187 free(palp); 188 free(objs); 189 return (1); 190 } 191 192 Rt_map * 193 setup(char **envp, auxv_t *auxv, Word _flags, char *_platform, int _syspagsz, 194 char *_rtldname, ulong_t ld_base, ulong_t interp_base, int fd, Phdr *phdr, 195 char *execname, char **argv, uid_t uid, uid_t euid, gid_t gid, gid_t egid, 196 void *aoutdyn, int auxflags, uint_t hwcap_1) 197 { 198 Rt_map *rlmp, *mlmp, *clmp, **tobj = NULL; 199 Ehdr *ehdr; 200 rtld_stat_t status; 201 int features = 0, ldsoexec = 0; 202 size_t eaddr, esize; 203 char *str, *argvname; 204 Word lmflags; 205 mmapobj_result_t *mpp; 206 Fdesc fdr = { 0 }, fdm = { 0 }; 207 Rej_desc rej = { 0 }; 208 209 /* 210 * Now that ld.so has relocated itself, initialize our own 'environ' so 211 * as to establish an address suitable for any libc requirements. 212 */ 213 _environ = (char **)((ulong_t)auxv - sizeof (char *)); 214 _init(); 215 _environ = envp; 216 217 /* 218 * Establish a base time. Total time diagnostics start from entering 219 * ld.so.1 here, however the base time is reset each time the ld.so.1 220 * is re-entered. Note also, there will be a large time associated 221 * with the first diagnostic from ld.so.1, as bootstrapping ld.so.1 222 * and establishing the liblddbg infrastructure takes some time. 223 */ 224 (void) gettimeofday(&DBG_TOTALTIME, NULL); 225 DBG_DELTATIME = DBG_TOTALTIME; 226 227 /* 228 * Determine how ld.so.1 has been executed. 229 */ 230 if ((fd == -1) && (phdr == NULL)) { 231 /* 232 * If we received neither the AT_EXECFD nor the AT_PHDR aux 233 * vector, ld.so.1 must have been invoked directly from the 234 * command line. 235 */ 236 ldsoexec = 1; 237 238 /* 239 * AT_SUN_EXECNAME provides the most precise name, if it is 240 * available, otherwise fall back to argv[0]. At this time, 241 * there is no process name. 242 */ 243 if (execname) 244 rtldname = execname; 245 else if (argv[0]) 246 rtldname = argv[0]; 247 else 248 rtldname = (char *)MSG_INTL(MSG_STR_UNKNOWN); 249 } else { 250 /* 251 * Otherwise, we have a standard process. AT_SUN_EXECNAME 252 * provides the most precise name, if it is available, 253 * otherwise fall back to argv[0]. Provided the application 254 * is already mapped, the process is the application, so 255 * simplify the application name for use in any diagnostics. 256 */ 257 if (execname) 258 argvname = execname; 259 else if (argv[0]) 260 argvname = execname = argv[0]; 261 else 262 argvname = execname = (char *)MSG_INTL(MSG_STR_UNKNOWN); 263 264 if (fd == -1) { 265 if ((str = strrchr(argvname, '/')) != NULL) 266 procname = ++str; 267 else 268 procname = argvname; 269 } 270 271 /* 272 * At this point, we don't know the runtime linkers full path 273 * name. The _rtldname passed to us is the SONAME of the 274 * runtime linker, which is typically /lib/ld.so.1 no matter 275 * what the full path is. Use this for now, we'll reset the 276 * runtime linkers name once the application is analyzed. 277 */ 278 if (_rtldname) { 279 if ((str = strrchr(_rtldname, '/')) != NULL) 280 rtldname = ++str; 281 else 282 rtldname = _rtldname; 283 } else 284 rtldname = (char *)MSG_INTL(MSG_STR_UNKNOWN); 285 286 /* exec() brought in two objects for us. Count the second one */ 287 cnt_map++; 288 } 289 290 /* 291 * Initialize any global variables. 292 */ 293 at_flags = _flags; 294 platform = _platform; 295 296 /* 297 * If pagesize is unspecified find its value. 298 */ 299 if ((syspagsz = _syspagsz) == 0) 300 syspagsz = _sysconfig(_CONFIG_PAGESIZE); 301 302 /* 303 * Add the unused portion of the last data page to the free space list. 304 * The page size must be set before doing this. Here, _end refers to 305 * the end of the runtime linkers bss. Note that we do not use the 306 * unused data pages from any included .so's to supplement this free 307 * space as badly behaved .os's may corrupt this data space, and in so 308 * doing ruin our data. 309 */ 310 eaddr = S_DROUND((size_t)&_end); 311 esize = eaddr % syspagsz; 312 if (esize) { 313 esize = syspagsz - esize; 314 addfree((void *)eaddr, esize); 315 } 316 317 /* 318 * Establish initial link-map list flags, and link-map list alists. 319 */ 320 if (alist_append(&lml_main.lm_lists, NULL, sizeof (Lm_cntl), 321 AL_CNT_LMLISTS) == NULL) 322 return (0); 323 lml_main.lm_flags |= LML_FLG_BASELM; 324 lml_main.lm_lmid = LM_ID_BASE; 325 lml_main.lm_lmidstr = (char *)MSG_ORIG(MSG_LMID_BASE); 326 327 if (alist_append(&lml_rtld.lm_lists, NULL, sizeof (Lm_cntl), 328 AL_CNT_LMLISTS) == NULL) 329 return (0); 330 lml_rtld.lm_flags |= (LML_FLG_RTLDLM | LML_FLG_NOAUDIT | 331 LML_FLG_HOLDLOCK); 332 lml_rtld.lm_lmid = LM_ID_LDSO; 333 lml_rtld.lm_lmidstr = (char *)MSG_ORIG(MSG_LMID_LDSO); 334 335 /* 336 * Determine whether we have a secure executable. 337 */ 338 security(uid, euid, gid, egid, auxflags); 339 340 /* 341 * Initialize a hardware capability descriptor for use in comparing 342 * each loaded object. 343 */ 344 if (auxflags & AF_SUN_HWCAPVERIFY) { 345 rtld_flags2 |= RT_FL2_HWCAP; 346 hwcap = (ulong_t)hwcap_1; 347 } 348 349 /* 350 * Look for environment strings (allows things like LD_NOAUDIT to be 351 * established, although debugging isn't enabled until later). 352 */ 353 if ((readenv_user((const char **)envp, &(lml_main.lm_flags), 354 &(lml_main.lm_tflags), (aoutdyn != 0))) == 1) 355 return (0); 356 357 /* 358 * Create a mapping descriptor for ld.so.1. We can determine our 359 * two segments information from known symbols. 360 */ 361 if ((mpp = calloc(2, sizeof (mmapobj_result_t))) == NULL) 362 return (0); 363 mpp[0].mr_addr = (caddr_t)M_PTRUNC(ld_base); 364 mpp[0].mr_msize = (caddr_t)&_etext - mpp[0].mr_addr; 365 mpp[0].mr_fsize = mpp[0].mr_msize; 366 mpp[0].mr_prot = (PROT_READ | PROT_EXEC); 367 368 mpp[1].mr_addr = (caddr_t)M_PTRUNC((uintptr_t)&r_debug); 369 mpp[1].mr_msize = (caddr_t)&_end - mpp[1].mr_addr; 370 mpp[1].mr_fsize = (caddr_t)&_edata - mpp[1].mr_addr; 371 mpp[1].mr_prot = (PROT_READ | PROT_WRITE | PROT_EXEC); 372 373 if ((fdr.fd_nname = stravl_insert(_rtldname, 0, 0, 0)) == NULL) 374 return (0); 375 if ((rlmp = elf_new_lmp(&lml_rtld, ALIST_OFF_DATA, &fdr, 376 (Addr)mpp->mr_addr, (size_t)((uintptr_t)eaddr - (uintptr_t)ld_base), 377 NULL, NULL)) == NULL) 378 return (0); 379 380 MMAPS(rlmp) = mpp; 381 MMAPCNT(rlmp) = 2; 382 PADSTART(rlmp) = (ulong_t)mpp[0].mr_addr; 383 PADIMLEN(rlmp) = (ulong_t)mpp[0].mr_addr + (ulong_t)mpp[1].mr_addr + 384 (ulong_t)mpp[1].mr_msize; 385 386 MODE(rlmp) |= (RTLD_LAZY | RTLD_NODELETE | RTLD_GLOBAL | RTLD_WORLD); 387 FLAGS(rlmp) |= (FLG_RT_ANALYZED | FLG_RT_RELOCED | FLG_RT_INITDONE | 388 FLG_RT_INITCLCT | FLG_RT_FINICLCT | FLG_RT_MODESET); 389 390 /* 391 * Initialize the runtime linkers information. 392 */ 393 interp = &_interp; 394 interp->i_name = (char *)rtldname; 395 interp->i_faddr = (caddr_t)ADDR(rlmp); 396 ldso_plt_init(rlmp); 397 398 /* 399 * If ld.so.1 has been invoked directly, process its arguments. 400 */ 401 if (ldsoexec) { 402 /* 403 * Process any arguments that are specific to ld.so.1, and 404 * reorganize the process stack to effectively remove ld.so.1 405 * from it. Reinitialize the environment pointer, as this may 406 * have been shifted after skipping ld.so.1's arguments. 407 */ 408 if (rtld_getopt(argv, &envp, &auxv, &(lml_main.lm_flags), 409 &(lml_main.lm_tflags), (aoutdyn != 0)) == 1) { 410 eprintf(&lml_main, ERR_NONE, MSG_INTL(MSG_USG_BADOPT)); 411 return (0); 412 } 413 _environ = envp; 414 415 /* 416 * Open the object that ld.so.1 is to execute. 417 */ 418 argvname = execname = argv[0]; 419 420 if ((fd = open(argvname, O_RDONLY)) == -1) { 421 int err = errno; 422 eprintf(&lml_main, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), 423 argvname, strerror(err)); 424 return (0); 425 } 426 } 427 428 /* 429 * Map in the file, if exec has not already done so, or if the file 430 * was passed as an argument to an explicit execution of ld.so.1 from 431 * the command line. 432 */ 433 if (fd != -1) { 434 /* 435 * Map the file. Once the object is mapped we no longer need 436 * the file descriptor. 437 */ 438 (void) rtld_fstat(fd, &status); 439 fdm.fd_ftp = map_obj(&lml_main, &fdm, status.st_size, argvname, 440 fd, &rej); 441 (void) close(fd); 442 443 if (fdm.fd_ftp == NULL) { 444 Conv_reject_desc_buf_t rej_buf; 445 446 eprintf(&lml_main, ERR_FATAL, 447 MSG_INTL(err_reject[rej.rej_type]), argvname, 448 conv_reject_desc(&rej, &rej_buf, M_MACH)); 449 return (0); 450 } 451 452 /* 453 * Finish processing the loading of the file. 454 */ 455 if ((fdm.fd_nname = stravl_insert(argvname, 0, 0, 0)) == NULL) 456 return (0); 457 fdm.fd_dev = status.st_dev; 458 fdm.fd_ino = status.st_ino; 459 460 if ((mlmp = load_file(&lml_main, ALIST_OFF_DATA, &fdm, 461 NULL)) == NULL) 462 return (0); 463 464 /* 465 * We now have a process name for error diagnostics. 466 */ 467 if ((str = strrchr(argvname, '/')) != NULL) 468 procname = ++str; 469 else 470 procname = argvname; 471 472 if (ldsoexec) { 473 mmapobj_result_t *mpp = MMAPS(mlmp); 474 uint_t mnum, mapnum = MMAPCNT(mlmp); 475 void *brkbase = NULL; 476 477 /* 478 * Since ld.so.1 was the primary executed object - the 479 * brk() base has not yet been initialized, we need to 480 * initialize it. For an executable, initialize it to 481 * the end of the object. For a shared object (ET_DYN) 482 * initialize it to the first page in memory. 483 */ 484 for (mnum = 0; mnum < mapnum; mnum++, mpp++) 485 brkbase = mpp->mr_addr + mpp->mr_msize; 486 487 if (brkbase == NULL) 488 brkbase = (void *)syspagsz; 489 490 if (_brk_unlocked(brkbase) == -1) { 491 int err = errno; 492 493 eprintf(&lml_main, ERR_FATAL, 494 MSG_INTL(MSG_SYS_BRK), argvname, 495 strerror(err)); 496 return (0); 497 } 498 } 499 } else { 500 /* 501 * Set up function ptr and arguments according to the type 502 * of file class the executable is. (Currently only supported 503 * types are ELF and a.out format.) Then create a link map 504 * for the executable. 505 */ 506 if (aoutdyn) { 507 #ifdef A_OUT 508 mmapobj_result_t *mpp; 509 510 /* 511 * Create a mapping structure sufficient to describe 512 * a single two segments. The ADDR() of the a.out is 513 * established as 0, which is required but the AOUT 514 * relocation code. 515 */ 516 if ((mpp = 517 calloc(sizeof (mmapobj_result_t), 2)) == NULL) 518 return (0); 519 520 if ((fdm.fd_nname = 521 stravl_insert(execname, 0, 0, 0)) == NULL) 522 return (0); 523 if ((mlmp = aout_new_lmp(&lml_main, ALIST_OFF_DATA, 524 &fdm, 0, 0, aoutdyn, NULL)) == NULL) 525 return (0); 526 527 /* 528 * Establish the true mapping information for the a.out. 529 */ 530 if (aout_get_mmap(&lml_main, mpp)) { 531 free(mpp); 532 return (0); 533 } 534 535 MSIZE(mlmp) = 536 (size_t)(mpp[1].mr_addr + mpp[1].mr_msize) - 537 S_ALIGN((size_t)mpp[0].mr_addr, syspagsz); 538 MMAPS(mlmp) = mpp; 539 MMAPCNT(mlmp) = 2; 540 PADSTART(mlmp) = (ulong_t)mpp->mr_addr; 541 PADIMLEN(mlmp) = mpp->mr_msize; 542 543 /* 544 * Disable any object configuration cache (BCP apps 545 * bring in sbcp which can benefit from any object 546 * cache, but both the app and sbcp can't use the same 547 * objects). 548 */ 549 rtld_flags |= RT_FL_NOOBJALT; 550 551 /* 552 * Make sure no-direct bindings are in effect. 553 */ 554 lml_main.lm_tflags |= LML_TFLG_NODIRECT; 555 #else 556 eprintf(&lml_main, ERR_FATAL, 557 MSG_INTL(MSG_ERR_REJ_UNKFILE), argvname); 558 return (0); 559 #endif 560 } else if (phdr) { 561 Phdr *pptr; 562 Off i_offset = 0; 563 Addr base = 0; 564 ulong_t phsize; 565 mmapobj_result_t *mpp, *fmpp, *hmpp = NULL; 566 uint_t mapnum = 0; 567 int i; 568 size_t msize; 569 570 /* 571 * Using the executables phdr address determine the base 572 * address of the input file. NOTE, this assumes the 573 * program headers and elf header are part of the same 574 * mapped segment. Although this has held for many 575 * years now, it might be more flexible if the kernel 576 * gave use the ELF headers start address, rather than 577 * the Program headers. 578 * 579 * Determine from the ELF header if we're been called 580 * from a shared object or dynamic executable. If the 581 * latter, then any addresses within the object are used 582 * as is. Addresses within shared objects must be added 583 * to the process's base address. 584 */ 585 ehdr = (Ehdr *)((Addr)phdr - phdr->p_offset); 586 phsize = ehdr->e_phentsize; 587 if (ehdr->e_type == ET_DYN) 588 base = (Addr)ehdr; 589 590 /* 591 * Allocate a mapping array to retain mapped segment 592 * information. 593 */ 594 if ((fmpp = mpp = calloc(ehdr->e_phnum, 595 sizeof (mmapobj_result_t))) == NULL) 596 return (0); 597 598 /* 599 * Extract the needed information from the segment 600 * headers. 601 */ 602 for (i = 0, pptr = phdr; i < ehdr->e_phnum; i++) { 603 if (pptr->p_type == PT_INTERP) { 604 i_offset = pptr->p_offset; 605 interp->i_faddr = 606 (caddr_t)interp_base; 607 } 608 if ((pptr->p_type == PT_LOAD) && 609 (pptr->p_filesz || pptr->p_memsz)) { 610 int perm = (PROT_READ | PROT_EXEC); 611 size_t off; 612 613 if (i_offset && pptr->p_filesz && 614 (i_offset >= pptr->p_offset) && 615 (i_offset <= 616 (pptr->p_memsz + pptr->p_offset))) { 617 interp->i_name = (char *) 618 pptr->p_vaddr + i_offset - 619 pptr->p_offset + base; 620 i_offset = 0; 621 } 622 623 if (pptr->p_flags & PF_W) 624 perm |= PROT_WRITE; 625 626 /* 627 * Retain segments mapping info. Round 628 * each segment to a page boundary, as 629 * this insures addresses are suitable 630 * for mprotect() if required. 631 */ 632 off = pptr->p_vaddr + base; 633 if (hmpp == NULL) { 634 hmpp = mpp; 635 mpp->mr_addr = (caddr_t)ehdr; 636 } else 637 mpp->mr_addr = (caddr_t)off; 638 639 off -= (size_t)(uintptr_t)mpp->mr_addr; 640 mpp->mr_msize = pptr->p_memsz + off; 641 mpp->mr_fsize = pptr->p_filesz + off; 642 mpp->mr_prot = perm; 643 644 mpp++, mapnum++; 645 } 646 647 pptr = (Phdr *)((ulong_t)pptr + phsize); 648 } 649 650 mpp--; 651 msize = (size_t)(mpp->mr_addr + mpp->mr_msize) - 652 S_ALIGN((size_t)fmpp->mr_addr, syspagsz); 653 654 if ((fdm.fd_nname = 655 stravl_insert(execname, 0, 0, 0)) == NULL) 656 return (0); 657 if ((mlmp = elf_new_lmp(&lml_main, ALIST_OFF_DATA, &fdm, 658 (Addr)hmpp->mr_addr, msize, NULL, NULL)) == NULL) 659 return (0); 660 661 MMAPS(mlmp) = fmpp; 662 MMAPCNT(mlmp) = mapnum; 663 PADSTART(mlmp) = (ulong_t)fmpp->mr_addr; 664 PADIMLEN(mlmp) = (ulong_t)fmpp->mr_addr + 665 (ulong_t)mpp->mr_addr + (ulong_t)mpp->mr_msize; 666 } 667 } 668 669 /* 670 * Establish the interpretors name as that defined within the initial 671 * object (executable). This provides for ORIGIN processing of ld.so.1 672 * dependencies. Note, the NAME() of the object remains that which was 673 * passed to us as the SONAME on execution. 674 */ 675 if (ldsoexec == 0) { 676 size_t len = strlen(interp->i_name); 677 678 if (expand(&interp->i_name, &len, 0, 0, 679 (PD_TKN_ISALIST | PD_TKN_HWCAP), rlmp) & PD_TKN_RESOLVED) 680 fdr.fd_flags |= FLG_FD_RESOLVED; 681 } 682 fdr.fd_pname = interp->i_name; 683 (void) fullpath(rlmp, &fdr); 684 685 /* 686 * The runtime linker acts as a filtee for various dl*() functions that 687 * are defined in libc (and libdl). Make sure this standard name for 688 * the runtime linker is also registered in the FullPathNode AVL tree. 689 */ 690 (void) fpavl_insert(&lml_rtld, rlmp, _rtldname, 0); 691 692 /* 693 * Having established the true runtime linkers name, simplify the name 694 * for error diagnostics. 695 */ 696 if ((str = strrchr(PATHNAME(rlmp), '/')) != NULL) 697 rtldname = ++str; 698 else 699 rtldname = PATHNAME(rlmp); 700 701 /* 702 * Expand the fullpath name of the application. This typically occurs 703 * as a part of loading an object, but as the kernel probably mapped 704 * it in, complete this processing now. 705 */ 706 (void) fullpath(mlmp, 0); 707 708 /* 709 * Some troublesome programs will change the value of argv[0]. Dupping 710 * the process string protects us, and insures the string is left in 711 * any core files. 712 */ 713 if ((str = (char *)strdup(procname)) == NULL) 714 return (0); 715 procname = str; 716 717 #if defined(_ELF64) 718 /* 719 * If this is a 64-bit process, determine whether this process has 720 * restricted the process address space to 32-bits. Any dependencies 721 * that are restricted to a 32-bit address space can only be loaded if 722 * the executable has established this requirement. 723 */ 724 if (SFCAP(mlmp) & SF1_SUNW_ADDR32) 725 rtld_flags2 |= RT_FL2_ADDR32; 726 #endif 727 /* 728 * Validate any hardware capabilities information. 729 */ 730 if (HWCAP(mlmp) && (hwcap_check(HWCAP(mlmp), &rej) == 0)) { 731 if (lml_main.lm_flags & LML_FLG_TRC_ENABLE) { 732 (void) printf(MSG_INTL(MSG_LDD_GEN_HWCAP_1), 733 NAME(mlmp), rej.rej_str); 734 } else { 735 eprintf(&lml_main, ERR_FATAL, 736 MSG_INTL(MSG_GEN_BADHWCAP_1), rej.rej_str); 737 return (0); 738 } 739 } 740 741 /* 742 * Validate any software capabilities information, other than 743 * SF1_SUNW_ADDR32. Only dependencies need check their SF1_SUNW_ADDR32 744 * use against the application enabling a 32-bit address space. 745 */ 746 if ((SFCAP(mlmp) & ~SF1_SUNW_ADDR32) && 747 (sfcap_check(SFCAP(mlmp), &rej) == 0)) { 748 if (lml_main.lm_flags & LML_FLG_TRC_ENABLE) { 749 (void) printf(MSG_INTL(MSG_LDD_GEN_SFCAP_1), 750 NAME(mlmp), rej.rej_str); 751 } else { 752 eprintf(&lml_main, ERR_FATAL, 753 MSG_INTL(MSG_GEN_BADSFCAP_1), rej.rej_str); 754 return (0); 755 } 756 } 757 758 FLAGS(mlmp) |= (FLG_RT_ISMAIN | FLG_RT_MODESET); 759 FLAGS1(mlmp) |= FL1_RT_USED; 760 761 /* 762 * It's the responsibility of MAIN(crt0) to call it's _init and _fini 763 * section, therefore null out any INIT/FINI so that this object isn't 764 * collected during tsort processing. And, if the application has no 765 * initarray or finiarray we can economize on establishing bindings. 766 */ 767 INIT(mlmp) = FINI(mlmp) = NULL; 768 if ((INITARRAY(mlmp) == NULL) && (FINIARRAY(mlmp) == NULL)) 769 FLAGS1(mlmp) |= FL1_RT_NOINIFIN; 770 771 /* 772 * Identify lddstub if necessary. 773 */ 774 if (lml_main.lm_flags & LML_FLG_TRC_LDDSTUB) 775 FLAGS1(mlmp) |= FL1_RT_LDDSTUB; 776 777 /* 778 * Retain our argument information for use in dlinfo. 779 */ 780 argsinfo.dla_argv = argv--; 781 argsinfo.dla_argc = (long)*argv; 782 argsinfo.dla_envp = envp; 783 argsinfo.dla_auxv = auxv; 784 785 (void) enter(0); 786 787 /* 788 * Add our two main link-maps to the dynlm_list 789 */ 790 if (aplist_append(&dynlm_list, &lml_main, AL_CNT_DYNLIST) == NULL) 791 return (0); 792 793 if (aplist_append(&dynlm_list, &lml_rtld, AL_CNT_DYNLIST) == NULL) 794 return (0); 795 796 /* 797 * Reset the link-map counts for both lists. The init count is used to 798 * track how many objects have pending init sections, this gets incre- 799 * mented each time an object is relocated. Since ld.so.1 relocates 800 * itself, it's init count will remain zero. 801 * The object count is used to track how many objects have pending fini 802 * sections, as ld.so.1 handles its own fini we can zero its count. 803 */ 804 lml_main.lm_obj = 1; 805 lml_rtld.lm_obj = 0; 806 807 /* 808 * Initialize debugger information structure. Some parts of this 809 * structure were initialized statically. 810 */ 811 r_debug.rtd_rdebug.r_map = (Link_map *)lml_main.lm_head; 812 r_debug.rtd_rdebug.r_ldsomap = (Link_map *)lml_rtld.lm_head; 813 r_debug.rtd_rdebug.r_ldbase = r_debug.rtd_rdebug.r_ldsomap->l_addr; 814 r_debug.rtd_dynlmlst = &dynlm_list; 815 816 if (platform) 817 platform_sz = strlen(platform); 818 819 /* 820 * Determine the dev/inode information for the executable to complete 821 * load_so() checking for those who might dlopen(a.out). 822 */ 823 if (rtld_stat(PATHNAME(mlmp), &status) == 0) { 824 STDEV(mlmp) = status.st_dev; 825 STINO(mlmp) = status.st_ino; 826 } 827 828 /* 829 * Initialize any configuration information. 830 */ 831 if (!(rtld_flags & RT_FL_NOCFG)) { 832 if ((features = elf_config(mlmp, (aoutdyn != 0))) == -1) 833 return (0); 834 } 835 836 /* 837 * Establish the modes of the initial object. These modes are 838 * propagated to any preloaded objects and explicit shared library 839 * dependencies. 840 * 841 * If we're generating a configuration file using crle(1), remove 842 * any RTLD_NOW use, as we don't want to trigger any relocation proc- 843 * essing during crle(1)'s first past (this would just be unnecessary 844 * overhead). Any filters are explicitly loaded, and thus RTLD_NOW is 845 * not required to trigger filter loading. 846 * 847 * Note, RTLD_NOW may have been established during analysis of the 848 * application had the application been built -z now. 849 */ 850 MODE(mlmp) |= (RTLD_NODELETE | RTLD_GLOBAL | RTLD_WORLD); 851 852 if (rtld_flags & RT_FL_CONFGEN) { 853 MODE(mlmp) |= RTLD_CONFGEN; 854 MODE(mlmp) &= ~RTLD_NOW; 855 rtld_flags2 &= ~RT_FL2_BINDNOW; 856 } 857 858 if ((MODE(mlmp) & RTLD_NOW) == 0) { 859 if (rtld_flags2 & RT_FL2_BINDNOW) 860 MODE(mlmp) |= RTLD_NOW; 861 else 862 MODE(mlmp) |= RTLD_LAZY; 863 } 864 865 /* 866 * If debugging was requested initialize things now that any cache has 867 * been established. A user can specify LD_DEBUG=help to discover the 868 * list of debugging tokens available without running the application. 869 * However, don't allow this setting from a configuration file. 870 * 871 * Note, to prevent recursion issues caused by loading and binding the 872 * debugging libraries themselves, a local debugging descriptor is 873 * initialized. Once the debugging setup has completed, this local 874 * descriptor is copied to the global descriptor which effectively 875 * enables diagnostic output. 876 * 877 * Ignore any debugging request if we're being monitored by a process 878 * that expects the old getpid() initialization handshake. 879 */ 880 if ((rpl_debug || prm_debug) && ((rtld_flags & RT_FL_DEBUGGER) == 0)) { 881 Dbg_desc _dbg_desc = {0}; 882 struct timeval total = DBG_TOTALTIME; 883 struct timeval delta = DBG_DELTATIME; 884 885 if (rpl_debug) { 886 if (dbg_setup(rpl_debug, &_dbg_desc) == 0) 887 return (0); 888 if (_dbg_desc.d_extra & DBG_E_HELP_EXIT) 889 rtldexit(&lml_main, 0); 890 } 891 if (prm_debug) 892 (void) dbg_setup(prm_debug, &_dbg_desc); 893 894 *dbg_desc = _dbg_desc; 895 DBG_TOTALTIME = total; 896 DBG_DELTATIME = delta; 897 } 898 899 /* 900 * Now that debugging is enabled generate any diagnostics from any 901 * previous events. 902 */ 903 if (hwcap) 904 DBG_CALL(Dbg_cap_val_hw1(&lml_main, hwcap, M_MACH)); 905 if (features) 906 DBG_CALL(Dbg_file_config_dis(&lml_main, config->c_name, 907 features)); 908 909 if (DBG_ENABLED) { 910 DBG_CALL(Dbg_file_ldso(rlmp, envp, auxv, 911 LIST(rlmp)->lm_lmidstr, ALIST_OFF_DATA)); 912 913 if (THIS_IS_ELF(mlmp)) { 914 DBG_CALL(Dbg_file_elf(&lml_main, PATHNAME(mlmp), 915 ADDR(mlmp), MSIZE(mlmp), LIST(mlmp)->lm_lmidstr, 916 ALIST_OFF_DATA)); 917 } else { 918 DBG_CALL(Dbg_file_aout(&lml_main, PATHNAME(mlmp), 919 ADDR(mlmp), MSIZE(mlmp), LIST(mlmp)->lm_lmidstr, 920 ALIST_OFF_DATA)); 921 } 922 } 923 924 /* 925 * Enable auditing. 926 */ 927 if (rpl_audit || prm_audit || profile_lib) { 928 int ndx; 929 const char *aud[3]; 930 931 aud[0] = rpl_audit; 932 aud[1] = prm_audit; 933 aud[2] = profile_lib; 934 935 /* 936 * Any global auditing (set using LD_AUDIT or LD_PROFILE) that 937 * can't be established is non-fatal. 938 */ 939 if ((auditors = calloc(1, sizeof (Audit_desc))) == NULL) 940 return (0); 941 942 for (ndx = 0; ndx < 3; ndx++) { 943 if (aud[ndx]) { 944 if ((auditors->ad_name = 945 strdup(aud[ndx])) == NULL) 946 return (0); 947 rtld_flags2 |= RT_FL2_FTL2WARN; 948 (void) audit_setup(mlmp, auditors, 949 PD_FLG_EXTLOAD, NULL); 950 rtld_flags2 &= ~RT_FL2_FTL2WARN; 951 } 952 } 953 lml_main.lm_tflags |= auditors->ad_flags; 954 } 955 if (AUDITORS(mlmp)) { 956 /* 957 * Any object required auditing (set with a DT_DEPAUDIT dynamic 958 * entry) that can't be established is fatal. 959 */ 960 if (FLAGS1(mlmp) & FL1_RT_GLOBAUD) { 961 /* 962 * If this object requires global auditing, use the 963 * local auditing information to set the global 964 * auditing descriptor. The effect is that a 965 * DT_DEPAUDIT act as an LD_AUDIT. 966 */ 967 if ((auditors == NULL) && ((auditors = calloc(1, 968 sizeof (Audit_desc))) == NULL)) 969 return (0); 970 971 auditors->ad_name = AUDITORS(mlmp)->ad_name; 972 if (audit_setup(mlmp, auditors, 0, NULL) == 0) 973 return (0); 974 lml_main.lm_tflags |= auditors->ad_flags; 975 976 /* 977 * Clear the local auditor information. 978 */ 979 free((void *) AUDITORS(mlmp)); 980 AUDITORS(mlmp) = NULL; 981 982 } else { 983 /* 984 * Establish any local auditing. 985 */ 986 if (audit_setup(mlmp, AUDITORS(mlmp), 0, NULL) == 0) 987 return (0); 988 989 AFLAGS(mlmp) |= AUDITORS(mlmp)->ad_flags; 990 lml_main.lm_flags |= LML_FLG_LOCAUDIT; 991 } 992 } 993 994 /* 995 * Explicitly add the initial object and ld.so.1 to those objects being 996 * audited. Note, although the ld.so.1 link-map isn't auditable, 997 * establish a cookie for ld.so.1 as this may be bound to via the 998 * dl*() family. 999 */ 1000 if ((lml_main.lm_tflags | AFLAGS(mlmp)) & LML_TFLG_AUD_MASK) { 1001 if (((audit_objopen(mlmp, mlmp) == 0) || 1002 (audit_objopen(mlmp, rlmp) == 0)) && 1003 (AFLAGS(mlmp) & LML_TFLG_AUD_MASK)) 1004 return (0); 1005 } 1006 1007 /* 1008 * Map in any preloadable shared objects. Establish the caller as the 1009 * head of the main link-map list. In the case of being exercised from 1010 * lddstub, the caller gets reassigned to the first target shared object 1011 * so as to provide intuitive diagnostics from ldd(). 1012 * 1013 * Note, it is valid to preload a 4.x shared object with a 5.0 1014 * executable (or visa-versa), as this functionality is required by 1015 * ldd(1). 1016 */ 1017 clmp = mlmp; 1018 if (rpl_preload && (preload(rpl_preload, mlmp, &clmp) == 0)) 1019 return (0); 1020 if (prm_preload && (preload(prm_preload, mlmp, &clmp) == 0)) 1021 return (0); 1022 1023 /* 1024 * Load all dependent (needed) objects. 1025 */ 1026 if (analyze_lmc(&lml_main, ALIST_OFF_DATA, mlmp, NULL) == NULL) 1027 return (0); 1028 1029 /* 1030 * Relocate all the dependencies we've just added. 1031 * 1032 * If this process has been established via crle(1), the environment 1033 * variable LD_CONFGEN will have been set. crle(1) may create this 1034 * process twice. The first time crle only needs to gather dependency 1035 * information. The second time, is to dldump() the images. 1036 * 1037 * If we're only gathering dependencies, relocation is unnecessary. 1038 * As crle(1) may be building an arbitrary family of objects, they may 1039 * not fully relocate either. Hence the relocation phase is not carried 1040 * out now, but will be called by crle(1) once all objects have been 1041 * loaded. 1042 */ 1043 if ((rtld_flags & RT_FL_CONFGEN) == 0) { 1044 1045 DBG_CALL(Dbg_util_nl(&lml_main, DBG_NL_STD)); 1046 1047 if (relocate_lmc(&lml_main, ALIST_OFF_DATA, mlmp, 1048 mlmp, NULL) == 0) 1049 return (0); 1050 1051 /* 1052 * Inform the debuggers that basic process initialization is 1053 * complete, and that the state of ld.so.1 (link-map lists, 1054 * etc.) is stable. This handshake enables the debugger to 1055 * initialize themselves, and consequently allows the user to 1056 * set break points in .init code. 1057 * 1058 * Most new debuggers use librtld_db to monitor activity events. 1059 * Older debuggers indicated their presence by setting the 1060 * DT_DEBUG entry in the dynamic executable (see elf_new_lm()). 1061 * In this case, getpid() is called so that the debugger can 1062 * catch the system call. This old mechanism has some 1063 * restrictions, as getpid() should not be called prior to 1064 * basic process initialization being completed. This 1065 * restriction has become increasingly difficult to maintain, 1066 * as the use of auditors, LD_DEBUG, and the initialization 1067 * handshake with libc can result in "premature" getpid() 1068 * calls. The use of this getpid() handshake is expected to 1069 * disappear at some point in the future, and there is intent 1070 * to work towards that goal. 1071 */ 1072 rd_event(&lml_main, RD_DLACTIVITY, RT_CONSISTENT); 1073 rd_event(&lml_rtld, RD_DLACTIVITY, RT_CONSISTENT); 1074 1075 if (rtld_flags & RT_FL_DEBUGGER) { 1076 r_debug.rtd_rdebug.r_flags |= RD_FL_ODBG; 1077 (void) getpid(); 1078 } 1079 } 1080 1081 /* 1082 * Indicate preinit activity, and call any auditing routines. These 1083 * routines are called before initializing any threads via libc, or 1084 * before collecting the complete set of .inits on the primary link-map. 1085 * Although most libc interfaces are encapsulated in local routines 1086 * within libc, they have been known to escape (ie. call a .plt). As 1087 * the appcert auditor uses preinit as a trigger to establish some 1088 * external interfaces to the main link-maps libc, we need to activate 1089 * this trigger before exercising any code within libc. Additionally, 1090 * I wouldn't put it past an auditor to add additional objects to the 1091 * primary link-map. Hence, we collect .inits after the audit call. 1092 */ 1093 rd_event(&lml_main, RD_PREINIT, 0); 1094 1095 if ((lml_main.lm_tflags | AFLAGS(mlmp)) & LML_TFLG_AUD_ACTIVITY) 1096 audit_activity(mlmp, LA_ACT_CONSISTENT); 1097 if ((lml_main.lm_tflags | AFLAGS(mlmp)) & LML_TFLG_AUD_PREINIT) 1098 audit_preinit(mlmp); 1099 1100 /* 1101 * If we're creating initial configuration information, we're done 1102 * now that the auditing step has been called. 1103 */ 1104 if (rtld_flags & RT_FL_CONFGEN) { 1105 leave(LIST(mlmp), 0); 1106 return (mlmp); 1107 } 1108 1109 /* 1110 * Sort the .init sections of all objects we've added. If we're 1111 * tracing we only need to execute this under ldd(1) with the -i or -u 1112 * options. 1113 */ 1114 lmflags = lml_main.lm_flags; 1115 if (((lmflags & LML_FLG_TRC_ENABLE) == 0) || 1116 (lmflags & (LML_FLG_TRC_INIT | LML_FLG_TRC_UNREF))) { 1117 if ((tobj = tsort(mlmp, LIST(mlmp)->lm_init, 1118 RT_SORT_REV)) == (Rt_map **)S_ERROR) 1119 return (0); 1120 } 1121 1122 /* 1123 * If we are tracing we're done. This is the one legitimate use of a 1124 * direct call to rtldexit() rather than return, as we don't want to 1125 * return and jump to the application. 1126 */ 1127 if (lmflags & LML_FLG_TRC_ENABLE) { 1128 unused(&lml_main); 1129 rtldexit(&lml_main, 0); 1130 } 1131 1132 /* 1133 * Check if this instance of the linker should have a primary link 1134 * map. This flag allows multiple copies of the -same- -version- 1135 * of the linker (and libc) to run in the same address space. 1136 * 1137 * Without this flag we only support one copy of the linker in a 1138 * process because by default the linker will always try to 1139 * initialize at one primary link map The copy of libc which is 1140 * initialized on a primary link map will initalize global TLS 1141 * data which can be shared with other copies of libc in the 1142 * process. The problem is that if there is more than one copy 1143 * of the linker, only one copy should link libc onto a primary 1144 * link map, otherwise libc will attempt to re-initialize global 1145 * TLS data. So when a copy of the linker is loaded with this 1146 * flag set, it will not initialize any primary link maps since 1147 * persumably another copy of the linker will do this. 1148 * 1149 * Note that this flag only allows multiple copies of the -same- 1150 * -version- of the linker (and libc) to coexist. This approach 1151 * will not work if we are trying to load different versions of 1152 * the linker and libc into the same process. The reason for 1153 * this is that the format of the global TLS data may not be 1154 * the same for different versions of libc. In this case each 1155 * different version of libc must have it's own primary link map 1156 * and be able to maintain it's own TLS data. The only way this 1157 * can be done is by carefully managing TLS pointers on transitions 1158 * between code associated with each of the different linkers. 1159 * Note that this is actually what is done for processes in lx 1160 * branded zones. Although in the lx branded zone case, the 1161 * other linker and libc are actually gld and glibc. But the 1162 * same general TLS management mechanism used by the lx brand 1163 * would apply to any attempts to run multiple versions of the 1164 * solaris linker and libc in a single process. 1165 */ 1166 if (auxflags & AF_SUN_NOPLM) 1167 rtld_flags2 |= RT_FL2_NOPLM; 1168 1169 /* 1170 * Establish any static TLS for this primary link-map. Note, regardless 1171 * of whether TLS is available, an initial handshake occurs with libc to 1172 * indicate we're processing the primary link-map. Having identified 1173 * the primary link-map, initialize threads. 1174 */ 1175 if (rt_get_extern(&lml_main, mlmp) == 0) 1176 return (0); 1177 1178 if ((rtld_flags2 & RT_FL2_NOPLM) == 0) { 1179 if (tls_statmod(&lml_main, mlmp) == 0) 1180 return (0); 1181 rt_thr_init(&lml_main); 1182 rtld_flags2 |= RT_FL2_PLMSETUP; 1183 } else { 1184 rt_thr_init(&lml_main); 1185 } 1186 1187 rtld_flags |= RT_FL_APPLIC; 1188 1189 /* 1190 * Fire all dependencies .init sections. Identify any unused 1191 * dependencies, and leave the runtime linker - effectively calling 1192 * the dynamic executables entry point. 1193 */ 1194 call_array(PREINITARRAY(mlmp), (uint_t)PREINITARRAYSZ(mlmp), mlmp, 1195 SHT_PREINIT_ARRAY); 1196 1197 if (tobj) 1198 call_init(tobj, DBG_INIT_SORT); 1199 1200 rd_event(&lml_main, RD_POSTINIT, 0); 1201 1202 unused(&lml_main); 1203 1204 DBG_CALL(Dbg_util_call_main(mlmp)); 1205 1206 rtld_flags |= RT_FL_OPERATION; 1207 leave(LIST(mlmp), 0); 1208 1209 return (mlmp); 1210 } 1211