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