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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 1988 AT&T 24 * All Rights Reserved 25 * 26 * 27 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 28 * Use is subject to license terms. 29 */ 30 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 33 /* 34 * Run time linker common setup. 35 * 36 * Called from _setup to get the process going at startup. 37 */ 38 #include "_synonyms.h" 39 40 #include <stdlib.h> 41 #include <fcntl.h> 42 #include <stdio.h> 43 #include <sys/types.h> 44 #include <sys/stat.h> 45 #include <sys/mman.h> 46 #include <string.h> 47 #include <stdio.h> 48 #include <unistd.h> 49 #include <dlfcn.h> 50 #include <sys/sysconfig.h> 51 #include <sys/auxv.h> 52 #include "_rtld.h" 53 #include "_audit.h" 54 #include "_elf.h" 55 #include "_a.out.h" 56 #include "msg.h" 57 #include "debug.h" 58 #include "conv.h" 59 60 61 extern int _end, _edata, _etext; 62 extern void _init(void); 63 extern int _brk_unlocked(void *); 64 65 #ifndef SGS_PRE_UNIFIED_PROCESS 66 /* needed for _brk_unlocked() */ 67 void *_nd = &_end; 68 #endif 69 70 /* 71 * Define for the executable's interpreter. 72 * Usually it is ld.so.1, but for the first release of ICL binaries 73 * it is libc.so.1. We keep this information so that we don't end 74 * up mapping libc twice if it is the interpreter. 75 */ 76 static Interp _interp; 77 78 79 static int 80 preload(const char *str, Rt_map *lmp) 81 { 82 Rt_map *clmp = lmp; 83 char *objs, *ptr, *next; 84 Word lmflags = lml_main.lm_flags; 85 uint_t flags; 86 87 DBG_CALL(Dbg_util_nl()); 88 89 if ((objs = strdup(str)) == 0) 90 return (0); 91 92 /* 93 * Establish the flags for loading each object. If we're called via 94 * lddstub, then the first shared object is the object being inspected 95 * by ldd(1). This object should not be marked as an interposer, as 96 * it is intended to act like the first object of the process. 97 */ 98 if ((lmflags & LML_FLG_TRC_ENABLE) && (FLAGS1(lmp) & FL1_RT_LDDSTUB)) 99 flags = FLG_RT_PRELOAD; 100 else 101 flags = (FLG_RT_PRELOAD | FLG_RT_INTRPOSE); 102 103 ptr = strtok_r(objs, MSG_ORIG(MSG_STR_DELIMIT), &next); 104 do { 105 Pnode *pnp; 106 Rt_map *nlmp = 0; 107 108 DBG_CALL(Dbg_file_preload(ptr)); 109 110 /* 111 * If this a secure application, then preload errors are 112 * reduced to warnings, as the errors are non-fatal. 113 */ 114 if (rtld_flags & RT_FL_SECURE) 115 rtld_flags2 |= RT_FL2_FTL2WARN; 116 if ((pnp = expand_paths(clmp, ptr, PN_SER_EXTLOAD, 0)) != 0) 117 nlmp = load_one(&lml_main, ALO_DATA, pnp, clmp, 118 MODE(lmp), flags, 0); 119 if (pnp) 120 remove_pnode(pnp); 121 if (rtld_flags & RT_FL_SECURE) 122 rtld_flags2 &= ~RT_FL2_FTL2WARN; 123 if (nlmp && (bind_one(clmp, nlmp, BND_NEEDED) == 0)) 124 nlmp = 0; 125 126 /* 127 * Establish state for the next preloadable object. If no 128 * error occurred with loading this object, indicate that this 129 * link-map list contains an interposer. 130 */ 131 flags |= FLG_RT_INTRPOSE; 132 if (nlmp == 0) { 133 if ((lmflags & LML_FLG_TRC_ENABLE) || 134 (rtld_flags & RT_FL_SECURE)) 135 continue; 136 else 137 return (0); 138 } 139 lml_main.lm_flags |= LML_FLG_INTRPOSE; 140 141 /* 142 * If we're tracing shared objects via lddstub, establish a 143 * binding between the initial shared object and lddstub so that 144 * the shared object isn't called out from unused() processing. 145 * After the first object is loaded increment the caller to the 146 * initial preloaded object to provide intuitive ldd -v and -s 147 * diagnostics 148 */ 149 if ((lmflags & LML_FLG_TRC_ENABLE) && 150 (FLAGS1(lmp) & FL1_RT_LDDSTUB)) { 151 if ((lmp == clmp) && (lmflags & 152 (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED))) { 153 if (bind_one(clmp, nlmp, BND_REFER) == 0) 154 continue; 155 } 156 clmp = (Rt_map *)NEXT(lmp); 157 } 158 159 } while ((ptr = strtok_r(NULL, 160 MSG_ORIG(MSG_STR_DELIMIT), &next)) != NULL); 161 162 free(objs); 163 return (1); 164 } 165 166 Rt_map * 167 setup(ulong_t envp, ulong_t _auxv, Word _flags, char *_platform, int _syspagsz, 168 char *_rt_name, Dyn *dyn_ptr, ulong_t ld_base, ulong_t interp_base, int fd, 169 Phdr *phdr, char *_execname, char **_argv, int dz_fd, uid_t uid, uid_t euid, 170 gid_t gid, gid_t egid, void *aoutdyn, int auxflags, uint_t hwcap_1) 171 { 172 Rt_map *rlmp, *mlmp, **tobj = 0; 173 ulong_t etext; 174 Ehdr *ehdr; 175 struct stat status; 176 int features = 0, _name, i, ldsoexec = 0; 177 size_t eaddr, esize; 178 char *c; 179 Mmap *mmaps; 180 181 /* 182 * Now that ld.so has relocated itself, initialize any global variables. 183 * Initialize our own 'environ' so as to establish an address suitable 184 * for libc's hardware mul/div magic (libc/sparc/crt/hwmuldiv.o). 185 */ 186 _environ = (char **)((ulong_t)_auxv - sizeof (char *)); 187 _init(); 188 _environ = (char **)envp; 189 190 at_flags = _flags; 191 if (dz_fd != FD_UNAVAIL) 192 dz_init(dz_fd); 193 platform = _platform; 194 195 /* 196 * If pagesize is unspecified find its value. 197 */ 198 if ((syspagsz = _syspagsz) == 0) 199 syspagsz = _sysconfig(_CONFIG_PAGESIZE); 200 fmap_setup(); 201 202 /* 203 * Add the unused portion of the last data page to the free space list. 204 * The page size must be set before doing this. Here, _end refers to 205 * the end of the runtime linkers bss. Note that we do not use the 206 * unused data pages from any included .so's to supplement this free 207 * space as badly behaved .os's may corrupt this data space, and in so 208 * doing ruin our data. 209 */ 210 eaddr = S_DROUND((size_t)&_end); 211 esize = eaddr % syspagsz; 212 if (esize) { 213 esize = syspagsz - esize; 214 addfree((void *)eaddr, esize); 215 } 216 217 /* 218 * Establish initial link-map list flags, and link-map list alists. 219 */ 220 if (alist_append(&lml_main.lm_lists, 0, sizeof (Lm_cntl), 221 AL_CNT_LMLISTS) == 0) 222 return (0); 223 lml_main.lm_flags |= LML_FLG_BASELM; 224 225 if (alist_append(&lml_rtld.lm_lists, 0, sizeof (Lm_cntl), 226 AL_CNT_LMLISTS) == 0) 227 return (0); 228 lml_rtld.lm_flags |= (LML_FLG_RTLDLM | LML_FLG_NOAUDIT | 229 LML_FLG_HOLDLOCK); 230 231 /* 232 * Determine whether we have a secure executable. 233 */ 234 security(uid, euid, gid, egid, auxflags); 235 236 /* 237 * Initialize a hardware capability descriptor for use in comparing 238 * each loaded object. 239 */ 240 #ifdef AT_SUN_AUXFLAGS 241 if (auxflags & AF_SUN_HWCAPVERIFY) { 242 rtld_flags2 |= RT_FL2_HWCAP; 243 hwcap = (ulong_t)hwcap_1; 244 } 245 #endif 246 247 /* 248 * Look for environment strings (allows things like LD_NOAUDIT to be 249 * established, although debugging isn't enabled until later). 250 */ 251 if ((readenv_user((const char **)envp, &(lml_main.lm_flags), 252 &(lml_main.lm_tflags), (aoutdyn != 0))) == 1) 253 return (0); 254 255 /* 256 * Create a mapping descriptor for ld.so.1. We can determine our 257 * two segments information from known symbols. 258 */ 259 if ((mmaps = calloc(3, sizeof (Mmap))) == 0) 260 return (0); 261 mmaps[0].m_vaddr = (caddr_t)M_PTRUNC(ld_base); 262 mmaps[0].m_msize = (size_t)((caddr_t)&_etext - mmaps[0].m_vaddr); 263 mmaps[0].m_fsize = mmaps[0].m_msize; 264 mmaps[0].m_perm = (PROT_READ | PROT_EXEC); 265 mmaps[1].m_vaddr = (caddr_t)M_PTRUNC((ulong_t)&r_debug); 266 mmaps[1].m_msize = (size_t)((caddr_t)&_end - mmaps[1].m_vaddr); 267 mmaps[1].m_fsize = (size_t)((caddr_t)&_edata - mmaps[1].m_vaddr); 268 mmaps[1].m_perm = (PROT_READ | PROT_WRITE | PROT_EXEC); 269 270 /* 271 * Create a link map structure for ld.so. We assign the NAME() after 272 * link-map creation to avoid fullpath() processing within elf_new_lm(). 273 * This is carried out later when the true interpretor path (as defined 274 * within the application) is known. 275 */ 276 if ((rlmp = elf_new_lm(&lml_rtld, 0, 0, dyn_ptr, ld_base, 277 (ulong_t)&_etext, ALO_DATA, (ulong_t)(eaddr - ld_base), 0, ld_base, 278 (ulong_t)(eaddr - ld_base), mmaps, 2)) == 0) { 279 return (0); 280 } 281 NAME(rlmp) = _rt_name; 282 MODE(rlmp) |= (RTLD_LAZY | RTLD_NODELETE | RTLD_GLOBAL | RTLD_WORLD); 283 FLAGS(rlmp) |= (FLG_RT_ANALYZED | FLG_RT_RELOCED | FLG_RT_INITDONE | 284 FLG_RT_INITCLCT | FLG_RT_FINICLCT | FLG_RT_MODESET); 285 ldso_plt_init(rlmp); 286 287 /* 288 * If we received neither the AT_EXECFD nor the AT_PHDR aux vector, 289 * ld.so.1 must have been invoked directly from the command line. In 290 * this case examine argv to determine what file to execute. 291 */ 292 if ((fd == -1) && (!phdr)) { 293 /* 294 * Set pr_name now to print error messages if required. It will 295 * be reset below if an executable is found. 296 */ 297 rt_name = pr_name = _argv[0]; 298 if (rtld_getopt(_argv, &(lml_main.lm_flags), 299 &(lml_main.lm_tflags), (aoutdyn != 0)) == 1) 300 return (0); 301 if ((fd = open(_argv[0], O_RDONLY)) == -1) { 302 int err = errno; 303 eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), _argv[0], 304 strerror(err)); 305 return (0); 306 } 307 NAME(rlmp) = _execname; 308 interp = &_interp; 309 interp->i_name = NAME(rlmp); 310 interp->i_faddr = (caddr_t)ADDR(rlmp); 311 ldsoexec = 1; 312 } 313 314 /* 315 * Duplicate the runtime linkers name so that it is available in a core 316 * file. 317 */ 318 if ((NAME(rlmp) = strdup(NAME(rlmp))) == 0) 319 return (0); 320 321 /* 322 * Get the filename of the rtld for use in any diagnostics (but 323 * save the full name in the link map for future comparisons) 324 */ 325 rt_name = c = NAME(rlmp); 326 while (*c) { 327 if (*c++ == '/') 328 rt_name = c; 329 } 330 331 /* 332 * Establish the applications name. Note, if ld.so.1 was executed with 333 * the application as its argument, argv[0] will now reflect the 334 * application name. 335 */ 336 if (_argv[0]) { 337 /* 338 * Some troublesome programs will change the value of argv[0]. 339 * Dupping this string protects ourselves from such programs. 340 */ 341 if ((pr_name = (const char *)strdup(_argv[0])) == 0) 342 return (0); 343 } else 344 pr_name = (const char *)MSG_INTL(MSG_STR_UNKNOWN); 345 346 _name = 0; 347 348 /* 349 * Map in the file, if exec has not already done so. If it has, 350 * simply create a new link map structure for the executable. 351 */ 352 if (fd != -1) { 353 Rej_desc rej; 354 Fct *ftp; 355 356 /* 357 * Find out what type of object we have. 358 */ 359 (void) fstat(fd, &status); 360 if ((ftp = are_u_this(&rej, fd, &status, pr_name)) == 0) { 361 eprintf(ERR_FATAL, MSG_INTL(err_reject[rej.rej_type]), 362 pr_name, conv_reject_str(&rej)); 363 return (0); 364 } 365 366 /* 367 * Map in object. 368 */ 369 mlmp = (ftp->fct_map_so)(&lml_main, ALO_DATA, 0, pr_name, fd); 370 if (mlmp == 0) 371 return (0); 372 373 if (ldsoexec) { 374 Addr brkbase = 0; 375 376 /* 377 * Since ld.so.1 was the primary executed object - the 378 * brk() base has not yet been initialized, we need to 379 * initialize it. For an executable, initialize it to 380 * the end of the object. For a shared object (ET_DYN) 381 * initialize it to the first page in memory. 382 */ 383 ehdr = (Ehdr *)ADDR(mlmp); 384 385 if ((FCT(mlmp) == &elf_fct) && 386 (ehdr->e_type == ET_EXEC)) { 387 Phdr * _phdr = (Phdr *)((uintptr_t)ADDR(mlmp) + 388 ehdr->e_phoff); 389 390 /* 391 * We scan the program headers to find the tail 392 * of the memory image. We can't use MSIZE() 393 * since that's already been page aligned. 394 */ 395 for (i = 0; i < ehdr->e_phnum; i++, _phdr++) { 396 if (_phdr->p_type == PT_LOAD) 397 brkbase = _phdr->p_vaddr + 398 _phdr->p_memsz; 399 } 400 } 401 402 if (!brkbase) 403 brkbase = syspagsz; 404 405 if (_brk_unlocked((void *)brkbase) == -1) { 406 int err = errno; 407 eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_BRK), 408 pr_name, strerror(err)); 409 } 410 } 411 412 /* 413 * Object has now been mmaped in, we no longer 414 * need the file descriptor. 415 */ 416 (void) close(fd); 417 418 } else { 419 /* 420 * Set up function ptr and arguments according to the type 421 * of file class the executable is. (Currently only supported 422 * types are ELF and a.out format.) Then create a link map 423 * for the executable. 424 */ 425 if (aoutdyn) { 426 #ifdef A_OUT 427 if ((mlmp = aout_new_lm(&lml_main, 0, 0, aoutdyn, 0, 0, 428 ALO_DATA)) == 0) { 429 return (0); 430 } 431 432 /* 433 * Set the memory size. Note, we only know the end of 434 * text, and although we could find the _end by looking 435 * up the symbol, this may not be present. We should 436 * set ADDR to MAIN_BASE, but presently all the a.out 437 * relocation code assumes ADDR is 0 for the dynamic 438 * executable. (these data items are only used for 439 * dladdr(3x), and there aren't many a.out dladdr(3x) 440 * users to warrant spending much time on this :-). 441 */ 442 MSIZE(mlmp) = MAIN_BASE + ETEXT(mlmp); 443 444 /* 445 * Disable any object configuration cache (BCP apps 446 * bring in sbcp which can benefit from any object 447 * cache, but both the app and sbcp can't use the same 448 * objects). 449 */ 450 rtld_flags |= RT_FL_NOOBJALT; 451 452 /* 453 * Make sure no-direct bindings are in effect. 454 */ 455 lml_main.lm_tflags |= LML_TFLG_NODIRECT; 456 457 #else 458 eprintf(ERR_FATAL, MSG_INTL(MSG_ERR_REJ_UNKFILE), 459 pr_name); 460 return (0); 461 #endif 462 } else if (phdr) { 463 Phdr *pptr, *firstptr = 0, *lastptr; 464 Phdr *tlsphdr = 0; 465 Phdr *unwindphdr = 0; 466 Dyn *dyn = 0; 467 Cap *cap = 0; 468 Off i_offset; 469 Addr base = 0; 470 char *name = 0; 471 ulong_t memsize, phsize, entry; 472 uint_t mmapcnt = 0; 473 474 /* 475 * Using the executables phdr address determine the base 476 * address of the input file. NOTE, this assumes the 477 * program headers and elf header are part of the same 478 * mapped segment. Although this has held for many 479 * years now, it might be more flexible if the kernel 480 * gave use the ELF headers start address, rather than 481 * the Program headers. 482 * 483 * Determine from the ELF header if we're been called 484 * from a shared object or dynamic executable. If the 485 * latter, then any addresses within the object are used 486 * as is. Addresses within shared objects must be added 487 * to the process's base address. 488 */ 489 ehdr = (Ehdr *)((Addr)phdr - phdr->p_offset); 490 phsize = ehdr->e_phentsize; 491 if (ehdr->e_type == ET_DYN) { 492 base = (Addr)ehdr; 493 name = (char *)pr_name; 494 _name = 1; 495 } 496 497 /* 498 * Allocate a mapping array to retain mapped segment 499 * information. 500 */ 501 if ((mmaps = calloc(ehdr->e_phnum, sizeof (Mmap))) == 0) 502 return (0); 503 504 /* 505 * Extract the needed information from the segment 506 * headers. 507 */ 508 for (i = 0, pptr = phdr; i < ehdr->e_phnum; i++) { 509 if (pptr->p_type == PT_INTERP) { 510 interp = &_interp; 511 i_offset = pptr->p_offset; 512 interp->i_faddr = 513 (caddr_t)interp_base; 514 } 515 if ((pptr->p_type == PT_LOAD) && 516 (pptr->p_filesz || pptr->p_memsz)) { 517 int perm = (PROT_READ | PROT_EXEC); 518 size_t off; 519 520 if (!firstptr) 521 firstptr = pptr; 522 lastptr = pptr; 523 if (!interp->i_name && pptr->p_filesz && 524 (i_offset >= pptr->p_offset) && 525 (i_offset <= 526 (pptr->p_memsz + pptr->p_offset))) { 527 interp->i_name = (char *) 528 pptr->p_vaddr + i_offset - 529 pptr->p_offset + base; 530 } 531 if ((pptr->p_flags & 532 (PF_R | PF_W)) == PF_R) 533 etext = pptr->p_vaddr + 534 pptr->p_memsz + base; 535 else 536 perm |= PROT_WRITE; 537 538 /* 539 * Retain segments mapping info. Round 540 * each segment to a page boundary, as 541 * this insures addresses are suitable 542 * for mprotect() if required. 543 */ 544 off = pptr->p_vaddr + base; 545 mmaps[mmapcnt].m_vaddr = 546 (caddr_t)M_PTRUNC(off); 547 off -= (size_t)mmaps[mmapcnt].m_vaddr; 548 mmaps[mmapcnt].m_msize = 549 pptr->p_memsz + off; 550 mmaps[mmapcnt].m_fsize = 551 pptr->p_filesz + off; 552 mmaps[mmapcnt].m_perm = perm; 553 mmapcnt++; 554 555 } else if (pptr->p_type == PT_DYNAMIC) 556 dyn = (Dyn *)(pptr->p_vaddr + base); 557 else if (pptr->p_type == PT_TLS) 558 tlsphdr = pptr; 559 else if (pptr->p_type == PT_SUNW_UNWIND) 560 unwindphdr = pptr; 561 else if (pptr->p_type == PT_SUNWCAP) 562 cap = (Cap *)(pptr->p_vaddr + base); 563 pptr = (Phdr *)((ulong_t)pptr + phsize); 564 } 565 566 567 memsize = (lastptr->p_vaddr + lastptr->p_memsz) - 568 S_ALIGN(firstptr->p_vaddr, syspagsz); 569 570 entry = ehdr->e_entry; 571 if (ehdr->e_type == ET_DYN) 572 entry += (ulong_t)ehdr; 573 574 if ((mlmp = elf_new_lm(&lml_main, name, 0, dyn, 575 (Addr)ehdr, etext, ALO_DATA, memsize, entry, 576 (ulong_t)ehdr, memsize, mmaps, mmapcnt)) == 0) { 577 return (0); 578 } 579 if (tlsphdr) { 580 PTTLS(mlmp) = tlsphdr; 581 tls_assign_soffset(mlmp); 582 } 583 if (unwindphdr) 584 PTUNWIND(mlmp) = unwindphdr; 585 if (cap) 586 cap_assign(cap, mlmp); 587 } 588 } 589 590 /* 591 * Determine whether the kernel has supplied a AT_SUN_EXECNAME aux 592 * vector. This vector points to the full pathname, on the stack, of 593 * the object that started the process. If this is null, then 594 * AT_SUN_EXECNAME isn't supported (if the pathname exceeded the system 595 * limit (PATH_MAX) the exec would have failed). 596 */ 597 if (_execname) 598 rtld_flags |= RT_FL_EXECNAME; 599 600 /* 601 * Having mapped the executable in and created its link map, initialize 602 * the name and flags entries as necessary. 603 * 604 * Note that any object that starts the process is identified as `main', 605 * even shared objects. This assumes that the starting object will call 606 * .init and .fini from its own crt use (this is a pretty valid 607 * assumption as the crts also provide the necessary entry point). 608 * However, newer objects may contain .initarray or .finiarray which 609 * the runtime linker must execute, and which require bindings to be 610 * established to main for proper initarray/finiarray ordering. 611 */ 612 if (_name == 0) { 613 /* 614 * If the argv[0] name is a full path, and an AT_SUN_EXECNAME 615 * exists, and we haven't executed ld.so.1 directly, then use 616 * this name for diagnostics. Various commands use isaexec(3C) 617 * to execute their 64-bit counterparts, however, the 64-bit 618 * application simply obtains its argv[] from the parent, and 619 * thus will contain the 32-bit application name. 620 */ 621 if ((*pr_name == '/') && _execname && (ldsoexec == 0)) 622 pr_name = _execname; 623 NAME(mlmp) = (char *)pr_name; 624 } 625 626 /* 627 * Setup the PATHNAME()/ORIGNAME() for the main primary object and 628 * for ld.so.1. If we didn't receive a AT_SUN_EXECNAME or it 629 * was ld.so.1 itself that was executed, then PATHNAME() will be 630 * based off of argv[0]. Otherwise - the PATHNAME is AT_SUN_EXECNAME. 631 */ 632 if ((ldsoexec) || (_execname == 0)) 633 PATHNAME(mlmp) = NAME(mlmp); 634 else 635 PATHNAME(mlmp) = _execname; 636 637 ORIGNAME(mlmp) = PATHNAME(mlmp); 638 639 /* 640 * If the kernel has provided hardware capabilities information, and 641 * the executable contains hardware capabilities information, make 642 * sure it's a valid object. 643 */ 644 if ((rtld_flags2 & RT_FL2_HWCAP) && HWCAP(mlmp)) { 645 ulong_t mhwcap; 646 647 if ((mhwcap = (HWCAP(mlmp) & ~hwcap)) != 0) { 648 if (lml_main.lm_flags & LML_FLG_TRC_ENABLE) { 649 (void) printf(MSG_INTL(MSG_LDD_GEN_HWCAP_1), 650 NAME(mlmp), 651 conv_hwcap_1_str(mhwcap, M_MACH)); 652 } else { 653 eprintf(ERR_FATAL, MSG_INTL(MSG_GEN_BADHWCAP_1), 654 conv_hwcap_1_str(mhwcap, M_MACH)); 655 return (0); 656 } 657 } 658 } 659 660 FLAGS(mlmp) |= (FLG_RT_ISMAIN | FLG_RT_MODESET); 661 FLAGS1(mlmp) |= FL1_RT_USED; 662 if ((INITARRAY(mlmp) == 0) && (FINIARRAY(mlmp) == 0)) 663 FLAGS1(mlmp) |= FL1_RT_NOINIFIN; 664 665 /* 666 * Establish the interpretors name as that defined within the initial 667 * object (executable). This provides for ORIGIN processing of ld.so.1 668 * dependencies. 669 */ 670 if (interp) { 671 size_t len; 672 ORIGNAME(rlmp) = interp->i_name; 673 len = strlen(interp->i_name); 674 (void) expand(&interp->i_name, &len, 0, 0, 675 (PN_TKN_ISALIST | PN_TKN_HWCAP), mlmp); 676 PATHNAME(rlmp) = interp->i_name; 677 } else { 678 ORIGNAME(rlmp) = PATHNAME(rlmp) = NAME(rlmp); 679 } 680 681 /* 682 * Far the most common application execution revolves around appending 683 * the application name to the users PATH definition, thus a full name 684 * is passed to exec() which will in turn be returned via 685 * AT_SUN_EXECNAME. Applications may also be invoked from the current 686 * working directory, or via a relative name. 687 * 688 * When $ORIGIN was first introduced, the expansion of a relative 689 * pathname was deferred until it was required. However now we insure 690 * a full pathname is always created - things like the analyzer wish to 691 * rely on librtld_db returning a full path. The overhead of this is 692 * perceived to be low, providing the associated libc version of getcwd 693 * is available (see 4336878), plus it only affects execing relative 694 * paths. Here we expand the application and ld.so.1 - see 695 * elf_new_lm() for the expansion of all other dependencies. 696 */ 697 if (FLAGS1(mlmp) & FL1_RT_RELATIVE) 698 (void) fullpath(mlmp, 0); 699 if (FLAGS1(rlmp) & FL1_RT_RELATIVE) 700 (void) fullpath(rlmp, 0); 701 702 /* 703 * Identify lddstub if necessary. 704 */ 705 if (lml_main.lm_flags & LML_FLG_TRC_LDDSTUB) 706 FLAGS1(mlmp) |= FL1_RT_LDDSTUB; 707 708 (void) enter(); 709 710 /* 711 * If no .initarray or .finiarray are present on the executable do not 712 * enter them into the 'sorting' mechanism for .init/.fini firing. 713 * 714 * This is to prevent them showing up during 'ldd -i ...' 715 * output when they don't do anything. 716 */ 717 if (INITARRAY(mlmp) == 0) { 718 FLAGS(mlmp) |= (FLG_RT_INITCLCT | FLG_RT_INITCALL | 719 FLG_RT_INITDONE); 720 LIST(mlmp)->lm_init--; 721 } 722 723 if (FINIARRAY(mlmp) == 0) 724 FLAGS(mlmp) |= FLG_RT_FINICLCT; 725 726 /* 727 * Add our two main link-maps to the dynlm_list 728 */ 729 if (list_append(&dynlm_list, &lml_main) == 0) 730 return (0); 731 732 if (list_append(&dynlm_list, &lml_rtld) == 0) 733 return (0); 734 735 /* 736 * Reset the link-map counts for both lists. The init count is used to 737 * track how many objects have pending init sections, this gets incre- 738 * mented each time an object is relocated. Since ld.so.1 relocates 739 * itself, it's init count will remain zero. 740 * The object count is used to track how many objects have pending fini 741 * sections, as ld.so.1 handles its own fini we can zero its count. 742 */ 743 lml_main.lm_obj = 1; 744 lml_rtld.lm_obj = 0; 745 746 /* 747 * Initialize debugger information structure. Some parts of this 748 * structure were initialized statically. 749 */ 750 r_debug.rtd_rdebug.r_map = (Link_map *)lml_main.lm_head; 751 r_debug.rtd_rdebug.r_ldsomap = (Link_map *)lml_rtld.lm_head; 752 r_debug.rtd_rdebug.r_ldbase = r_debug.rtd_rdebug.r_ldsomap->l_addr; 753 r_debug.rtd_dynlmlst = &dynlm_list; 754 755 if (platform) 756 platform_sz = strlen(platform); 757 758 /* 759 * Determine the dev/inode information for the executable to complete 760 * load_so() checking for those who might dlopen(a.out). 761 */ 762 if ((FLAGS1(mlmp) & FL1_RT_RELATIVE) && 763 (stat(PATHNAME(mlmp), &status) == 0)) { 764 STDEV(mlmp) = status.st_dev; 765 STINO(mlmp) = status.st_ino; 766 } 767 768 /* 769 * Initialize any configuration information. 770 */ 771 if (!(rtld_flags & RT_FL_NOCFG)) { 772 if ((features = elf_config(mlmp, (aoutdyn != 0))) == -1) 773 return (0); 774 } 775 776 /* 777 * Establish the modes of the initial object. These modes are 778 * propagated to any preloaded objects and explicit shared library 779 * dependencies. 780 */ 781 MODE(mlmp) |= (RTLD_NODELETE | RTLD_GLOBAL | RTLD_WORLD); 782 if (rtld_flags & RT_FL_CONFGEN) 783 MODE(mlmp) |= RTLD_CONFGEN; 784 if (rtld_flags2 & RT_FL2_BINDNOW) 785 MODE(mlmp) |= RTLD_NOW; 786 else 787 MODE(mlmp) |= RTLD_LAZY; 788 789 /* 790 * If debugging was requested initialize things now that any cache has 791 * been established. 792 */ 793 if (rpl_debug) 794 dbg_mask |= dbg_setup(rpl_debug); 795 if (prm_debug) 796 dbg_mask |= dbg_setup(prm_debug); 797 798 /* 799 * Now that debugging is enabled generate any diagnostics from any 800 * previous events. 801 */ 802 if (hwcap) 803 DBG_CALL(Dbg_cap_hw_1(hwcap, M_MACH)); 804 if (features) 805 DBG_CALL(Dbg_file_config_dis(config->c_name, features)); 806 807 if (dbg_mask) { 808 DBG_CALL(Dbg_file_ldso(rt_name, (ulong_t)DYN(rlmp), 809 ADDR(rlmp), envp, _auxv)); 810 811 if (FCT(mlmp) == &elf_fct) { 812 DBG_CALL(Dbg_file_elf(PATHNAME(mlmp), 813 (ulong_t)DYN(mlmp), ADDR(mlmp), MSIZE(mlmp), 814 ENTRY(mlmp), get_linkmap_id(LIST(mlmp)), ALO_DATA)); 815 } else { 816 DBG_CALL(Dbg_file_aout(PATHNAME(mlmp), 817 (ulong_t)AOUTDYN(mlmp), (ulong_t)ADDR(mlmp), 818 (ulong_t)MSIZE(mlmp))); 819 } 820 } 821 822 /* 823 * Enable auditing. 824 */ 825 if (rpl_audit || prm_audit || profile_lib) { 826 int ndx; 827 const char *aud[3]; 828 829 aud[0] = rpl_audit; 830 aud[1] = prm_audit; 831 aud[2] = profile_lib; 832 833 /* 834 * Any global auditing (set using LD_AUDIT or LD_PROFILE) that 835 * can't be established is non-fatal. 836 */ 837 if ((auditors = calloc(1, sizeof (Audit_desc))) == 0) 838 return (0); 839 840 for (ndx = 0; ndx < 3; ndx++) { 841 if (aud[ndx]) { 842 if ((auditors->ad_name = strdup(aud[ndx])) == 0) 843 return (0); 844 rtld_flags2 |= RT_FL2_FTL2WARN; 845 (void) audit_setup(mlmp, auditors); 846 rtld_flags2 &= ~RT_FL2_FTL2WARN; 847 } 848 } 849 lml_main.lm_tflags |= auditors->ad_flags; 850 } 851 if (AUDITORS(mlmp)) { 852 /* 853 * Any object required auditing (set with a DT_DEPAUDIT dynamic 854 * entry) that can't be established is fatal. 855 */ 856 if (audit_setup(mlmp, AUDITORS(mlmp)) == 0) 857 return (0); 858 859 FLAGS1(mlmp) |= AUDITORS(mlmp)->ad_flags; 860 lml_main.lm_flags |= LML_FLG_LOCAUDIT; 861 } 862 863 /* 864 * Explicitly add the initial object and ld.so.1 to those objects being 865 * audited. Note, although the ld.so.1 link-map isn't auditable, 866 * establish a cookie for ld.so.1 as this may be bound to via the 867 * dl*() family. 868 */ 869 if ((lml_main.lm_tflags | FLAGS1(mlmp)) & LML_TFLG_AUD_MASK) { 870 if (((audit_objopen(mlmp, mlmp) == 0) || 871 (audit_objopen(mlmp, rlmp) == 0)) && 872 (FLAGS1(mlmp) & LML_TFLG_AUD_MASK)) 873 return (0); 874 } 875 876 /* 877 * Map in any preloadable shared objects. Note, it is valid to preload 878 * a 4.x shared object with a 5.0 executable (or visa-versa), as this 879 * functionality is required by ldd(1). 880 */ 881 if (rpl_preload && (preload(rpl_preload, mlmp) == 0)) 882 return (0); 883 if (prm_preload && (preload(prm_preload, mlmp) == 0)) 884 return (0); 885 886 /* 887 * Load all dependent (needed) objects. 888 */ 889 if (analyze_lmc(&lml_main, ALO_DATA, mlmp) == 0) 890 return (0); 891 892 /* 893 * Relocate all the dependencies we've just added. 894 * 895 * If this process has been established via crle(1), the environment 896 * variable LD_CONFGEN will have been set. crle(1) may create this 897 * process twice. The first time crle only needs to gather dependency 898 * information. The second time, is to dldump() the images. 899 * 900 * If we're only gathering dependencies, relocation is unnecessary. 901 * As crle(1) may be building an arbitrary family of objects, they may 902 * not fully relocate either. Hence the relocation phase is not carried 903 * out now, but will be called by crle(1) once all objects have been 904 * loaded. 905 */ 906 if ((rtld_flags & RT_FL_CONFGEN) == 0) { 907 Word lmflags; 908 909 DBG_CALL(Dbg_file_nl()); 910 911 if (relocate_lmc(&lml_main, ALO_DATA, mlmp) == 0) 912 return (0); 913 914 /* 915 * Sort the .init sections of all objects we've added. If 916 * we're tracing we only need to execute this under ldd(1) 917 * with the -i or -u options. 918 */ 919 lmflags = lml_main.lm_flags; 920 if (((lmflags & LML_FLG_TRC_ENABLE) == 0) || 921 (lmflags & (LML_FLG_TRC_INIT | LML_FLG_TRC_UNREF))) { 922 if ((tobj = tsort(mlmp, LIST(mlmp)->lm_init, 923 RT_SORT_REV)) == (Rt_map **)S_ERROR) 924 return (0); 925 } 926 927 /* 928 * If we are tracing we're done. This is the one legitimate use 929 * of a direct call to rtldexit() rather than return, as we 930 * don't want to return and jump to the application. 931 */ 932 if (lmflags & LML_FLG_TRC_ENABLE) { 933 unused(&lml_main); 934 rtldexit(&lml_main, 0); 935 } 936 937 /* 938 * Inform the debuggers we're here and stable. Newer debuggers 939 * can indicate their presence by setting the DT_DEBUG entry in 940 * the dynamic executable (see elf_new_lm()). In this case call 941 * getpid() so the debugger can catch the system call. This 942 * handshake allows the debugger to initialize, and consequently 943 * allows the user to set break points in .init code. 944 */ 945 rd_event(&lml_rtld, RD_DLACTIVITY, RT_CONSISTENT); 946 rd_event(&lml_main, RD_DLACTIVITY, RT_CONSISTENT); 947 948 if (rtld_flags & RT_FL_DEBUGGER) { 949 r_debug.rtd_rdebug.r_flags |= RD_FL_ODBG; 950 (void) getpid(); 951 } 952 953 /* 954 * Initialize any initial TLS storage. 955 */ 956 if (tls_report_modules() == 0) 957 return (0); 958 } 959 960 /* 961 * Call any necessary auditing routines, clean up any file descriptors 962 * and such, and then fire all dependencies .init sections. 963 */ 964 rtld_flags |= RT_FL_APPLIC; 965 966 rd_event(&lml_main, RD_PREINIT, 0); 967 968 if ((lml_main.lm_tflags | FLAGS1(mlmp)) & LML_TFLG_AUD_ACTIVITY) 969 audit_activity(mlmp, LA_ACT_CONSISTENT); 970 if ((lml_main.lm_tflags | FLAGS1(mlmp)) & LML_TFLG_AUD_PREINIT) 971 audit_preinit(mlmp); 972 973 call_array(PREINITARRAY(mlmp), (uint_t)PREINITARRAYSZ(mlmp), mlmp, 974 SHT_PREINIT_ARRAY); 975 976 if (tobj) 977 call_init(tobj, DBG_INIT_SORT); 978 979 rd_event(&lml_main, RD_POSTINIT, 0); 980 981 unused(&lml_main); 982 983 DBG_CALL(Dbg_util_call_main(NAME(mlmp))); 984 985 leave(LIST(mlmp)); 986 987 return (mlmp); 988 } 989