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 if (dbg_setup(rpl_debug, &_dbg_desc) == 0) 862 return (0); 863 if (_dbg_desc.d_extra & DBG_E_HELP_EXIT) 864 rtldexit(&lml_main, 0); 865 } 866 if (prm_debug) 867 (void) dbg_setup(prm_debug, &_dbg_desc); 868 869 *dbg_desc = _dbg_desc; 870 } 871 872 /* 873 * Now that debugging is enabled generate any diagnostics from any 874 * previous events. 875 */ 876 if (hwcap) 877 DBG_CALL(Dbg_cap_val_hw1(&lml_main, hwcap, M_MACH)); 878 if (features) 879 DBG_CALL(Dbg_file_config_dis(&lml_main, config->c_name, 880 features)); 881 882 if (DBG_ENABLED) { 883 DBG_CALL(Dbg_file_ldso(rlmp, envp, auxv, 884 LIST(rlmp)->lm_lmidstr, ALIST_OFF_DATA)); 885 886 if (THIS_IS_ELF(mlmp)) { 887 DBG_CALL(Dbg_file_elf(&lml_main, PATHNAME(mlmp), 888 ADDR(mlmp), MSIZE(mlmp), LIST(mlmp)->lm_lmidstr, 889 ALIST_OFF_DATA)); 890 } else { 891 DBG_CALL(Dbg_file_aout(&lml_main, PATHNAME(mlmp), 892 ADDR(mlmp), MSIZE(mlmp), LIST(mlmp)->lm_lmidstr, 893 ALIST_OFF_DATA)); 894 } 895 } 896 897 /* 898 * Enable auditing. 899 */ 900 if (rpl_audit || prm_audit || profile_lib) { 901 int ndx; 902 const char *aud[3]; 903 904 aud[0] = rpl_audit; 905 aud[1] = prm_audit; 906 aud[2] = profile_lib; 907 908 /* 909 * Any global auditing (set using LD_AUDIT or LD_PROFILE) that 910 * can't be established is non-fatal. 911 */ 912 if ((auditors = calloc(1, sizeof (Audit_desc))) == NULL) 913 return (0); 914 915 for (ndx = 0; ndx < 3; ndx++) { 916 if (aud[ndx]) { 917 if ((auditors->ad_name = 918 strdup(aud[ndx])) == NULL) 919 return (0); 920 rtld_flags2 |= RT_FL2_FTL2WARN; 921 (void) audit_setup(mlmp, auditors, 922 PD_FLG_EXTLOAD, NULL); 923 rtld_flags2 &= ~RT_FL2_FTL2WARN; 924 } 925 } 926 lml_main.lm_tflags |= auditors->ad_flags; 927 } 928 if (AUDITORS(mlmp)) { 929 /* 930 * Any object required auditing (set with a DT_DEPAUDIT dynamic 931 * entry) that can't be established is fatal. 932 */ 933 if (FLAGS1(mlmp) & FL1_RT_GLOBAUD) { 934 /* 935 * If this object requires global auditing, use the 936 * local auditing information to set the global 937 * auditing descriptor. The effect is that a 938 * DT_DEPAUDIT act as an LD_AUDIT. 939 */ 940 if ((auditors == NULL) && ((auditors = calloc(1, 941 sizeof (Audit_desc))) == NULL)) 942 return (0); 943 944 auditors->ad_name = AUDITORS(mlmp)->ad_name; 945 if (audit_setup(mlmp, auditors, 0, NULL) == 0) 946 return (0); 947 lml_main.lm_tflags |= auditors->ad_flags; 948 949 /* 950 * Clear the local auditor information. 951 */ 952 free((void *) AUDITORS(mlmp)); 953 AUDITORS(mlmp) = NULL; 954 955 } else { 956 /* 957 * Establish any local auditing. 958 */ 959 if (audit_setup(mlmp, AUDITORS(mlmp), 0, NULL) == 0) 960 return (0); 961 962 AFLAGS(mlmp) |= AUDITORS(mlmp)->ad_flags; 963 lml_main.lm_flags |= LML_FLG_LOCAUDIT; 964 } 965 } 966 967 /* 968 * Explicitly add the initial object and ld.so.1 to those objects being 969 * audited. Note, although the ld.so.1 link-map isn't auditable, 970 * establish a cookie for ld.so.1 as this may be bound to via the 971 * dl*() family. 972 */ 973 if ((lml_main.lm_tflags | AFLAGS(mlmp)) & LML_TFLG_AUD_MASK) { 974 if (((audit_objopen(mlmp, mlmp) == 0) || 975 (audit_objopen(mlmp, rlmp) == 0)) && 976 (AFLAGS(mlmp) & LML_TFLG_AUD_MASK)) 977 return (0); 978 } 979 980 /* 981 * Map in any preloadable shared objects. Establish the caller as the 982 * head of the main link-map list. In the case of being exercised from 983 * lddstub, the caller gets reassigned to the first target shared object 984 * so as to provide intuitive diagnostics from ldd(). 985 * 986 * Note, it is valid to preload a 4.x shared object with a 5.0 987 * executable (or visa-versa), as this functionality is required by 988 * ldd(1). 989 */ 990 clmp = mlmp; 991 if (rpl_preload && (preload(rpl_preload, mlmp, &clmp) == 0)) 992 return (0); 993 if (prm_preload && (preload(prm_preload, mlmp, &clmp) == 0)) 994 return (0); 995 996 /* 997 * Load all dependent (needed) objects. 998 */ 999 if (analyze_lmc(&lml_main, ALIST_OFF_DATA, mlmp, NULL) == NULL) 1000 return (0); 1001 1002 /* 1003 * Relocate all the dependencies we've just added. 1004 * 1005 * If this process has been established via crle(1), the environment 1006 * variable LD_CONFGEN will have been set. crle(1) may create this 1007 * process twice. The first time crle only needs to gather dependency 1008 * information. The second time, is to dldump() the images. 1009 * 1010 * If we're only gathering dependencies, relocation is unnecessary. 1011 * As crle(1) may be building an arbitrary family of objects, they may 1012 * not fully relocate either. Hence the relocation phase is not carried 1013 * out now, but will be called by crle(1) once all objects have been 1014 * loaded. 1015 */ 1016 if ((rtld_flags & RT_FL_CONFGEN) == 0) { 1017 1018 DBG_CALL(Dbg_util_nl(&lml_main, DBG_NL_STD)); 1019 1020 if (relocate_lmc(&lml_main, ALIST_OFF_DATA, mlmp, 1021 mlmp, NULL) == 0) 1022 return (0); 1023 1024 /* 1025 * Inform the debuggers that basic process initialization is 1026 * complete, and that the state of ld.so.1 (link-map lists, 1027 * etc.) is stable. This handshake enables the debugger to 1028 * initialize themselves, and consequently allows the user to 1029 * set break points in .init code. 1030 * 1031 * Most new debuggers use librtld_db to monitor activity events. 1032 * Older debuggers indicated their presence by setting the 1033 * DT_DEBUG entry in the dynamic executable (see elf_new_lm()). 1034 * In this case, getpid() is called so that the debugger can 1035 * catch the system call. This old mechanism has some 1036 * restrictions, as getpid() should not be called prior to 1037 * basic process initialization being completed. This 1038 * restriction has become increasingly difficult to maintain, 1039 * as the use of auditors, LD_DEBUG, and the initialization 1040 * handshake with libc can result in "premature" getpid() 1041 * calls. The use of this getpid() handshake is expected to 1042 * disappear at some point in the future, and there is intent 1043 * to work towards that goal. 1044 */ 1045 rd_event(&lml_main, RD_DLACTIVITY, RT_CONSISTENT); 1046 rd_event(&lml_rtld, RD_DLACTIVITY, RT_CONSISTENT); 1047 1048 if (rtld_flags & RT_FL_DEBUGGER) { 1049 r_debug.rtd_rdebug.r_flags |= RD_FL_ODBG; 1050 (void) getpid(); 1051 } 1052 } 1053 1054 /* 1055 * Indicate preinit activity, and call any auditing routines. These 1056 * routines are called before initializing any threads via libc, or 1057 * before collecting the complete set of .inits on the primary link-map. 1058 * Although most libc interfaces are encapsulated in local routines 1059 * within libc, they have been known to escape (ie. call a .plt). As 1060 * the appcert auditor uses preinit as a trigger to establish some 1061 * external interfaces to the main link-maps libc, we need to activate 1062 * this trigger before exercising any code within libc. Additionally, 1063 * I wouldn't put it past an auditor to add additional objects to the 1064 * primary link-map. Hence, we collect .inits after the audit call. 1065 */ 1066 rd_event(&lml_main, RD_PREINIT, 0); 1067 1068 if ((lml_main.lm_tflags | AFLAGS(mlmp)) & LML_TFLG_AUD_ACTIVITY) 1069 audit_activity(mlmp, LA_ACT_CONSISTENT); 1070 if ((lml_main.lm_tflags | AFLAGS(mlmp)) & LML_TFLG_AUD_PREINIT) 1071 audit_preinit(mlmp); 1072 1073 /* 1074 * If we're creating initial configuration information, we're done 1075 * now that the auditing step has been called. 1076 */ 1077 if (rtld_flags & RT_FL_CONFGEN) { 1078 leave(LIST(mlmp), 0); 1079 return (mlmp); 1080 } 1081 1082 /* 1083 * Sort the .init sections of all objects we've added. If we're 1084 * tracing we only need to execute this under ldd(1) with the -i or -u 1085 * options. 1086 */ 1087 lmflags = lml_main.lm_flags; 1088 if (((lmflags & LML_FLG_TRC_ENABLE) == 0) || 1089 (lmflags & (LML_FLG_TRC_INIT | LML_FLG_TRC_UNREF))) { 1090 if ((tobj = tsort(mlmp, LIST(mlmp)->lm_init, 1091 RT_SORT_REV)) == (Rt_map **)S_ERROR) 1092 return (0); 1093 } 1094 1095 /* 1096 * If we are tracing we're done. This is the one legitimate use of a 1097 * direct call to rtldexit() rather than return, as we don't want to 1098 * return and jump to the application. 1099 */ 1100 if (lmflags & LML_FLG_TRC_ENABLE) { 1101 unused(&lml_main); 1102 rtldexit(&lml_main, 0); 1103 } 1104 1105 /* 1106 * Check if this instance of the linker should have a primary link 1107 * map. This flag allows multiple copies of the -same- -version- 1108 * of the linker (and libc) to run in the same address space. 1109 * 1110 * Without this flag we only support one copy of the linker in a 1111 * process because by default the linker will always try to 1112 * initialize at one primary link map The copy of libc which is 1113 * initialized on a primary link map will initalize global TLS 1114 * data which can be shared with other copies of libc in the 1115 * process. The problem is that if there is more than one copy 1116 * of the linker, only one copy should link libc onto a primary 1117 * link map, otherwise libc will attempt to re-initialize global 1118 * TLS data. So when a copy of the linker is loaded with this 1119 * flag set, it will not initialize any primary link maps since 1120 * persumably another copy of the linker will do this. 1121 * 1122 * Note that this flag only allows multiple copies of the -same- 1123 * -version- of the linker (and libc) to coexist. This approach 1124 * will not work if we are trying to load different versions of 1125 * the linker and libc into the same process. The reason for 1126 * this is that the format of the global TLS data may not be 1127 * the same for different versions of libc. In this case each 1128 * different version of libc must have it's own primary link map 1129 * and be able to maintain it's own TLS data. The only way this 1130 * can be done is by carefully managing TLS pointers on transitions 1131 * between code associated with each of the different linkers. 1132 * Note that this is actually what is done for processes in lx 1133 * branded zones. Although in the lx branded zone case, the 1134 * other linker and libc are actually gld and glibc. But the 1135 * same general TLS management mechanism used by the lx brand 1136 * would apply to any attempts to run multiple versions of the 1137 * solaris linker and libc in a single process. 1138 */ 1139 if (auxflags & AF_SUN_NOPLM) 1140 rtld_flags2 |= RT_FL2_NOPLM; 1141 1142 /* 1143 * Establish any static TLS for this primary link-map. Note, regardless 1144 * of whether TLS is available, an initial handshake occurs with libc to 1145 * indicate we're processing the primary link-map. Having identified 1146 * the primary link-map, initialize threads. 1147 */ 1148 if (rt_get_extern(&lml_main, mlmp) == 0) 1149 return (0); 1150 1151 if ((rtld_flags2 & RT_FL2_NOPLM) == 0) { 1152 if (tls_statmod(&lml_main, mlmp) == 0) 1153 return (0); 1154 rt_thr_init(&lml_main); 1155 rtld_flags2 |= RT_FL2_PLMSETUP; 1156 } else { 1157 rt_thr_init(&lml_main); 1158 } 1159 1160 rtld_flags |= RT_FL_APPLIC; 1161 1162 /* 1163 * Fire all dependencies .init sections. Identify any unused 1164 * dependencies, and leave the runtime linker - effectively calling 1165 * the dynamic executables entry point. 1166 */ 1167 call_array(PREINITARRAY(mlmp), (uint_t)PREINITARRAYSZ(mlmp), mlmp, 1168 SHT_PREINIT_ARRAY); 1169 1170 if (tobj) 1171 call_init(tobj, DBG_INIT_SORT); 1172 1173 rd_event(&lml_main, RD_POSTINIT, 0); 1174 1175 unused(&lml_main); 1176 1177 DBG_CALL(Dbg_util_call_main(mlmp)); 1178 1179 rtld_flags |= RT_FL_OPERATION; 1180 leave(LIST(mlmp), 0); 1181 1182 return (mlmp); 1183 } 1184