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 * Object file dependent support for ELF objects. 35 */ 36 #include "_synonyms.h" 37 38 #include <stdio.h> 39 #include <sys/procfs.h> 40 #include <sys/mman.h> 41 #include <sys/debug.h> 42 #include <string.h> 43 #include <limits.h> 44 #include <dlfcn.h> 45 #include "conv.h" 46 #include "_rtld.h" 47 #include "_audit.h" 48 #include "_elf.h" 49 #include "msg.h" 50 #include "debug.h" 51 52 /* 53 * Default and secure dependency search paths. 54 */ 55 static Pnode elf_dflt_dirs[] = { 56 #if defined(_ELF64) 57 #ifndef SGS_PRE_UNIFIED_PROCESS 58 { MSG_ORIG(MSG_PTH_LIB_64), 0, MSG_PTH_LIB_64_SIZE, 59 LA_SER_DEFAULT, 0, &elf_dflt_dirs[1] }, 60 #endif 61 { MSG_ORIG(MSG_PTH_USRLIB_64), 0, MSG_PTH_USRLIB_64_SIZE, 62 LA_SER_DEFAULT, 0, 0 } 63 #else 64 #ifndef SGS_PRE_UNIFIED_PROCESS 65 { MSG_ORIG(MSG_PTH_LIB), 0, MSG_PTH_LIB_SIZE, 66 LA_SER_DEFAULT, 0, &elf_dflt_dirs[1] }, 67 #endif 68 { MSG_ORIG(MSG_PTH_USRLIB), 0, MSG_PTH_USRLIB_SIZE, 69 LA_SER_DEFAULT, 0, 0 } 70 #endif 71 }; 72 73 static Pnode elf_secure_dirs[] = { 74 #if defined(_ELF64) 75 #ifndef SGS_PRE_UNIFIED_PROCESS 76 { MSG_ORIG(MSG_PTH_LIBSE_64), 0, MSG_PTH_LIBSE_64_SIZE, 77 LA_SER_SECURE, 0, &elf_secure_dirs[1] }, 78 #endif 79 { MSG_ORIG(MSG_PTH_USRLIBSE_64), 0, 80 MSG_PTH_USRLIBSE_64_SIZE, 81 LA_SER_SECURE, 0, 0 } 82 #else 83 #ifndef SGS_PRE_UNIFIED_PROCESS 84 { MSG_ORIG(MSG_PTH_LIBSE), 0, MSG_PTH_LIBSE_SIZE, 85 LA_SER_SECURE, 0, &elf_secure_dirs[1] }, 86 #endif 87 { MSG_ORIG(MSG_PTH_USRLIBSE), 0, MSG_PTH_USRLIBSE_SIZE, 88 LA_SER_SECURE, 0, 0 } 89 #endif 90 }; 91 92 /* 93 * Defines for local functions. 94 */ 95 static Pnode *elf_fix_name(const char *, Rt_map *, uint_t); 96 static int elf_are_u(Rej_desc *); 97 static void elf_dladdr(ulong_t, Rt_map *, Dl_info *, void **, int); 98 static ulong_t elf_entry_pt(void); 99 static char *elf_get_so(const char *, const char *); 100 static Rt_map *elf_map_so(Lm_list *, Aliste, const char *, const char *, int); 101 static int elf_needed(Lm_list *, Aliste, Rt_map *); 102 static void elf_unmap_so(Rt_map *); 103 static int elf_verify_vers(const char *, Rt_map *, Rt_map *); 104 105 /* 106 * Functions and data accessed through indirect pointers. 107 */ 108 Fct elf_fct = { 109 elf_are_u, 110 elf_entry_pt, 111 elf_map_so, 112 elf_unmap_so, 113 elf_needed, 114 lookup_sym, 115 elf_reloc, 116 elf_dflt_dirs, 117 elf_secure_dirs, 118 elf_fix_name, 119 elf_get_so, 120 elf_dladdr, 121 dlsym_handle, 122 elf_verify_vers, 123 elf_set_prot 124 }; 125 126 127 /* 128 * Redefine NEEDED name if necessary. 129 */ 130 static Pnode * 131 elf_fix_name(const char *name, Rt_map *clmp, uint_t orig) 132 { 133 /* 134 * For ABI compliance, if we are asked for ld.so.1, then really give 135 * them libsys.so.1 (the SONAME of libsys.so.1 is ld.so.1). 136 */ 137 if (((*name == '/') && 138 #if defined(_ELF64) 139 (strcmp(name, MSG_ORIG(MSG_PTH_RTLD_64)) == 0)) || 140 #else 141 (strcmp(name, MSG_ORIG(MSG_PTH_RTLD)) == 0)) || 142 #endif 143 (strcmp(name, MSG_ORIG(MSG_FIL_RTLD)) == 0)) { 144 Pnode *pnp; 145 146 DBG_CALL(Dbg_file_fixname(name, MSG_ORIG(MSG_PTH_LIBSYS))); 147 if (((pnp = calloc(sizeof (Pnode), 1)) == 0) || 148 ((pnp->p_name = strdup(MSG_ORIG(MSG_PTH_LIBSYS))) == 0)) { 149 if (pnp) 150 free(pnp); 151 return (0); 152 } 153 pnp->p_len = MSG_PTH_LIBSYS_SIZE; 154 pnp->p_orig = (orig & PN_SER_MASK); 155 return (pnp); 156 } 157 158 return (expand_paths(clmp, name, orig, 0)); 159 } 160 161 /* 162 * Determine if we have been given an ELF file and if so determine if the file 163 * is compatible. Returns 1 if true, else 0 and sets the reject descriptor 164 * with associated error information. 165 */ 166 static int 167 elf_are_u(Rej_desc *rej) 168 { 169 Ehdr *ehdr; 170 171 /* 172 * Determine if we're an elf file. If not simply return, we don't set 173 * any rejection information as this test allows use to scroll through 174 * the objects we support (ELF, AOUT). 175 */ 176 if (fmap->fm_fsize < sizeof (Ehdr) || 177 fmap->fm_maddr[EI_MAG0] != ELFMAG0 || 178 fmap->fm_maddr[EI_MAG1] != ELFMAG1 || 179 fmap->fm_maddr[EI_MAG2] != ELFMAG2 || 180 fmap->fm_maddr[EI_MAG3] != ELFMAG3) { 181 return (0); 182 } 183 184 /* 185 * Check class and encoding. 186 */ 187 /* LINTED */ 188 ehdr = (Ehdr *)fmap->fm_maddr; 189 if (ehdr->e_ident[EI_CLASS] != M_CLASS) { 190 rej->rej_type = SGS_REJ_CLASS; 191 rej->rej_info = (uint_t)ehdr->e_ident[EI_CLASS]; 192 return (0); 193 } 194 if (ehdr->e_ident[EI_DATA] != M_DATA) { 195 rej->rej_type = SGS_REJ_DATA; 196 rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA]; 197 return (0); 198 } 199 if ((ehdr->e_type != ET_REL) && (ehdr->e_type != ET_EXEC) && 200 (ehdr->e_type != ET_DYN)) { 201 rej->rej_type = SGS_REJ_TYPE; 202 rej->rej_info = (uint_t)ehdr->e_type; 203 return (0); 204 } 205 206 /* 207 * Verify machine specific flags, and hardware capability requirements. 208 */ 209 if ((elf_mach_flags_check(rej, ehdr) == 0) || 210 ((rtld_flags2 & RT_FL2_HWCAP) && (hwcap_check(rej, ehdr) == 0))) 211 return (0); 212 213 /* 214 * Verify ELF version. ??? is this too restrictive ??? 215 */ 216 if (ehdr->e_version > EV_CURRENT) { 217 rej->rej_type = SGS_REJ_VERSION; 218 rej->rej_info = (uint_t)ehdr->e_version; 219 return (0); 220 } 221 return (1); 222 } 223 224 /* 225 * The runtime linker employs lazy loading to provide the libraries needed for 226 * debugging, preloading .o's and dldump(). As these are seldom used, the 227 * standard startup of ld.so.1 doesn't initialize all the information necessary 228 * to perform plt relocation on ld.so.1's link-map. The first time lazy loading 229 * is called we get here to perform these initializations: 230 * 231 * o elf_needed() is called to set up the DYNINFO() indexes for each lazy 232 * dependency. Typically, for all other objects, this is called during 233 * analyze_so(), but as ld.so.1 is set-contained we skip this processing. 234 * 235 * o For intel, ld.so.1's JMPSLOT relocations need relative updates. These 236 * are by default skipped thus delaying all relative relocation processing 237 * on every invocation of ld.so.1. 238 */ 239 int 240 elf_rtld_load() 241 { 242 Lm_list *lml = &lml_rtld; 243 Rt_map *lmp = lml->lm_head; 244 245 if (lml->lm_flags & LML_FLG_PLTREL) 246 return (1); 247 248 /* 249 * As we need to refer to the DYNINFO() information, insure that it has 250 * been initialized. 251 */ 252 if (elf_needed(lml, ALO_DATA, lmp) == 0) 253 return (0); 254 255 #if defined(i386) 256 /* 257 * This is a kludge to give ld.so.1 a performance benefit on i386. 258 * It's based around two factors. 259 * 260 * o JMPSLOT relocations (PLT's) actually need a relative relocation 261 * applied to the GOT entry so that they can find PLT0. 262 * 263 * o ld.so.1 does not exercise *any* PLT's before it has made a call 264 * to elf_lazy_load(). This is because all dynamic dependencies 265 * are recorded as lazy dependencies. 266 */ 267 (void) elf_reloc_relacount((ulong_t)JMPREL(lmp), 268 (ulong_t)(PLTRELSZ(lmp) / RELENT(lmp)), (ulong_t)RELENT(lmp), 269 (ulong_t)ADDR(lmp)); 270 #endif 271 272 lml->lm_flags |= LML_FLG_PLTREL; 273 return (1); 274 } 275 276 /* 277 * Lazy load an object. 278 */ 279 Rt_map * 280 elf_lazy_load(Rt_map *clmp, uint_t ndx, const char *sym) 281 { 282 Rt_map *nlmp, *hlmp; 283 Dyninfo *dip = &DYNINFO(clmp)[ndx]; 284 uint_t flags = 0; 285 Pnode *pnp; 286 const char *name; 287 Lm_list *lml = LIST(clmp); 288 Lm_cntl *lmc; 289 Aliste lmco; 290 291 /* 292 * If this dependency has already been processed, we're done. 293 */ 294 if (((nlmp = (Rt_map *)dip->di_info) != 0) || 295 (dip->di_flags & FLG_DI_PROCESSD)) 296 return (nlmp); 297 298 /* 299 * Determine the initial dependency name, and indicate that this 300 * dependencies processing has initiated. 301 */ 302 name = STRTAB(clmp) + DYN(clmp)[ndx].d_un.d_val; 303 DBG_CALL(Dbg_file_lazyload(name, NAME(clmp), sym)); 304 if (lml->lm_flags & LML_FLG_TRC_ENABLE) 305 dip->di_flags |= FLG_DI_PROCESSD; 306 307 if (dip->di_flags & FLG_DI_GROUP) 308 flags |= (FLG_RT_SETGROUP | FLG_RT_HANDLE); 309 310 /* 311 * Expand the requested name if necessary. 312 */ 313 if ((pnp = elf_fix_name(name, clmp, PN_SER_NEEDED)) == 0) 314 return (0); 315 316 /* 317 * Provided the object on the head of the link-map has completed its 318 * relocation, create a new link-map control list for this request. 319 */ 320 hlmp = lml->lm_head; 321 if (FLAGS(hlmp) & FLG_RT_RELOCED) { 322 if ((lmc = alist_append(&(lml->lm_lists), 0, sizeof (Lm_cntl), 323 AL_CNT_LMLISTS)) == 0) { 324 remove_pnode(pnp); 325 return (0); 326 } 327 lmco = (Aliste)((char *)lmc - (char *)lml->lm_lists); 328 } else { 329 lmc = 0; 330 lmco = ALO_DATA; 331 } 332 333 /* 334 * Load the associated object. 335 */ 336 dip->di_info = nlmp = 337 load_one(lml, lmco, pnp, clmp, MODE(clmp), flags, 0); 338 339 /* 340 * Remove any expanded pathname infrastructure. Reduce the pending lazy 341 * dependency count of the caller, together with the link-map lists 342 * count of objects that still have lazy dependencies pending. 343 */ 344 remove_pnode(pnp); 345 if (--LAZY(clmp) == 0) 346 LIST(clmp)->lm_lazy--; 347 348 /* 349 * Finish processing the objects associated with this request. 350 */ 351 if (nlmp && ((analyze_lmc(lml, lmco, nlmp) == 0) || 352 (relocate_lmc(lml, lmco, nlmp) == 0))) 353 dip->di_info = nlmp = 0; 354 355 /* 356 * If the dependency has been successfully processed, and it is part of 357 * a link-map control list that is equivalent, or less, that the callers 358 * control list, create an association between the caller and this 359 * dependency. If this dependency isn't yet apart of the callers 360 * link-map control list, then it is still apart of a list that is being 361 * relocated. As the relocation of an object on this list might still 362 * fail, we can't yet bind the caller to this object. To do so, would 363 * be locking the object so that it couldn't be deleted. Mark this 364 * object as free, and it will be reprocessed when this dependency is 365 * next referenced. 366 */ 367 if (nlmp) { 368 if (CNTL(nlmp) <= CNTL(clmp)) { 369 if (bind_one(clmp, nlmp, BND_NEEDED) == 0) 370 dip->di_info = nlmp = 0; 371 } else { 372 dip->di_info = 0; 373 dip->di_flags &= ~FLG_DI_PROCESSD; 374 if (LAZY(clmp)++ == 0) 375 LIST(clmp)->lm_lazy++; 376 } 377 } 378 379 /* 380 * After a successful load, any objects collected on the new link-map 381 * control list will have been moved to the callers link-map control 382 * list. This control list can now be deleted. 383 */ 384 if (lmc) { 385 if (nlmp == 0) 386 remove_incomplete(lml, lmco); 387 remove_cntl(lml, lmco); 388 } 389 390 return (nlmp); 391 } 392 393 394 /* 395 * Return the entry point of the ELF executable. 396 */ 397 static ulong_t 398 elf_entry_pt(void) 399 { 400 return (ENTRY(lml_main.lm_head)); 401 } 402 403 /* 404 * Unmap a given ELF shared object from the address space. 405 */ 406 static void 407 elf_unmap_so(Rt_map *lmp) 408 { 409 caddr_t addr; 410 size_t size; 411 Mmap *mmaps; 412 413 /* 414 * If this link map represents a relocatable object concatenation, then 415 * the image was simply generated in allocated memory. Free the memory. 416 * 417 * Note: the memory was originally allocated in the libelf:_elf_outmap 418 * routine and would normally have been free'd in elf_outsync(), but 419 * because we 'interpose' on that routine the memory wasn't free'd at 420 * that time. 421 */ 422 if (FLAGS(lmp) & FLG_RT_IMGALLOC) { 423 free((void *)ADDR(lmp)); 424 return; 425 } 426 427 /* 428 * If padding was enabled via rtld_db, then we have at least one page 429 * in front of the image - and possibly a trailing page. 430 * Unmap the front page first: 431 */ 432 if (PADSTART(lmp) != ADDR(lmp)) { 433 addr = (caddr_t)M_PTRUNC(PADSTART(lmp)); 434 size = ADDR(lmp) - (ulong_t)addr; 435 (void) munmap(addr, size); 436 } 437 438 /* 439 * Unmap any trailing padding. 440 */ 441 if (M_PROUND((PADSTART(lmp) + PADIMLEN(lmp))) > 442 M_PROUND(ADDR(lmp) + MSIZE(lmp))) { 443 addr = (caddr_t)M_PROUND(ADDR(lmp) + MSIZE(lmp)); 444 size = M_PROUND(PADSTART(lmp) + PADIMLEN(lmp)) - (ulong_t)addr; 445 (void) munmap(addr, size); 446 } 447 448 /* 449 * Unmmap all mapped segments. 450 */ 451 for (mmaps = MMAPS(lmp); mmaps->m_vaddr; mmaps++) 452 (void) munmap(mmaps->m_vaddr, mmaps->m_msize); 453 } 454 455 /* 456 * Determine if a dependency requires a particular version and if so verify 457 * that the version exists in the dependency. 458 */ 459 static int 460 elf_verify_vers(const char *name, Rt_map *clmp, Rt_map *nlmp) 461 { 462 Verneed *vnd = VERNEED(clmp); 463 int _num, num = VERNEEDNUM(clmp); 464 char *cstrs = (char *)STRTAB(clmp); 465 Lm_list *lml = LIST(clmp); 466 467 /* 468 * Traverse the callers version needed information and determine if any 469 * specific versions are required from the dependency. 470 */ 471 for (_num = 1; _num <= num; _num++, 472 vnd = (Verneed *)((Xword)vnd + vnd->vn_next)) { 473 Half cnt = vnd->vn_cnt; 474 Vernaux *vnap; 475 char *nstrs, *need; 476 477 /* 478 * Determine if a needed entry matches this dependency. 479 */ 480 need = (char *)(cstrs + vnd->vn_file); 481 if (strcmp(name, need) != 0) 482 continue; 483 484 DBG_CALL(Dbg_ver_need_title(NAME(clmp))); 485 if ((lml->lm_flags & LML_FLG_TRC_VERBOSE) && 486 ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0)) 487 (void) printf(MSG_INTL(MSG_LDD_VER_FIND), name); 488 489 /* 490 * Validate that each version required actually exists in the 491 * dependency. 492 */ 493 nstrs = (char *)STRTAB(nlmp); 494 495 for (vnap = (Vernaux *)((Xword)vnd + vnd->vn_aux); cnt; 496 cnt--, vnap = (Vernaux *)((Xword)vnap + vnap->vna_next)) { 497 char *version, *define; 498 Verdef *vdf = VERDEF(nlmp); 499 ulong_t _num, num = VERDEFNUM(nlmp); 500 int found = 0; 501 502 version = (char *)(cstrs + vnap->vna_name); 503 DBG_CALL(Dbg_ver_need_entry(0, need, version)); 504 505 for (_num = 1; _num <= num; _num++, 506 vdf = (Verdef *)((Xword)vdf + vdf->vd_next)) { 507 Verdaux *vdap; 508 509 if (vnap->vna_hash != vdf->vd_hash) 510 continue; 511 512 vdap = (Verdaux *)((Xword)vdf + vdf->vd_aux); 513 define = (char *)(nstrs + vdap->vda_name); 514 if (strcmp(version, define) != 0) 515 continue; 516 517 found++; 518 break; 519 } 520 521 /* 522 * If we're being traced print out any matched version 523 * when the verbose (-v) option is in effect. Always 524 * print any unmatched versions. 525 */ 526 if (lml->lm_flags & LML_FLG_TRC_ENABLE) { 527 if (found) { 528 if (!(lml->lm_flags & LML_FLG_TRC_VERBOSE)) 529 continue; 530 531 (void) printf(MSG_ORIG(MSG_LDD_VER_FOUND), 532 need, version, NAME(nlmp)); 533 } else { 534 if (rtld_flags & RT_FL_SILENCERR) 535 continue; 536 537 (void) printf(MSG_INTL(MSG_LDD_VER_NFOUND), 538 need, version); 539 } 540 continue; 541 } 542 543 /* 544 * If the version hasn't been found then this is a 545 * candidate for a fatal error condition. Weak 546 * version definition requirements are silently 547 * ignored. Also, if the image inspected for a version 548 * definition has no versioning recorded at all then 549 * silently ignore this (this provides better backward 550 * compatibility to old images created prior to 551 * versioning being available). Both of these skipped 552 * diagnostics are available under tracing (see above). 553 */ 554 if ((found == 0) && (num != 0) && 555 (!(vnap->vna_flags & VER_FLG_WEAK))) { 556 eprintf(ERR_FATAL, MSG_INTL(MSG_VER_NFOUND), 557 need, version, NAME(clmp)); 558 return (0); 559 } 560 } 561 return (1); 562 } 563 return (1); 564 } 565 566 /* 567 * Search through the dynamic section for DT_NEEDED entries and perform one 568 * of two functions. If only the first argument is specified then load the 569 * defined shared object, otherwise add the link map representing the defined 570 * link map the the dlopen list. 571 */ 572 static int 573 elf_needed(Lm_list *lml, Aliste lmco, Rt_map *clmp) 574 { 575 Dyn *dyn; 576 ulong_t ndx = 0; 577 uint_t lazy = 0, flags = 0; 578 Word lmflags = lml->lm_flags; 579 Word lmtflags = lml->lm_tflags; 580 581 /* 582 * Process each shared object on needed list. 583 */ 584 if (DYN(clmp) == 0) 585 return (1); 586 587 for (dyn = (Dyn *)DYN(clmp); dyn->d_tag != DT_NULL; dyn++, ndx++) { 588 Dyninfo *dip = &DYNINFO(clmp)[ndx]; 589 Rt_map *nlmp = 0; 590 char *name; 591 int silent = 0; 592 Pnode *pnp; 593 594 switch (dyn->d_tag) { 595 case DT_POSFLAG_1: 596 if ((dyn->d_un.d_val & DF_P1_LAZYLOAD) && 597 !(lmtflags & LML_TFLG_NOLAZYLD)) 598 lazy = 1; 599 if (dyn->d_un.d_val & DF_P1_GROUPPERM) 600 flags = (FLG_RT_SETGROUP | FLG_RT_HANDLE); 601 continue; 602 case DT_NEEDED: 603 case DT_USED: 604 dip->di_flags |= FLG_DI_NEEDED; 605 if (flags) 606 dip->di_flags |= FLG_DI_GROUP; 607 608 name = (char *)STRTAB(clmp) + dyn->d_un.d_val; 609 610 /* 611 * NOTE, libc.so.1 can't be lazy loaded. Although a 612 * lazy position flag won't be produced when a RTLDINFO 613 * .dynamic entry is found (introduced with the UPM in 614 * Solaris 10), it was possible to mark libc for lazy 615 * loading on previous releases. To reduce the overhead 616 * of testing for this occurrence, only carry out this 617 * check for the first object on the link-map list 618 * (there aren't many applications built without libc). 619 */ 620 if (lazy && (lml->lm_head == clmp) && 621 (strcmp(name, MSG_ORIG(MSG_FIL_LIBC)) == 0)) 622 lazy = 0; 623 624 /* 625 * Don't bring in lazy loaded objects yet unless we've 626 * been asked to attempt to load all available objects 627 * (crle(1) sets LD_FLAGS=loadavail). Even under 628 * RTLD_NOW we don't process this - RTLD_NOW will cause 629 * relocation processing which in turn might trigger 630 * lazy loading, but its possible that the object has a 631 * lazy loaded file with no bindings (i.e., it should 632 * never have been a dependency in the first place). 633 */ 634 if (lazy) { 635 if ((lmflags & LML_FLG_LOADAVAIL) == 0) { 636 LAZY(clmp)++; 637 lazy = flags = 0; 638 continue; 639 } 640 641 /* 642 * Silence any error messages - see description 643 * under elf_lookup_filtee(). 644 */ 645 if ((rtld_flags & RT_FL_SILENCERR) == 0) { 646 rtld_flags |= RT_FL_SILENCERR; 647 silent = 1; 648 } 649 } 650 break; 651 case DT_AUXILIARY: 652 dip->di_flags |= FLG_DI_AUXFLTR; 653 lazy = flags = 0; 654 continue; 655 case DT_SUNW_AUXILIARY: 656 dip->di_flags |= (FLG_DI_AUXFLTR | FLG_DI_SYMFLTR); 657 lazy = flags = 0; 658 continue; 659 case DT_FILTER: 660 dip->di_flags |= FLG_DI_STDFLTR; 661 lazy = flags = 0; 662 continue; 663 case DT_SUNW_FILTER: 664 dip->di_flags |= (FLG_DI_STDFLTR | FLG_DI_SYMFLTR); 665 lazy = flags = 0; 666 continue; 667 default: 668 lazy = flags = 0; 669 continue; 670 } 671 672 DBG_CALL(Dbg_file_needed(name, NAME(clmp))); 673 if (lml->lm_flags & LML_FLG_TRC_ENABLE) 674 dip->di_flags |= FLG_DI_PROCESSD; 675 676 /* 677 * Establish the objects name, load it and establish a binding 678 * with the caller. 679 */ 680 if (((pnp = elf_fix_name(name, clmp, PN_SER_NEEDED)) == 0) || 681 ((nlmp = load_one(lml, lmco, pnp, clmp, MODE(clmp), 682 flags, 0)) == 0) || (bind_one(clmp, nlmp, BND_NEEDED) == 0)) 683 nlmp = 0; 684 685 /* 686 * Clean up any infrastructure, including the removal of the 687 * error suppression state, if it had been previously set in 688 * this routine. 689 */ 690 if (pnp) 691 remove_pnode(pnp); 692 if (silent) 693 rtld_flags &= ~RT_FL_SILENCERR; 694 lazy = flags = 0; 695 if ((dip->di_info = (void *)nlmp) == 0) { 696 /* 697 * If the object could not be mapped, continue if error 698 * suppression is established or we're here with ldd(1). 699 */ 700 if ((MODE(clmp) & RTLD_CONFGEN) || (lmflags & 701 (LML_FLG_LOADAVAIL | LML_FLG_TRC_ENABLE))) 702 continue; 703 else 704 return (0); 705 } 706 } 707 708 if (LAZY(clmp)) 709 lml->lm_lazy++; 710 711 return (1); 712 } 713 714 static int 715 elf_map_check(const char *name, caddr_t vaddr, Off size) 716 { 717 prmap_t *maps, *_maps; 718 int pfd, num, _num; 719 caddr_t eaddr = vaddr + size; 720 int err; 721 722 /* 723 * If memory reservations have been established for alternative objects 724 * determine if this object falls within the reservation, if it does no 725 * further checking is required. 726 */ 727 if (rtld_flags & RT_FL_MEMRESV) { 728 Rtc_head *head = (Rtc_head *)config->c_bgn; 729 730 if ((vaddr >= (caddr_t)(uintptr_t)head->ch_resbgn) && 731 (eaddr <= (caddr_t)(uintptr_t)head->ch_resend)) 732 return (0); 733 } 734 735 /* 736 * Determine the mappings presently in use by this process. 737 */ 738 if ((pfd = pr_open()) == FD_UNAVAIL) 739 return (1); 740 741 if (ioctl(pfd, PIOCNMAP, (void *)&num) == -1) { 742 err = errno; 743 eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_PROC), name, strerror(err)); 744 return (1); 745 } 746 747 if ((maps = malloc((num + 1) * sizeof (prmap_t))) == 0) 748 return (1); 749 750 if (ioctl(pfd, PIOCMAP, (void *)maps) == -1) { 751 err = errno; 752 eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_PROC), name, strerror(err)); 753 free(maps); 754 return (1); 755 } 756 757 /* 758 * Determine if the supplied address clashes with any of the present 759 * process mappings. 760 */ 761 for (_num = 0, _maps = maps; _num < num; _num++, _maps++) { 762 caddr_t _eaddr = _maps->pr_vaddr + _maps->pr_size; 763 Rt_map *lmp; 764 const char *str; 765 766 if ((eaddr < _maps->pr_vaddr) || (vaddr >= _eaddr)) 767 continue; 768 769 /* 770 * We have a memory clash. See if one of the known dynamic 771 * dependency mappings represents this space so as to provide 772 * the user a more meaningful message. 773 */ 774 if ((lmp = _caller(vaddr, 0)) != 0) 775 str = NAME(lmp); 776 else 777 str = MSG_INTL(MSG_STR_UNKNOWN); 778 779 eprintf(ERR_FATAL, MSG_INTL(MSG_GEN_MAPINUSE), name, 780 EC_ADDR(vaddr), EC_OFF(size), str); 781 return (1); 782 } 783 free(maps); 784 return (0); 785 } 786 787 /* 788 * Obtain a memory reservation. On newer systems, both MAP_ANON and MAP_ALIGN 789 * are used to obtained an aligned reservation from anonymous memory. If 790 * MAP_ANON isn't available, then MAP_ALIGN isn't either, so obtain a standard 791 * reservation using the file as backing. 792 */ 793 static Am_ret 794 elf_map_reserve(const char *name, caddr_t *maddr, Off msize, int mperm, 795 int fd, Xword align) 796 { 797 Am_ret amret; 798 int mflag = MAP_PRIVATE | MAP_NORESERVE; 799 800 #if defined(MAP_ALIGN) 801 if ((rtld_flags2 & RT_FL2_NOMALIGN) == 0) { 802 mflag |= MAP_ALIGN; 803 *maddr = (caddr_t)align; 804 } 805 #endif 806 if ((amret = anon_map(maddr, msize, PROT_NONE, mflag)) == AM_ERROR) 807 return (amret); 808 809 if (amret == AM_OK) 810 return (AM_OK); 811 812 /* 813 * If an anonymous memory request failed (which should only be the 814 * case if it is unsupported on the system we're running on), establish 815 * the initial mapping directly from the file. 816 */ 817 *maddr = 0; 818 if ((*maddr = mmap(*maddr, msize, mperm, MAP_PRIVATE, 819 fd, 0)) == MAP_FAILED) { 820 int err = errno; 821 eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_MMAP), name, strerror(err)); 822 return (AM_ERROR); 823 } 824 return (AM_NOSUP); 825 } 826 827 static void * 828 elf_map_textdata(caddr_t addr, Off flen, int mperm, int phdr_mperm, int mflag, 829 int fd, Off foff) 830 { 831 #if defined(MAP_TEXT) && defined(MAP_INITDATA) 832 static int notd = 0; 833 834 /* 835 * If MAP_TEXT and MAP_INITDATA are available, select the appropriate 836 * flag. 837 */ 838 if (notd == 0) { 839 if ((phdr_mperm & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC) 840 mflag |= MAP_TEXT; 841 else 842 mflag |= MAP_INITDATA; 843 } 844 #endif 845 if (mmap((caddr_t)addr, flen, mperm, mflag, fd, foff) != MAP_FAILED) 846 return (0); 847 848 #if defined(MAP_TEXT) && defined(MAP_INITDATA) 849 if ((notd == 0) && (errno == EINVAL)) { 850 /* 851 * MAP_TEXT and MAP_INITDATA may not be supported on this 852 * platform, try again without. 853 */ 854 notd = 1; 855 mflag &= ~(MAP_TEXT | MAP_INITDATA); 856 857 return (mmap((caddr_t)addr, flen, mperm, mflag, fd, foff)); 858 } 859 #endif 860 return (MAP_FAILED); 861 } 862 863 /* 864 * Map in a file. 865 */ 866 static caddr_t 867 elf_map_it( 868 const char *name, /* actual name stored for pathname */ 869 Off fsize, /* total mapping claim of the file */ 870 Ehdr *ehdr, /* ELF header of file */ 871 Phdr *fphdr, /* first loadable Phdr */ 872 Phdr *lphdr, /* last loadable Phdr */ 873 Phdr **rrphdr, /* return first Phdr in reservation */ 874 caddr_t *rraddr, /* return start of reservation */ 875 Off *rrsize, /* return total size of reservation */ 876 int fixed, /* image is resolved to a fixed addr */ 877 int fd, /* images file descriptor */ 878 Xword align, /* image segments maximum alignment */ 879 Mmap *mmaps, /* mmap information array and */ 880 uint_t *mmapcnt) /* mapping count */ 881 { 882 caddr_t raddr; /* reservation address */ 883 Off rsize; /* reservation size */ 884 Phdr *phdr; /* working program header poiner */ 885 caddr_t maddr; /* working mmap address */ 886 caddr_t faddr; /* working file address */ 887 size_t padsize; /* object padding requirement */ 888 size_t padpsize = 0; /* padding size rounded to next page */ 889 size_t padmsize = 0; /* padding size rounded for alignment */ 890 int skipfseg; /* skip mapping first segment */ 891 int mperm; /* segment permissions */ 892 Am_ret amret = AM_NOSUP; 893 894 /* 895 * If padding is required extend both the front and rear of the image. 896 * To insure the image itself is mapped at the correct alignment the 897 * initial padding is rounded up to the nearest page. Once the image is 898 * mapped the excess can be pruned to the nearest page required for the 899 * actual padding itself. 900 */ 901 if ((padsize = r_debug.rtd_objpad) != 0) { 902 padpsize = M_PROUND(padsize); 903 if (fixed) 904 padmsize = padpsize; 905 else 906 padmsize = S_ROUND(padsize, align); 907 } 908 909 /* 910 * Determine the initial permissions used to map in the first segment. 911 * If this segments memsz is greater that its filesz then the difference 912 * must be zeroed. Make sure this segment is writable. 913 */ 914 mperm = 0; 915 if (fphdr->p_flags & PF_R) 916 mperm |= PROT_READ; 917 if (fphdr->p_flags & PF_X) 918 mperm |= PROT_EXEC; 919 if ((fphdr->p_flags & PF_W) || (fphdr->p_memsz > fphdr->p_filesz)) 920 mperm |= PROT_WRITE; 921 922 /* 923 * Determine whether or not to let system reserve address space based on 924 * whether this is a dynamic executable (addresses in object are fixed) 925 * or a shared object (addresses in object are relative to the objects' 926 * base). 927 */ 928 if (fixed) { 929 /* 930 * Determine the reservation address and size, and insure that 931 * this reservation isn't already in use. 932 */ 933 faddr = maddr = (caddr_t)M_PTRUNC((ulong_t)fphdr->p_vaddr); 934 raddr = maddr - padpsize; 935 rsize = fsize + padpsize + padsize; 936 937 if (lml_main.lm_head) { 938 if (elf_map_check(name, raddr, rsize) != 0) 939 return (0); 940 } 941 942 /* 943 * As this is a fixed image, all segments must be individually 944 * mapped. 945 */ 946 skipfseg = 0; 947 948 } else { 949 size_t esize; 950 951 /* 952 * If this isn't a fixed image, reserve enough address space for 953 * the entire image to be mapped. The amount of reservation is 954 * the range between the beginning of the first, and end of the 955 * last loadable segment, together with any padding, plus the 956 * alignment of the first segment. 957 * 958 * The optimal reservation is made as a no-reserve mapping from 959 * anonymous memory. Each segment is then mapped into this 960 * reservation. If the anonymous mapping capability isn't 961 * available, the reservation is obtained from the file itself. 962 * In this case the first segment of the image is mapped as part 963 * of the reservation, thus only the following segments need to 964 * be remapped. 965 */ 966 rsize = fsize + padmsize + padsize; 967 if ((amret = elf_map_reserve(name, &raddr, rsize, mperm, 968 fd, align)) == AM_ERROR) 969 return (0); 970 maddr = raddr + padmsize; 971 faddr = (caddr_t)S_ROUND((Off)maddr, align); 972 973 /* 974 * If this reservation has been obtained from anonymous memory, 975 * then all segments must be individually mapped. Otherwise, 976 * the first segment heads the reservation. 977 */ 978 if (amret == AM_OK) 979 skipfseg = 0; 980 else 981 skipfseg = 1; 982 983 /* 984 * For backward compatibility (where MAP_ALIGN isn't available), 985 * insure the alignment of the reservation is adequate for this 986 * object, and if not remap the object to obtain the correct 987 * alignment. 988 */ 989 if (faddr != maddr) { 990 (void) munmap(raddr, rsize); 991 992 rsize += align; 993 if ((amret = elf_map_reserve(name, &raddr, rsize, mperm, 994 fd, align)) == AM_ERROR) 995 return (0); 996 997 maddr = faddr = (caddr_t)S_ROUND((Off)(raddr + 998 padpsize), align); 999 1000 esize = maddr - raddr + padpsize; 1001 1002 /* 1003 * As ths image has been realigned, the first segment 1004 * of the file needs to be remapped to its correct 1005 * location. 1006 */ 1007 skipfseg = 0; 1008 } else 1009 esize = padmsize - padpsize; 1010 1011 /* 1012 * If this reservation included padding, remove any excess for 1013 * the start of the image (the padding was adjusted to insure 1014 * the image was aligned appropriately). 1015 */ 1016 if (esize) { 1017 (void) munmap(raddr, esize); 1018 raddr += esize; 1019 rsize -= esize; 1020 } 1021 } 1022 1023 /* 1024 * At this point we know the initial location of the image, and its 1025 * size. Pass these back to the caller for inclusion in the link-map 1026 * that will eventually be created. 1027 */ 1028 *rraddr = raddr; 1029 *rrsize = rsize; 1030 1031 /* 1032 * The first loadable segment is now pointed to by maddr. This segment 1033 * will eventually contain the elf header and program headers, so reset 1034 * the program header. Pass this back to the caller for inclusion in 1035 * the link-map so it can be used for later unmapping operations. 1036 */ 1037 /* LINTED */ 1038 *rrphdr = (Phdr *)((char *)maddr + ehdr->e_phoff); 1039 1040 /* 1041 * If padding is required at the front of the image, obtain that now. 1042 * Note, if we've already obtained a reservation from anonymous memory 1043 * then this reservation will already include suitable padding. 1044 * Otherwise this reservation is backed by the file, or in the case of 1045 * a fixed image, doesn't yet exist. Map the padding so that it is 1046 * suitably protected (PROT_NONE), and insure the first segment of the 1047 * file is mapped to its correct location. 1048 */ 1049 if (padsize) { 1050 if (amret == AM_NOSUP) { 1051 if (dz_map(raddr, padpsize, PROT_NONE, (MAP_PRIVATE | 1052 MAP_FIXED | MAP_NORESERVE)) == MAP_FAILED) 1053 return (0); 1054 1055 skipfseg = 0; 1056 } 1057 rsize -= padpsize; 1058 } 1059 1060 /* 1061 * Map individual segments. For a fixed image, these will each be 1062 * unique mappings. For a reservation these will fill in the 1063 * reservation. 1064 */ 1065 for (phdr = fphdr; phdr <= lphdr; 1066 phdr = (Phdr *)((Off)phdr + ehdr->e_phentsize)) { 1067 caddr_t addr; 1068 Off mlen, flen; 1069 size_t size; 1070 1071 /* 1072 * Skip non-loadable segments or segments that don't occupy 1073 * any memory. 1074 */ 1075 if (((phdr->p_type != PT_LOAD) && 1076 (phdr->p_type != PT_SUNWBSS)) || (phdr->p_memsz == 0)) 1077 continue; 1078 1079 /* 1080 * Establish this segments address relative to our base. 1081 */ 1082 addr = (caddr_t)M_PTRUNC((ulong_t)(phdr->p_vaddr + 1083 (fixed ? 0 : faddr))); 1084 1085 /* 1086 * Determine the mapping protection from the segment attributes. 1087 * Also determine the etext address from the last loadable 1088 * segment which has permissions but no write access. 1089 */ 1090 mperm = 0; 1091 if (phdr->p_flags) { 1092 if (phdr->p_flags & PF_R) 1093 mperm |= PROT_READ; 1094 if (phdr->p_flags & PF_X) 1095 mperm |= PROT_EXEC; 1096 if (phdr->p_flags & PF_W) 1097 mperm |= PROT_WRITE; 1098 else 1099 fmap->fm_etext = phdr->p_vaddr + phdr->p_memsz + 1100 (ulong_t)(fixed ? 0 : faddr); 1101 } 1102 1103 /* 1104 * Determine the type of mapping required. 1105 */ 1106 if (phdr->p_type == PT_SUNWBSS) { 1107 /* 1108 * Potentially, we can defer the loading of any SUNWBSS 1109 * segment, depending on whether the symbols it provides 1110 * have been bound to. In this manner, large segments 1111 * that are interposed upon between shared libraries 1112 * may not require mapping. Note, that the mapping 1113 * information is recorded in our mapping descriptor at 1114 * this time. 1115 */ 1116 mlen = phdr->p_memsz; 1117 flen = 0; 1118 1119 } else if ((phdr->p_filesz == 0) && (phdr->p_flags == 0)) { 1120 /* 1121 * If this segment has no backing file and no flags 1122 * specified, then it defines a reservation. At this 1123 * point all standard loadable segments will have been 1124 * processed. The segment reservation is mapped 1125 * directly from /dev/null. 1126 */ 1127 if (nu_map((caddr_t)addr, phdr->p_memsz, PROT_NONE, 1128 MAP_FIXED | MAP_PRIVATE) == MAP_FAILED) 1129 return (0); 1130 1131 mlen = phdr->p_memsz; 1132 flen = 0; 1133 1134 } else if (phdr->p_filesz == 0) { 1135 /* 1136 * If this segment has no backing file then it defines a 1137 * nobits segment and is mapped directly from /dev/zero. 1138 */ 1139 if (dz_map((caddr_t)addr, phdr->p_memsz, mperm, 1140 MAP_FIXED | MAP_PRIVATE) == MAP_FAILED) 1141 return (0); 1142 1143 mlen = phdr->p_memsz; 1144 flen = 0; 1145 1146 } else { 1147 Off foff; 1148 1149 /* 1150 * This mapping originates from the file. Determine the 1151 * file offset to which the mapping will be directed 1152 * (must be aligned) and how much to map (might be more 1153 * than the file in the case of .bss). 1154 */ 1155 foff = M_PTRUNC((ulong_t)phdr->p_offset); 1156 mlen = phdr->p_memsz + (phdr->p_offset - foff); 1157 flen = phdr->p_filesz + (phdr->p_offset - foff); 1158 1159 /* 1160 * If this is a non-fixed, non-anonymous mapping, and no 1161 * padding is involved, then the first loadable segment 1162 * is already part of the initial reservation. In this 1163 * case there is no need to remap this segment. 1164 */ 1165 if ((skipfseg == 0) || (phdr != fphdr)) { 1166 int phdr_mperm = mperm; 1167 /* 1168 * If this segments memsz is greater that its 1169 * filesz then the difference must be zeroed. 1170 * Make sure this segment is writable. 1171 */ 1172 if (phdr->p_memsz > phdr->p_filesz) 1173 mperm |= PROT_WRITE; 1174 1175 if (elf_map_textdata((caddr_t)addr, flen, 1176 mperm, phdr_mperm, 1177 (MAP_FIXED | MAP_PRIVATE), fd, foff) == 1178 MAP_FAILED) { 1179 int err = errno; 1180 eprintf(ERR_FATAL, 1181 MSG_INTL(MSG_SYS_MMAP), name, 1182 strerror(err)); 1183 return (0); 1184 } 1185 } 1186 1187 /* 1188 * If the memory occupancy of the segment overflows the 1189 * definition in the file, we need to "zero out" the end 1190 * of the mapping we've established, and if necessary, 1191 * map some more space from /dev/zero. Note, zero'ed 1192 * memory must end on a double word boundary to satisfy 1193 * zero(). 1194 */ 1195 if (phdr->p_memsz > phdr->p_filesz) { 1196 caddr_t zaddr; 1197 size_t zlen, zplen; 1198 Off fend; 1199 1200 foff = (Off)(phdr->p_vaddr + phdr->p_filesz + 1201 (fixed ? 0 : faddr)); 1202 zaddr = (caddr_t)M_PROUND(foff); 1203 zplen = (size_t)(zaddr - foff); 1204 1205 fend = (Off)S_DROUND((size_t)(phdr->p_vaddr + 1206 phdr->p_memsz + (fixed ? 0 : faddr))); 1207 zlen = (size_t)(fend - foff); 1208 1209 /* 1210 * Determine whether the number of bytes that 1211 * must be zero'ed overflow to the next page. 1212 * If not, simply clear the exact bytes 1213 * (filesz to memsz) from this page. Otherwise, 1214 * clear the remaining bytes of this page, and 1215 * map an following pages from /dev/zero. 1216 */ 1217 if (zlen < zplen) 1218 zero((caddr_t)foff, (long)zlen); 1219 else { 1220 zero((caddr_t)foff, (long)zplen); 1221 1222 if ((zlen = (fend - (Off)zaddr)) > 0) { 1223 if (dz_map(zaddr, zlen, mperm, 1224 MAP_FIXED | MAP_PRIVATE) == 1225 MAP_FAILED) 1226 return (0); 1227 } 1228 } 1229 } 1230 } 1231 1232 /* 1233 * Unmap anything from the last mapping address to this one and 1234 * update the mapping claim pointer. 1235 */ 1236 if ((fixed == 0) && ((size = addr - maddr) != 0)) { 1237 (void) munmap(maddr, size); 1238 rsize -= size; 1239 } 1240 1241 /* 1242 * Retain this segments mapping information. 1243 */ 1244 mmaps[*mmapcnt].m_vaddr = addr; 1245 mmaps[*mmapcnt].m_msize = mlen; 1246 mmaps[*mmapcnt].m_fsize = flen; 1247 mmaps[*mmapcnt].m_perm = mperm; 1248 (*mmapcnt)++; 1249 1250 maddr = addr + M_PROUND(mlen); 1251 rsize -= M_PROUND(mlen); 1252 } 1253 1254 /* 1255 * If padding is required at the end of the image, obtain that now. 1256 * Note, if we've already obtained a reservation from anonymous memory 1257 * then this reservation will already include suitable padding. 1258 */ 1259 if (padsize) { 1260 if (amret == AM_NOSUP) { 1261 /* 1262 * maddr is currently page aligned from the last segment 1263 * mapping. 1264 */ 1265 if (dz_map(maddr, padsize, PROT_NONE, (MAP_PRIVATE | 1266 MAP_FIXED | MAP_NORESERVE)) == MAP_FAILED) 1267 return (0); 1268 } 1269 maddr += padsize; 1270 rsize -= padsize; 1271 } 1272 1273 /* 1274 * Unmap any final reservation. 1275 */ 1276 if ((fixed == 0) && (rsize != 0)) 1277 (void) munmap(maddr, rsize); 1278 1279 return (faddr); 1280 } 1281 1282 /* 1283 * A null symbol interpretor. Used if a filter has no associated filtees. 1284 */ 1285 /* ARGSUSED0 */ 1286 static Sym * 1287 elf_null_find_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo) 1288 { 1289 return ((Sym *)0); 1290 } 1291 1292 /* 1293 * Disable filtee use. 1294 */ 1295 static void 1296 elf_disable_filtee(Rt_map * lmp, Dyninfo * dip) 1297 { 1298 dip->di_info = 0; 1299 1300 if ((dip->di_flags & FLG_DI_SYMFLTR) == 0) { 1301 /* 1302 * If this is an object filter, free the filtee's duplication. 1303 */ 1304 if (OBJFLTRNDX(lmp) != FLTR_DISABLED) { 1305 free(REFNAME(lmp)); 1306 REFNAME(lmp) = (char *)0; 1307 OBJFLTRNDX(lmp) = FLTR_DISABLED; 1308 1309 /* 1310 * Indicate that this filtee is no longer available. 1311 */ 1312 if (dip->di_flags & FLG_DI_STDFLTR) 1313 SYMINTP(lmp) = elf_null_find_sym; 1314 1315 } 1316 } else if (dip->di_flags & FLG_DI_STDFLTR) { 1317 /* 1318 * Indicate that this standard filtee is no longer available. 1319 */ 1320 if (SYMSFLTRCNT(lmp)) 1321 SYMSFLTRCNT(lmp)--; 1322 } else { 1323 /* 1324 * Indicate that this auxiliary filtee is no longer available. 1325 */ 1326 if (SYMAFLTRCNT(lmp)) 1327 SYMAFLTRCNT(lmp)--; 1328 } 1329 dip->di_flags &= ~MSK_DI_FILTER; 1330 } 1331 1332 /* 1333 * Find symbol interpreter - filters. 1334 * This function is called when the symbols from a shared object should 1335 * be resolved from the shared objects filtees instead of from within itself. 1336 * 1337 * A symbol name of 0 is used to trigger filtee loading. 1338 */ 1339 static Sym * 1340 _elf_lookup_filtee(Slookup *slp, Rt_map **dlmp, uint_t *binfo, uint_t ndx) 1341 { 1342 const char *name = slp->sl_name, *filtees; 1343 Rt_map *clmp = slp->sl_cmap; 1344 Rt_map *ilmp = slp->sl_imap; 1345 Pnode *pnp, **pnpp; 1346 int any; 1347 Dyninfo *dip = &DYNINFO(ilmp)[ndx]; 1348 Lm_list *lml = LIST(ilmp); 1349 Lm_cntl *lmc = 0; 1350 Aliste lmco; 1351 1352 /* 1353 * Indicate that the filter has been used. If a binding already exists 1354 * to the caller, indicate that this object is referenced. This insures 1355 * we don't generate false unreferenced diagnostics from ldd -u/U or 1356 * debugging. Don't create a binding regardless, as this filter may 1357 * have been dlopen()'ed. 1358 */ 1359 if (name && (ilmp != clmp)) { 1360 Word tracing = (LIST(clmp)->lm_flags & 1361 (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED)); 1362 1363 if (tracing || dbg_mask) { 1364 Bnd_desc ** bdpp; 1365 Aliste off; 1366 1367 FLAGS1(ilmp) |= FL1_RT_USED; 1368 1369 if ((tracing & LML_FLG_TRC_UNREF) || dbg_mask) { 1370 for (ALIST_TRAVERSE(CALLERS(ilmp), off, bdpp)) { 1371 Bnd_desc * bdp = *bdpp; 1372 1373 if (bdp->b_caller == clmp) { 1374 bdp->b_flags |= BND_REFER; 1375 break; 1376 } 1377 } 1378 } 1379 } 1380 } 1381 1382 /* 1383 * If this is the first call to process this filter, establish the 1384 * filtee list. If a configuration file exists, determine if any 1385 * filtee associations for this filter, and its filtee reference, are 1386 * defined. Otherwise, process the filtee reference. Any token 1387 * expansion is also completed at this point (i.e., $PLATFORM). 1388 */ 1389 filtees = (char *)STRTAB(ilmp) + DYN(ilmp)[ndx].d_un.d_val; 1390 if (dip->di_info == 0) { 1391 if (rtld_flags2 & RT_FL2_FLTCFG) 1392 dip->di_info = elf_config_flt(PATHNAME(ilmp), filtees); 1393 1394 if (dip->di_info == 0) { 1395 DBG_CALL(Dbg_file_filter(NAME(ilmp), filtees, 0)); 1396 if ((lml->lm_flags & 1397 (LML_FLG_TRC_VERBOSE | LML_FLG_TRC_SEARCH)) && 1398 ((FLAGS1(ilmp) & FL1_RT_LDDSTUB) == 0)) 1399 (void) printf(MSG_INTL(MSG_LDD_FIL_FILTER), 1400 NAME(ilmp), filtees); 1401 1402 if ((dip->di_info = (void *)expand_paths(ilmp, 1403 filtees, PN_SER_FILTEE, 0)) == 0) { 1404 elf_disable_filtee(ilmp, dip); 1405 return ((Sym *)0); 1406 } 1407 } 1408 } 1409 1410 /* 1411 * Traverse the filtee list, dlopen()'ing any objects specified and 1412 * using their group handle to lookup the symbol. 1413 */ 1414 for (any = 0, pnpp = (Pnode **)&(dip->di_info), pnp = *pnpp; pnp; 1415 pnpp = &pnp->p_next, pnp = * pnpp) { 1416 int mode; 1417 Grp_hdl *ghp; 1418 Rt_map *nlmp = 0; 1419 1420 if (pnp->p_len == 0) 1421 continue; 1422 1423 /* 1424 * Establish the mode of the filtee from the filter. As filtees 1425 * are loaded via a dlopen(), make sure that RTLD_GROUP is set 1426 * and the filtees aren't global. It would be nice to have 1427 * RTLD_FIRST used here also, but as filters got out long before 1428 * RTLD_FIRST was introduced it's a little too late now. 1429 */ 1430 mode = MODE(ilmp) | RTLD_GROUP; 1431 mode &= ~RTLD_GLOBAL; 1432 1433 /* 1434 * Insure that any auxiliary filter can locate symbols from its 1435 * caller. 1436 */ 1437 if (dip->di_flags & FLG_DI_AUXFLTR) 1438 mode |= RTLD_PARENT; 1439 1440 /* 1441 * Process any hardware capability directory. Establish a new 1442 * link-map control list from which to analyze any newly added 1443 * objects. Note that an lmc may already be allocated from a 1444 * previous filtee dlopen() that failed. 1445 */ 1446 if ((pnp->p_info == 0) && (pnp->p_orig & PN_TKN_HWCAP)) { 1447 if ((lmc == 0) && 1448 (FLAGS(lml->lm_head) & FLG_RT_RELOCED) && 1449 ((lmc = alist_append(&(lml->lm_lists), 0, 1450 sizeof (Lm_cntl), AL_CNT_LMLISTS)) == 0)) 1451 return ((Sym *)0); 1452 1453 if (lmc) 1454 lmco = (Aliste)((char *)lmc - 1455 (char *)lml->lm_lists); 1456 else 1457 lmco = ALO_DATA; 1458 1459 pnp = hwcap_filtees(pnpp, lmco, dip, ilmp, filtees, 1460 mode, (FLG_RT_HANDLE | FLG_RT_HWCAP)); 1461 } 1462 1463 if (pnp->p_len == 0) 1464 continue; 1465 1466 /* 1467 * Process an individual filtee. 1468 */ 1469 if (pnp->p_info == 0) { 1470 const char *filtee = pnp->p_name; 1471 int audit = 0; 1472 1473 DBG_CALL(Dbg_file_filtee(NAME(ilmp), filtee, 0)); 1474 1475 ghp = 0; 1476 1477 /* 1478 * Determine if the reference link map is already 1479 * loaded. As an optimization compare the filtee with 1480 * our interpretor. The most common filter is 1481 * libdl.so.1, which is a filter on ld.so.1. 1482 */ 1483 #if defined(_ELF64) 1484 if (strcmp(filtee, MSG_ORIG(MSG_PTH_RTLD_64)) == 0) { 1485 #else 1486 if (strcmp(filtee, MSG_ORIG(MSG_PTH_RTLD)) == 0) { 1487 #endif 1488 /* 1489 * Create an association between ld.so.1 and 1490 * the filter. 1491 */ 1492 nlmp = lml_rtld.lm_head; 1493 if ((ghp = hdl_create(&lml_rtld, nlmp, ilmp, 1494 (GPH_LDSO | GPH_FIRST | GPH_FILTEE))) == 0) 1495 nlmp = 0; 1496 1497 /* 1498 * Establish the filter handle to prevent any 1499 * recursion. 1500 */ 1501 if (nlmp && ghp) 1502 pnp->p_info = (void *)ghp; 1503 1504 /* 1505 * Audit the filter/filtee established. Ignore 1506 * any return from the auditor, as we can't 1507 * allow ignore filtering to ld.so.1, otherwise 1508 * nothing is going to work. 1509 */ 1510 if ((lml->lm_tflags | FLAGS1(ilmp)) & 1511 LML_TFLG_AUD_OBJFILTER) 1512 (void) audit_objfilter(ilmp, filtees, 1513 nlmp, 0); 1514 1515 } else { 1516 Rej_desc rej = { 0 }; 1517 1518 /* 1519 * Establish a new link-map control list from 1520 * which to analyze any newly added objects. 1521 * Note that an lmc may already be allocated 1522 * from a previous filtee dlopen() that failed. 1523 */ 1524 if ((lmc == 0) && 1525 (FLAGS(lml->lm_head) & FLG_RT_RELOCED) && 1526 ((lmc = alist_append(&(lml->lm_lists), 1527 0, sizeof (Lm_cntl), AL_CNT_LMLISTS)) == 0)) 1528 return ((Sym *)0); 1529 1530 if (lmc) 1531 lmco = (Aliste)((char *)lmc - 1532 (char *)lml->lm_lists); 1533 else 1534 lmco = ALO_DATA; 1535 1536 /* 1537 * Load the filtee. 1538 */ 1539 if ((nlmp = load_path(lml, lmco, filtee, ilmp, 1540 mode, FLG_RT_HANDLE, &ghp, 0, &rej)) == 0) { 1541 file_notfound(LIST(ilmp), filtee, ilmp, 1542 FLG_RT_HANDLE, &rej); 1543 remove_rej(&rej); 1544 } 1545 1546 /* 1547 * Establish the filter handle to prevent any 1548 * recursion. 1549 */ 1550 if (nlmp && ghp) { 1551 ghp->gh_flags |= GPH_FILTEE; 1552 pnp->p_info = (void *)ghp; 1553 } 1554 1555 /* 1556 * Audit the filter/filtee established. A 1557 * return of 0 indicates the auditor wishes to 1558 * ignore this filtee. 1559 */ 1560 if (nlmp && ((lml->lm_tflags | FLAGS1(ilmp)) & 1561 LML_TFLG_AUD_OBJFILTER)) { 1562 if (audit_objfilter(ilmp, filtees, 1563 nlmp, 0) == 0) { 1564 audit = 1; 1565 nlmp = 0; 1566 } 1567 } 1568 1569 /* 1570 * Finish processing the objects associated with 1571 * this request. Create an association between 1572 * this object and the originating filter to 1573 * provide sufficient information to tear down 1574 * this filtee if necessary. 1575 */ 1576 if (nlmp && ghp && 1577 ((analyze_lmc(lml, lmco, nlmp) == 0) || 1578 (relocate_lmc(lml, lmco, nlmp) == 0))) 1579 nlmp = 0; 1580 1581 /* 1582 * If the filtee has been successfully 1583 * processed, and it is part of a link-map 1584 * control list that is equivalent, or less, 1585 * than the filter control list, create an 1586 * association between the filter and filtee. 1587 * This association provides sufficient 1588 * information to tear down the filter and 1589 * filtee if necessary. 1590 */ 1591 if (nlmp && ghp && (CNTL(nlmp) <= CNTL(ilmp)) && 1592 (hdl_add(ghp, ilmp, GPD_FILTER) == 0)) 1593 nlmp = 0; 1594 } 1595 1596 /* 1597 * Generate a diagnostic if the filtee couldn't be 1598 * loaded, null out the pnode entry, and continue 1599 * the search. Otherwise, retain this group handle 1600 * for future symbol searches. 1601 */ 1602 if (nlmp == 0) { 1603 pnp->p_info = 0; 1604 DBG_CALL(Dbg_file_filtee(0, filtee, audit)); 1605 1606 if (ghp) 1607 (void) dlclose_core(ghp, ilmp); 1608 1609 if (lmc) { 1610 (void) lm_salvage(lml, 0, lmco); 1611 lmc->lc_head = lmc->lc_tail = 0; 1612 } 1613 pnp->p_len = 0; 1614 continue; 1615 } 1616 } 1617 1618 ghp = (Grp_hdl *)pnp->p_info; 1619 1620 /* 1621 * If we're just here to trigger filtee loading skip the symbol 1622 * lookup so we'll continue looking for additional filtees. 1623 */ 1624 if (name) { 1625 Grp_desc *gdp; 1626 Sym *sym = 0; 1627 Aliste off; 1628 Slookup sl = *slp; 1629 1630 sl.sl_flags |= LKUP_FIRST; 1631 any++; 1632 1633 /* 1634 * Look for the symbol in the handles dependencies. 1635 */ 1636 for (ALIST_TRAVERSE(ghp->gh_depends, off, gdp)) { 1637 if ((gdp->gd_flags & GPD_AVAIL) == 0) 1638 continue; 1639 1640 /* 1641 * If our parent is a dependency don't look at 1642 * it (otherwise we are in a recursive loop). 1643 * This situation can occur with auxiliary 1644 * filters if the filtee has a dependency on the 1645 * filter. This dependency isn't necessary as 1646 * auxiliary filters are opened RTLD_PARENT, but 1647 * users may still unknowingly add an explicit 1648 * dependency to the parent. 1649 */ 1650 if ((sl.sl_imap = gdp->gd_depend) == ilmp) 1651 continue; 1652 1653 if (((sym = SYMINTP(sl.sl_imap)(&sl, dlmp, 1654 binfo)) != 0) || 1655 (ghp->gh_flags & GPH_FIRST)) 1656 break; 1657 } 1658 1659 /* 1660 * If this filtee has just been loaded (nlmp != 0), 1661 * determine whether the filtee was triggered by a 1662 * relocation from an object that is still being 1663 * relocated on a leaf link-map control list. As the 1664 * relocation of an object on this list might still 1665 * fail, we can't yet bind the filter to the filtee. 1666 * To do so, would be locking the filtee so that it 1667 * couldn't be deleted, and the filtee itself could have 1668 * bound to an object that must be torn down. Insure 1669 * the caller isn't bound to the handle at this time. 1670 * Any association will be reestablished when the filter 1671 * is later referenced and the filtee has propagated to 1672 * the same link-map control list. 1673 */ 1674 if (nlmp && (CNTL(nlmp) > CNTL(ilmp))) { 1675 remove_caller(ghp, ilmp); 1676 pnp->p_info = 0; 1677 } 1678 if (sym) { 1679 if (lmc) 1680 remove_cntl(lml, lmco); 1681 1682 *binfo |= DBG_BINFO_FILTEE; 1683 return (sym); 1684 } 1685 } 1686 1687 /* 1688 * If this object is tagged to terminate filtee processing we're 1689 * done. 1690 */ 1691 if (FLAGS1(ghp->gh_owner) & FL1_RT_ENDFILTE) 1692 break; 1693 } 1694 1695 if (lmc) 1696 remove_cntl(lml, lmco); 1697 1698 /* 1699 * If we're just here to trigger filtee loading then we're done. 1700 */ 1701 if (name == 0) 1702 return ((Sym *)0); 1703 1704 /* 1705 * If no filtees have been found for a filter, clean up any Pnode 1706 * structures and disable their search completely. For auxiliary 1707 * filters we can reselect the symbol search function so that we never 1708 * enter this routine again for this object. For standard filters we 1709 * use the null symbol routine. 1710 */ 1711 if (any == 0) { 1712 remove_pnode((Pnode *)dip->di_info); 1713 elf_disable_filtee(ilmp, dip); 1714 return ((Sym *)0); 1715 } 1716 1717 return ((Sym *)0); 1718 } 1719 1720 /* 1721 * Focal point for disabling error messages for auxiliary filters. As an 1722 * auxiliary filter allows for filtee use, but provides a fallback should a 1723 * filtee not exist (or fail to load), any errors generated as a consequence of 1724 * trying to load the filtees are typically suppressed. Setting RT_FL_SILENCERR 1725 * suppresses errors generated by eprint(), but insures a debug diagnostic is 1726 * produced. ldd(1) employs printf(), and here, the selection of whether to 1727 * print a diagnostic in regards to auxiliary filters is a little more complex. 1728 * 1729 * . The determination of whether to produce an ldd message, or a fatal 1730 * error message is driven by LML_FLG_TRC_ENABLE. 1731 * . More detailed ldd messages may also be driven off of LML_FLG_TRC_WARN, 1732 * (ldd -d/-r), LML_FLG_TRC_VERBOSE (ldd -v), LML_FLG_TRC_SEARCH (ldd -s), 1733 * and LML_FLG_TRC_UNREF/LML_FLG_TRC_UNUSED (ldd -U/-u). 1734 * 1735 * . If the calling object is lddstub, then several classes of message are 1736 * suppressed. The user isn't trying to diagnose lddstub, this is simply 1737 * a stub executable employed to preload a user specified library against. 1738 * 1739 * . If RT_FL_SILENCERR is in effect then any generic ldd() messages should 1740 * be suppressed. All detailed ldd messages should still be produced. 1741 */ 1742 Sym * 1743 elf_lookup_filtee(Slookup *slp, Rt_map **dlmp, uint_t *binfo, uint_t ndx) 1744 { 1745 Sym *sym; 1746 Dyninfo *dip = &DYNINFO(slp->sl_imap)[ndx]; 1747 int silent = 0; 1748 1749 /* 1750 * Make sure this entry is still acting as a filter. We may have tried 1751 * to process this previously, and disabled it if the filtee couldn't 1752 * be processed. However, other entries may provide different filtees 1753 * that are yet to be completed. 1754 */ 1755 if (dip->di_flags == 0) 1756 return ((Sym *)0); 1757 1758 /* 1759 * Indicate whether an error message is required should this filtee not 1760 * be found, based on the type of filter. 1761 */ 1762 if ((dip->di_flags & FLG_DI_AUXFLTR) && 1763 ((rtld_flags & (RT_FL_WARNFLTR | RT_FL_SILENCERR)) == 0)) { 1764 rtld_flags |= RT_FL_SILENCERR; 1765 silent = 1; 1766 } 1767 1768 sym = _elf_lookup_filtee(slp, dlmp, binfo, ndx); 1769 1770 if (silent) 1771 rtld_flags &= ~RT_FL_SILENCERR; 1772 1773 return (sym); 1774 } 1775 1776 /* 1777 * Compute the elf hash value (as defined in the ELF access library). 1778 * The form of the hash table is: 1779 * 1780 * |--------------| 1781 * | # of buckets | 1782 * |--------------| 1783 * | # of chains | 1784 * |--------------| 1785 * | bucket[] | 1786 * |--------------| 1787 * | chain[] | 1788 * |--------------| 1789 */ 1790 ulong_t 1791 elf_hash(const char *name) 1792 { 1793 uint_t hval = 0; 1794 1795 while (*name) { 1796 uint_t g; 1797 hval = (hval << 4) + *name++; 1798 if ((g = (hval & 0xf0000000)) != 0) 1799 hval ^= g >> 24; 1800 hval &= ~g; 1801 } 1802 return ((ulong_t)hval); 1803 } 1804 1805 /* 1806 * If flag argument has LKUP_SPEC set, we treat undefined symbols of type 1807 * function specially in the executable - if they have a value, even though 1808 * undefined, we use that value. This allows us to associate all references 1809 * to a function's address to a single place in the process: the plt entry 1810 * for that function in the executable. Calls to lookup from plt binding 1811 * routines do NOT set LKUP_SPEC in the flag. 1812 */ 1813 Sym * 1814 elf_find_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo) 1815 { 1816 const char *name = slp->sl_name; 1817 Rt_map *ilmp = slp->sl_imap; 1818 ulong_t hash = slp->sl_hash; 1819 uint_t ndx, htmp, buckets, *chainptr; 1820 Sym *sym, *symtabptr; 1821 char *strtabptr, *strtabname; 1822 uint_t flags1; 1823 Syminfo *sip; 1824 1825 DBG_CALL(Dbg_syms_lookup(name, NAME(ilmp), MSG_ORIG(MSG_STR_ELF))); 1826 1827 if (HASH(ilmp) == 0) 1828 return ((Sym *)0); 1829 1830 buckets = HASH(ilmp)[0]; 1831 /* LINTED */ 1832 htmp = (uint_t)hash % buckets; 1833 1834 /* 1835 * Get the first symbol on hash chain and initialize the string 1836 * and symbol table pointers. 1837 */ 1838 if ((ndx = HASH(ilmp)[htmp + 2]) == 0) 1839 return ((Sym *)0); 1840 1841 chainptr = HASH(ilmp) + 2 + buckets; 1842 strtabptr = STRTAB(ilmp); 1843 symtabptr = SYMTAB(ilmp); 1844 1845 while (ndx) { 1846 sym = symtabptr + ndx; 1847 strtabname = strtabptr + sym->st_name; 1848 1849 /* 1850 * Compare the symbol found with the name required. If the 1851 * names don't match continue with the next hash entry. 1852 */ 1853 if ((*strtabname++ != *name) || strcmp(strtabname, &name[1])) { 1854 if ((ndx = chainptr[ndx]) != 0) 1855 continue; 1856 return ((Sym *)0); 1857 } 1858 1859 /* 1860 * If we find a match and the symbol is defined, return the 1861 * symbol pointer and the link map in which it was found. 1862 */ 1863 if (sym->st_shndx != SHN_UNDEF) { 1864 *dlmp = ilmp; 1865 *binfo |= DBG_BINFO_FOUND; 1866 if (FLAGS(ilmp) & FLG_RT_INTRPOSE) 1867 *binfo |= DBG_BINFO_INTERPOSE; 1868 if (slp->sl_flags & LKUP_SELF) 1869 return (sym); 1870 break; 1871 1872 /* 1873 * If we find a match and the symbol is undefined, the 1874 * symbol type is a function, and the value of the symbol 1875 * is non zero, then this is a special case. This allows 1876 * the resolution of a function address to the plt[] entry. 1877 * See SPARC ABI, Dynamic Linking, Function Addresses for 1878 * more details. 1879 */ 1880 } else if ((slp->sl_flags & (LKUP_SPEC | LKUP_SELF)) && 1881 (FLAGS(ilmp) & FLG_RT_ISMAIN) && (sym->st_value != 0) && 1882 (ELF_ST_TYPE(sym->st_info) == STT_FUNC)) { 1883 *dlmp = ilmp; 1884 *binfo |= (DBG_BINFO_FOUND | DBG_BINFO_PLTADDR); 1885 if (FLAGS(ilmp) & FLG_RT_INTRPOSE) 1886 *binfo |= DBG_BINFO_INTERPOSE; 1887 return (sym); 1888 } 1889 1890 /* 1891 * Undefined symbol. 1892 */ 1893 if (slp->sl_flags & LKUP_SELF) 1894 return (sym); 1895 1896 return ((Sym *)0); 1897 } 1898 1899 /* 1900 * We've found a match. Determine if the defining object contains 1901 * symbol binding information. 1902 */ 1903 if ((sip = SYMINFO(ilmp)) != 0) 1904 /* LINTED */ 1905 sip = (Syminfo *)((char *)sip + (ndx * SYMINENT(ilmp))); 1906 1907 /* 1908 * If this is a direct binding request, but the symbol definition has 1909 * disabled directly binding to it (presumably because the symbol 1910 * definition has been changed since the referring object was built), 1911 * indicate this failure so that the caller can fall back to a standard 1912 * symbol search. Clear any debug binding information for cleanliness. 1913 */ 1914 if (sip && (slp->sl_flags & LKUP_DIRECT) && 1915 (sip->si_flags & SYMINFO_FLG_NOEXTDIRECT)) { 1916 *binfo |= BINFO_DIRECTDIS; 1917 *binfo &= ~DBG_BINFO_MSK; 1918 return ((Sym *)0); 1919 } 1920 1921 /* 1922 * Determine whether this object is acting as a filter. 1923 */ 1924 if (((flags1 = FLAGS1(ilmp)) & MSK_RT_FILTER) == 0) 1925 return (sym); 1926 1927 /* 1928 * Determine if this object offers per-symbol filtering, and if so, 1929 * whether this symbol references a filtee. 1930 */ 1931 if (sip && (flags1 & (FL1_RT_SYMSFLTR | FL1_RT_SYMAFLTR))) { 1932 /* 1933 * If this is a standard filter reference, and no standard 1934 * filtees remain to be inspected, we're done. If this is an 1935 * auxiliary filter reference, and no auxiliary filtees remain, 1936 * we'll fall through in case any object filtering is available. 1937 */ 1938 if ((sip->si_flags & SYMINFO_FLG_FILTER) && 1939 (SYMSFLTRCNT(ilmp) == 0)) 1940 return ((Sym *)0); 1941 1942 if ((sip->si_flags & SYMINFO_FLG_FILTER) || 1943 ((sip->si_flags & SYMINFO_FLG_AUXILIARY) && 1944 SYMAFLTRCNT(ilmp))) { 1945 Sym * fsym; 1946 1947 /* 1948 * This symbol has an associated filtee. Lookup the 1949 * symbol in the filtee, and if it is found return it. 1950 * If the symbol doesn't exist, and this is a standard 1951 * filter, return an error, otherwise fall through to 1952 * catch any object filtering that may be available. 1953 */ 1954 if ((fsym = elf_lookup_filtee(slp, dlmp, binfo, 1955 sip->si_boundto)) != 0) 1956 return (fsym); 1957 if (sip->si_flags & SYMINFO_FLG_FILTER) 1958 return ((Sym *)0); 1959 } 1960 } 1961 1962 /* 1963 * Determine if this object provides global filtering. 1964 */ 1965 if (flags1 & (FL1_RT_OBJSFLTR | FL1_RT_OBJAFLTR)) { 1966 Sym * fsym; 1967 1968 if (OBJFLTRNDX(ilmp) != FLTR_DISABLED) { 1969 /* 1970 * This object has an associated filtee. Lookup the 1971 * symbol in the filtee, and if it is found return it. 1972 * If the symbol doesn't exist, and this is a standard 1973 * filter, return and error, otherwise return the symbol 1974 * within the filter itself. 1975 */ 1976 if ((fsym = elf_lookup_filtee(slp, dlmp, binfo, 1977 OBJFLTRNDX(ilmp))) != 0) 1978 return (fsym); 1979 } 1980 1981 if (flags1 & FL1_RT_OBJSFLTR) 1982 return ((Sym *)0); 1983 } 1984 return (sym); 1985 } 1986 1987 /* 1988 * Create a new Rt_map structure for an ELF object and initialize 1989 * all values. 1990 */ 1991 Rt_map * 1992 elf_new_lm(Lm_list *lml, const char *pname, const char *oname, Dyn *ld, 1993 ulong_t addr, ulong_t etext, Aliste lmco, ulong_t msize, ulong_t entry, 1994 ulong_t paddr, ulong_t padimsize, Mmap *mmaps, uint_t mmapcnt) 1995 { 1996 Rt_map *lmp; 1997 ulong_t base, fltr = 0, audit = 0, cfile = 0, crle = 0; 1998 Xword rpath = 0; 1999 Ehdr *ehdr = (Ehdr *)addr; 2000 2001 DBG_CALL(Dbg_file_elf(pname, (ulong_t)ld, addr, msize, entry, 2002 get_linkmap_id(lml), lmco)); 2003 2004 /* 2005 * Allocate space for the link-map and private elf information. Once 2006 * these are allocated and initialized, we can use remove_so(0, lmp) to 2007 * tear down the link-map should any failures occur. 2008 */ 2009 if ((lmp = calloc(sizeof (Rt_map), 1)) == 0) 2010 return (0); 2011 if ((ELFPRV(lmp) = calloc(sizeof (Rt_elfp), 1)) == 0) { 2012 free(lmp); 2013 return (0); 2014 } 2015 2016 /* 2017 * All fields not filled in were set to 0 by calloc. 2018 */ 2019 ORIGNAME(lmp) = PATHNAME(lmp) = NAME(lmp) = (char *)pname; 2020 DYN(lmp) = ld; 2021 ADDR(lmp) = addr; 2022 MSIZE(lmp) = msize; 2023 ENTRY(lmp) = (Addr)entry; 2024 SYMINTP(lmp) = elf_find_sym; 2025 ETEXT(lmp) = etext; 2026 FCT(lmp) = &elf_fct; 2027 LIST(lmp) = lml; 2028 PADSTART(lmp) = paddr; 2029 PADIMLEN(lmp) = padimsize; 2030 THREADID(lmp) = rt_thr_self(); 2031 OBJFLTRNDX(lmp) = FLTR_DISABLED; 2032 SORTVAL(lmp) = -1; 2033 2034 MMAPS(lmp) = mmaps; 2035 MMAPCNT(lmp) = mmapcnt; 2036 ASSERT(mmapcnt != 0); 2037 2038 /* 2039 * If this is a shared object, add the base address to each address. 2040 * if this is an executable, use address as is. 2041 */ 2042 if (ehdr->e_type == ET_EXEC) { 2043 base = 0; 2044 FLAGS(lmp) |= FLG_RT_FIXED; 2045 } else 2046 base = addr; 2047 2048 /* 2049 * Fill in rest of the link map entries with information from the file's 2050 * dynamic structure. 2051 */ 2052 if (ld) { 2053 uint_t dyncnt = 0; 2054 Xword pltpadsz = 0; 2055 void *rtldinfo; 2056 2057 /* CSTYLED */ 2058 for ( ; ld->d_tag != DT_NULL; ++ld, dyncnt++) { 2059 switch ((Xword)ld->d_tag) { 2060 case DT_SYMTAB: 2061 SYMTAB(lmp) = (void *)(ld->d_un.d_ptr + base); 2062 break; 2063 case DT_STRTAB: 2064 STRTAB(lmp) = (void *)(ld->d_un.d_ptr + base); 2065 break; 2066 case DT_SYMENT: 2067 SYMENT(lmp) = ld->d_un.d_val; 2068 break; 2069 case DT_FEATURE_1: 2070 ld->d_un.d_val |= DTF_1_PARINIT; 2071 if (ld->d_un.d_val & DTF_1_CONFEXP) 2072 crle = 1; 2073 break; 2074 case DT_MOVESZ: 2075 MOVESZ(lmp) = ld->d_un.d_val; 2076 FLAGS(lmp) |= FLG_RT_MOVE; 2077 break; 2078 case DT_MOVEENT: 2079 MOVEENT(lmp) = ld->d_un.d_val; 2080 break; 2081 case DT_MOVETAB: 2082 MOVETAB(lmp) = (void *)(ld->d_un.d_ptr + base); 2083 break; 2084 case DT_REL: 2085 case DT_RELA: 2086 /* 2087 * At this time we can only handle 1 type of 2088 * relocation per object. 2089 */ 2090 REL(lmp) = (void *)(ld->d_un.d_ptr + base); 2091 break; 2092 case DT_RELSZ: 2093 case DT_RELASZ: 2094 RELSZ(lmp) = ld->d_un.d_val; 2095 break; 2096 case DT_RELENT: 2097 case DT_RELAENT: 2098 RELENT(lmp) = ld->d_un.d_val; 2099 break; 2100 case DT_RELCOUNT: 2101 case DT_RELACOUNT: 2102 RELACOUNT(lmp) = (uint_t)ld->d_un.d_val; 2103 break; 2104 case DT_TEXTREL: 2105 FLAGS1(lmp) |= FL1_RT_TEXTREL; 2106 break; 2107 case DT_HASH: 2108 HASH(lmp) = (uint_t *)(ld->d_un.d_ptr + base); 2109 break; 2110 case DT_PLTGOT: 2111 PLTGOT(lmp) = (uint_t *)(ld->d_un.d_ptr + base); 2112 break; 2113 case DT_PLTRELSZ: 2114 PLTRELSZ(lmp) = ld->d_un.d_val; 2115 break; 2116 case DT_JMPREL: 2117 JMPREL(lmp) = (void *)(ld->d_un.d_ptr + base); 2118 break; 2119 case DT_INIT: 2120 INIT(lmp) = (void (*)())(ld->d_un.d_ptr + base); 2121 break; 2122 case DT_FINI: 2123 FINI(lmp) = (void (*)())(ld->d_un.d_ptr + base); 2124 break; 2125 case DT_INIT_ARRAY: 2126 INITARRAY(lmp) = (Addr *)(ld->d_un.d_ptr + 2127 base); 2128 break; 2129 case DT_INIT_ARRAYSZ: 2130 INITARRAYSZ(lmp) = (uint_t)ld->d_un.d_val; 2131 break; 2132 case DT_FINI_ARRAY: 2133 FINIARRAY(lmp) = (Addr *)(ld->d_un.d_ptr + 2134 base); 2135 break; 2136 case DT_FINI_ARRAYSZ: 2137 FINIARRAYSZ(lmp) = (uint_t)ld->d_un.d_val; 2138 break; 2139 case DT_PREINIT_ARRAY: 2140 PREINITARRAY(lmp) = (Addr *)(ld->d_un.d_ptr + 2141 base); 2142 break; 2143 case DT_PREINIT_ARRAYSZ: 2144 PREINITARRAYSZ(lmp) = (uint_t)ld->d_un.d_val; 2145 break; 2146 case DT_RPATH: 2147 case DT_RUNPATH: 2148 rpath = ld->d_un.d_val; 2149 break; 2150 case DT_FILTER: 2151 fltr = ld->d_un.d_val; 2152 OBJFLTRNDX(lmp) = dyncnt; 2153 FLAGS1(lmp) |= FL1_RT_OBJSFLTR; 2154 break; 2155 case DT_AUXILIARY: 2156 if (!(rtld_flags & RT_FL_NOAUXFLTR)) { 2157 fltr = ld->d_un.d_val; 2158 OBJFLTRNDX(lmp) = dyncnt; 2159 } 2160 FLAGS1(lmp) |= FL1_RT_OBJAFLTR; 2161 break; 2162 case DT_SUNW_FILTER: 2163 SYMSFLTRCNT(lmp)++; 2164 FLAGS1(lmp) |= FL1_RT_SYMSFLTR; 2165 break; 2166 case DT_SUNW_AUXILIARY: 2167 if (!(rtld_flags & RT_FL_NOAUXFLTR)) { 2168 SYMAFLTRCNT(lmp)++; 2169 } 2170 FLAGS1(lmp) |= FL1_RT_SYMAFLTR; 2171 break; 2172 case DT_DEPAUDIT: 2173 if (!(rtld_flags & RT_FL_NOAUDIT)) 2174 audit = ld->d_un.d_val; 2175 break; 2176 case DT_CONFIG: 2177 cfile = ld->d_un.d_val; 2178 break; 2179 case DT_DEBUG: 2180 /* 2181 * DT_DEBUG entries are only created in 2182 * dynamic objects that require an interpretor 2183 * (ie. all dynamic executables and some shared 2184 * objects), and provide for a hand-shake with 2185 * debuggers. This entry is initialized to 2186 * zero by the link-editor. If a debugger has 2187 * us and updated this entry set the debugger 2188 * flag, and finish initializing the debugging 2189 * structure (see setup() also). Switch off any 2190 * configuration object use as most debuggers 2191 * can't handle fixed dynamic executables as 2192 * dependencies, and we can't handle requests 2193 * like object padding for alternative objects. 2194 */ 2195 if (ld->d_un.d_ptr) 2196 rtld_flags |= 2197 (RT_FL_DEBUGGER | RT_FL_NOOBJALT); 2198 ld->d_un.d_ptr = (Addr)&r_debug; 2199 break; 2200 case DT_VERNEED: 2201 VERNEED(lmp) = (Verneed *)(ld->d_un.d_ptr + 2202 base); 2203 break; 2204 case DT_VERNEEDNUM: 2205 /* LINTED */ 2206 VERNEEDNUM(lmp) = (int)ld->d_un.d_val; 2207 break; 2208 case DT_VERDEF: 2209 VERDEF(lmp) = (Verdef *)(ld->d_un.d_ptr + base); 2210 break; 2211 case DT_VERDEFNUM: 2212 /* LINTED */ 2213 VERDEFNUM(lmp) = (int)ld->d_un.d_val; 2214 break; 2215 case DT_BIND_NOW: 2216 if ((ld->d_un.d_val & DF_BIND_NOW) && 2217 ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) { 2218 MODE(lmp) |= RTLD_NOW; 2219 MODE(lmp) &= ~RTLD_LAZY; 2220 } 2221 break; 2222 case DT_FLAGS: 2223 if (ld->d_un.d_val & DF_SYMBOLIC) 2224 FLAGS1(lmp) |= FL1_RT_SYMBOLIC; 2225 if (ld->d_un.d_val & DF_TEXTREL) 2226 FLAGS1(lmp) |= FL1_RT_TEXTREL; 2227 if ((ld->d_un.d_val & DF_BIND_NOW) && 2228 ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) { 2229 MODE(lmp) |= RTLD_NOW; 2230 MODE(lmp) &= ~RTLD_LAZY; 2231 } 2232 break; 2233 case DT_FLAGS_1: 2234 if (ld->d_un.d_val & DF_1_DISPRELPND) 2235 FLAGS1(lmp) |= FL1_RT_DISPREL; 2236 if (ld->d_un.d_val & DF_1_GROUP) 2237 FLAGS(lmp) |= 2238 (FLG_RT_SETGROUP | FLG_RT_HANDLE); 2239 if ((ld->d_un.d_val & DF_1_NOW) && 2240 ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) { 2241 MODE(lmp) |= RTLD_NOW; 2242 MODE(lmp) &= ~RTLD_LAZY; 2243 } 2244 if (ld->d_un.d_val & DF_1_NODELETE) 2245 MODE(lmp) |= RTLD_NODELETE; 2246 if (ld->d_un.d_val & DF_1_INITFIRST) 2247 FLAGS(lmp) |= FLG_RT_INITFRST; 2248 if (ld->d_un.d_val & DF_1_NOOPEN) 2249 FLAGS(lmp) |= FLG_RT_NOOPEN; 2250 if (ld->d_un.d_val & DF_1_LOADFLTR) 2251 FLAGS(lmp) |= FLG_RT_LOADFLTR; 2252 if (ld->d_un.d_val & DF_1_NODUMP) 2253 FLAGS(lmp) |= FLG_RT_NODUMP; 2254 if (ld->d_un.d_val & DF_1_CONFALT) 2255 crle = 1; 2256 if (ld->d_un.d_val & DF_1_DIRECT) 2257 FLAGS(lmp) |= FLG_RT_DIRECT; 2258 if (ld->d_un.d_val & DF_1_NODEFLIB) 2259 FLAGS1(lmp) |= FL1_RT_NODEFLIB; 2260 if (ld->d_un.d_val & DF_1_ENDFILTEE) 2261 FLAGS1(lmp) |= FL1_RT_ENDFILTE; 2262 if (ld->d_un.d_val & DF_1_TRANS) 2263 FLAGS(lmp) |= FLG_RT_TRANS; 2264 #ifndef EXPAND_RELATIVE 2265 if (ld->d_un.d_val & DF_1_ORIGIN) 2266 FLAGS1(lmp) |= FL1_RT_RELATIVE; 2267 #endif 2268 /* 2269 * If this object identifies itself as an 2270 * interposer, but relocation processing has 2271 * already started, then demote it. It's too 2272 * late to guarantee complete interposition. 2273 */ 2274 if (ld->d_un.d_val & DF_1_INTERPOSE) { 2275 if ((lml->lm_flags & LML_FLG_STARTREL) == 0) 2276 FLAGS(lmp) |= FLG_RT_INTRPOSE; 2277 else { 2278 DBG_CALL(Dbg_util_intoolate(NAME(lmp))); 2279 if (lml->lm_flags & LML_FLG_TRC_ENABLE) 2280 (void) printf( 2281 MSG_INTL(MSG_LDD_REL_ERR2), 2282 NAME(lmp)); 2283 } 2284 } 2285 break; 2286 case DT_SYMINFO: 2287 SYMINFO(lmp) = (Syminfo *)(ld->d_un.d_ptr + 2288 base); 2289 break; 2290 case DT_SYMINENT: 2291 SYMINENT(lmp) = ld->d_un.d_val; 2292 break; 2293 case DT_PLTPAD: 2294 PLTPAD(lmp) = (void *)(ld->d_un.d_ptr + base); 2295 break; 2296 case DT_PLTPADSZ: 2297 pltpadsz = ld->d_un.d_val; 2298 break; 2299 case DT_SUNW_RTLDINF: 2300 if ((lml->lm_info_lmp != 0) && 2301 (lml->lm_info_lmp != lmp)) { 2302 DBG_CALL(Dbg_unused_rtldinfo( 2303 NAME(lmp), 2304 NAME(lml->lm_info_lmp))); 2305 break; 2306 } 2307 lml->lm_info_lmp = lmp; 2308 rtldinfo = (void *)(ld->d_un.d_ptr + base); 2309 2310 /* 2311 * We maintain a list of DT_SUNW_RTLDINFO 2312 * structures for a given object. This permits 2313 * the RTLDINFO structures to be grouped 2314 * functionly inside of a shared object. 2315 * 2316 * For example, we could have one for 2317 * thread_init, and another for atexit 2318 * reservations. 2319 */ 2320 if (alist_append(&lml->lm_rtldinfo, &rtldinfo, 2321 sizeof (void *), AL_CNT_RTLDINFO) == 0) { 2322 remove_so(0, lmp); 2323 return (0); 2324 } 2325 break; 2326 case DT_DEPRECATED_SPARC_REGISTER: 2327 case M_DT_REGISTER: 2328 FLAGS(lmp) |= FLG_RT_REGSYMS; 2329 break; 2330 case M_DT_PLTRESERVE: 2331 PLTRESERVE(lmp) = (void *)(ld->d_un.d_ptr + 2332 base); 2333 break; 2334 } 2335 } 2336 2337 2338 if (PLTPAD(lmp)) { 2339 if (pltpadsz == (Xword)0) 2340 PLTPAD(lmp) = 0; 2341 else 2342 PLTPADEND(lmp) = (void *)((Addr)PLTPAD(lmp) + 2343 pltpadsz); 2344 } 2345 2346 /* 2347 * Allocate Dynamic Info structure 2348 */ 2349 if ((DYNINFO(lmp) = calloc((size_t)dyncnt, 2350 sizeof (Dyninfo))) == 0) { 2351 remove_so(0, lmp); 2352 return (0); 2353 } 2354 DYNINFOCNT(lmp) = dyncnt; 2355 } 2356 2357 /* 2358 * If configuration file use hasn't been disabled, and a configuration 2359 * file hasn't already been set via an environment variable, see if any 2360 * application specific configuration file is specified. An LD_CONFIG 2361 * setting is used first, but if this image was generated via crle(1) 2362 * then a default configuration file is a fall-back. 2363 */ 2364 if ((!(rtld_flags & RT_FL_NOCFG)) && (config->c_name == 0)) { 2365 if (cfile) 2366 config->c_name = (const char *)(cfile + 2367 (char *)STRTAB(lmp)); 2368 else if (crle) { 2369 rtld_flags |= RT_FL_CONFAPP; 2370 #ifndef EXPAND_RELATIVE 2371 FLAGS1(lmp) |= FL1_RT_RELATIVE; 2372 #endif 2373 } 2374 } 2375 2376 if (rpath) 2377 RPATH(lmp) = (char *)(rpath + (char *)STRTAB(lmp)); 2378 if (fltr) { 2379 /* 2380 * If this object is a global filter, duplicate the filtee 2381 * string name(s) so that REFNAME() is available in core files. 2382 * This cludge was useful for debuggers at one point, but only 2383 * when the filtee name was an individual full path. 2384 */ 2385 if ((REFNAME(lmp) = strdup(fltr + (char *)STRTAB(lmp))) == 0) { 2386 remove_so(0, lmp); 2387 return (0); 2388 } 2389 } 2390 2391 if (rtld_flags & RT_FL_RELATIVE) 2392 FLAGS1(lmp) |= FL1_RT_RELATIVE; 2393 2394 /* 2395 * For Intel ABI compatibility. It's possible that a JMPREL can be 2396 * specified without any other relocations (e.g. a dynamic executable 2397 * normally only contains .plt relocations). If this is the case then 2398 * no REL, RELSZ or RELENT will have been created. For us to be able 2399 * to traverse the .plt relocations under LD_BIND_NOW we need to know 2400 * the RELENT for these relocations. Refer to elf_reloc() for more 2401 * details. 2402 */ 2403 if (!RELENT(lmp) && JMPREL(lmp)) 2404 RELENT(lmp) = sizeof (Rel); 2405 2406 /* 2407 * Establish any per-object auditing. If we're establishing `main's 2408 * link-map its too early to go searching for audit objects so just 2409 * hold the object name for later (see setup()). 2410 */ 2411 if (audit) { 2412 char *cp = audit + (char *)STRTAB(lmp); 2413 2414 if (*cp) { 2415 if (((AUDITORS(lmp) = 2416 calloc(1, sizeof (Audit_desc))) == 0) || 2417 ((AUDITORS(lmp)->ad_name = strdup(cp)) == 0)) { 2418 remove_so(0, lmp); 2419 return (0); 2420 } 2421 if (lml_main.lm_head) { 2422 if (audit_setup(lmp, AUDITORS(lmp), 0) == 0) { 2423 remove_so(0, lmp); 2424 return (0); 2425 } 2426 FLAGS1(lmp) |= AUDITORS(lmp)->ad_flags; 2427 lml->lm_flags |= LML_FLG_LOCAUDIT; 2428 } 2429 } 2430 } 2431 2432 if ((CONDVAR(lmp) = rt_cond_create()) == 0) { 2433 remove_so(0, lmp); 2434 return (0); 2435 } 2436 if (oname && ((append_alias(lmp, oname, 0)) == 0)) { 2437 remove_so(0, lmp); 2438 return (0); 2439 } 2440 2441 /* 2442 * Add the mapped object to the end of the link map list. 2443 */ 2444 lm_append(lml, lmco, lmp); 2445 return (lmp); 2446 } 2447 2448 /* 2449 * Assign hardware/software capabilities. 2450 */ 2451 void 2452 cap_assign(Cap *cap, Rt_map *lmp) 2453 { 2454 while (cap->c_tag != CA_SUNW_NULL) { 2455 switch (cap->c_tag) { 2456 case CA_SUNW_HW_1: 2457 HWCAP(lmp) = cap->c_un.c_val; 2458 break; 2459 case CA_SUNW_SF_1: 2460 SFCAP(lmp) = cap->c_un.c_val; 2461 } 2462 cap++; 2463 } 2464 } 2465 2466 /* 2467 * Map in an ELF object. 2468 * Takes an open file descriptor for the object to map and its pathname; returns 2469 * a pointer to a Rt_map structure for this object, or 0 on error. 2470 */ 2471 static Rt_map * 2472 elf_map_so(Lm_list *lml, Aliste lmco, const char *pname, const char *oname, 2473 int fd) 2474 { 2475 int i; /* general temporary */ 2476 Off memsize = 0; /* total memory size of pathname */ 2477 Off mentry; /* entry point */ 2478 Ehdr *ehdr; /* ELF header of ld.so */ 2479 Phdr *phdr; /* first Phdr in file */ 2480 Phdr *phdr0; /* Saved first Phdr in file */ 2481 Phdr *pptr; /* working Phdr */ 2482 Phdr *fph = 0; /* first loadable Phdr */ 2483 Phdr *lph; /* last loadable Phdr */ 2484 Phdr *lfph = 0; /* last loadable (filesz != 0) Phdr */ 2485 Phdr *lmph = 0; /* last loadable (memsz != 0) Phdr */ 2486 Phdr *swph = 0; /* program header for SUNWBSS */ 2487 Phdr *tlph = 0; /* program header for PT_TLS */ 2488 Phdr *unwindph = 0; /* program header for PT_SUNW_UNWIND */ 2489 Cap *cap = 0; /* program header for SUNWCAP */ 2490 Dyn *mld = 0; /* DYNAMIC structure for pathname */ 2491 size_t size; /* size of elf and program headers */ 2492 caddr_t faddr = 0; /* mapping address of pathname */ 2493 Rt_map *lmp; /* link map created */ 2494 caddr_t paddr; /* start of padded image */ 2495 Off plen; /* size of image including padding */ 2496 Half etype; 2497 int fixed; 2498 Mmap *mmaps; 2499 uint_t mmapcnt = 0; 2500 Xword align = 0; 2501 2502 /* LINTED */ 2503 ehdr = (Ehdr *)fmap->fm_maddr; 2504 2505 /* 2506 * If this a relocatable object then special processing is required. 2507 */ 2508 if ((etype = ehdr->e_type) == ET_REL) 2509 return (elf_obj_file(lml, lmco, pname, fd)); 2510 2511 /* 2512 * If this isn't a dynamic executable or shared object we can't process 2513 * it. If this is a dynamic executable then all addresses are fixed. 2514 */ 2515 if (etype == ET_EXEC) 2516 fixed = 1; 2517 else if (etype == ET_DYN) 2518 fixed = 0; 2519 else { 2520 eprintf(ERR_ELF, MSG_INTL(MSG_GEN_BADTYPE), pname, 2521 conv_etype_str(etype)); 2522 return (0); 2523 } 2524 2525 /* 2526 * If our original mapped page was not large enough to hold all the 2527 * program headers remap them. 2528 */ 2529 size = (size_t)((char *)ehdr->e_phoff + 2530 (ehdr->e_phnum * ehdr->e_phentsize)); 2531 if (size > fmap->fm_fsize) { 2532 eprintf(ERR_FATAL, MSG_INTL(MSG_GEN_CORTRUNC), pname); 2533 return (0); 2534 } 2535 if (size > fmap->fm_msize) { 2536 fmap_setup(); 2537 if ((fmap->fm_maddr = mmap(fmap->fm_maddr, size, PROT_READ, 2538 fmap->fm_mflags, fd, 0)) == MAP_FAILED) { 2539 int err = errno; 2540 eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_MMAP), pname, 2541 strerror(err)); 2542 return (0); 2543 } 2544 fmap->fm_msize = size; 2545 /* LINTED */ 2546 ehdr = (Ehdr *)fmap->fm_maddr; 2547 } 2548 /* LINTED */ 2549 phdr0 = phdr = (Phdr *)((char *)ehdr + ehdr->e_ehsize); 2550 2551 /* 2552 * Get entry point. 2553 */ 2554 mentry = ehdr->e_entry; 2555 2556 /* 2557 * Point at program headers and perform some basic validation. 2558 */ 2559 for (i = 0, pptr = phdr; i < (int)ehdr->e_phnum; i++, 2560 pptr = (Phdr *)((Off)pptr + ehdr->e_phentsize)) { 2561 if ((pptr->p_type == PT_LOAD) || 2562 (pptr->p_type == PT_SUNWBSS)) { 2563 2564 if (fph == 0) { 2565 fph = pptr; 2566 /* LINTED argument lph is initialized in first pass */ 2567 } else if (pptr->p_vaddr <= lph->p_vaddr) { 2568 eprintf(ERR_ELF, MSG_INTL(MSG_GEN_INVPRGHDR), 2569 pname); 2570 return (0); 2571 } 2572 2573 lph = pptr; 2574 2575 if (pptr->p_memsz) 2576 lmph = pptr; 2577 if (pptr->p_filesz) 2578 lfph = pptr; 2579 if (pptr->p_type == PT_SUNWBSS) 2580 swph = pptr; 2581 if (pptr->p_align > align) 2582 align = pptr->p_align; 2583 2584 } else if (pptr->p_type == PT_DYNAMIC) 2585 mld = (Dyn *)(pptr->p_vaddr); 2586 else if (pptr->p_type == PT_TLS) 2587 tlph = pptr; 2588 else if (pptr->p_type == PT_SUNWCAP) 2589 cap = (Cap *)(pptr->p_vaddr); 2590 else if (pptr->p_type == PT_SUNW_UNWIND) 2591 unwindph = pptr; 2592 } 2593 2594 #if defined(MAP_ALIGN) 2595 /* 2596 * Make sure the maximum page alignment is a power of 2 >= the system 2597 * page size, for use with MAP_ALIGN. 2598 */ 2599 align = M_PROUND(align); 2600 #endif 2601 2602 /* 2603 * We'd better have at least one loadable segment, together with some 2604 * specified file and memory size. 2605 */ 2606 if ((fph == 0) || (lmph == 0) || (lfph == 0)) { 2607 eprintf(ERR_ELF, MSG_INTL(MSG_GEN_NOLOADSEG), pname); 2608 return (0); 2609 } 2610 2611 /* 2612 * Check that the files size accounts for the loadable sections 2613 * we're going to map in (failure to do this may cause spurious 2614 * bus errors if we're given a truncated file). 2615 */ 2616 if (fmap->fm_fsize < ((size_t)lfph->p_offset + lfph->p_filesz)) { 2617 eprintf(ERR_FATAL, MSG_INTL(MSG_GEN_CORTRUNC), pname); 2618 return (0); 2619 } 2620 2621 /* 2622 * Memsize must be page rounded so that if we add object padding 2623 * at the end it will start at the beginning of a page. 2624 */ 2625 plen = memsize = M_PROUND((lmph->p_vaddr + lmph->p_memsz) - 2626 M_PTRUNC((ulong_t)fph->p_vaddr)); 2627 2628 /* 2629 * Determine if an existing mapping is acceptable. 2630 */ 2631 if (interp && (lml->lm_flags & LML_FLG_BASELM) && 2632 (strcmp(pname, interp->i_name) == 0)) { 2633 /* 2634 * If this is the interpreter then it has already been mapped 2635 * and we have the address so don't map it again. Note that 2636 * the common occurrence of a reference to the interpretor 2637 * (libdl -> ld.so.1) will have been caught during filter 2638 * initialization (see elf_lookup_filtee()). However, some 2639 * ELF implementations are known to record libc.so.1 as the 2640 * interpretor, and thus this test catches this behavior. 2641 */ 2642 paddr = faddr = interp->i_faddr; 2643 2644 } else if ((fixed == 0) && (r_debug.rtd_objpad == 0) && 2645 (memsize <= fmap->fm_msize) && ((fph->p_flags & PF_W) == 0) && 2646 (fph->p_filesz == fph->p_memsz) && 2647 (((Xword)fmap->fm_maddr % align) == 0)) { 2648 /* 2649 * If the mapping required has already been established from 2650 * the initial page we don't need to do anything more. Reset 2651 * the fmap address so then any later files start a new fmap. 2652 * This is really an optimization for filters, such as libdl.so, 2653 * which should only require one page. 2654 */ 2655 paddr = faddr = fmap->fm_maddr; 2656 fmap->fm_maddr = 0; 2657 fmap_setup(); 2658 } 2659 2660 /* 2661 * Allocate a mapping array to retain mapped segment information. 2662 */ 2663 if ((mmaps = calloc(ehdr->e_phnum, sizeof (Mmap))) == 0) 2664 return (0); 2665 2666 /* 2667 * If we're reusing an existing mapping determine the objects etext 2668 * address. Otherwise map the file (which will calculate the etext 2669 * address as part of the mapping process). 2670 */ 2671 if (faddr) { 2672 caddr_t base; 2673 2674 if (fixed) 2675 base = 0; 2676 else 2677 base = faddr; 2678 2679 /* LINTED */ 2680 phdr0 = phdr = (Phdr *)((char *)faddr + ehdr->e_ehsize); 2681 2682 for (i = 0, pptr = phdr; i < (int)ehdr->e_phnum; i++, 2683 pptr = (Phdr *)((Off)pptr + ehdr->e_phentsize)) { 2684 if (pptr->p_type != PT_LOAD) 2685 continue; 2686 2687 mmaps[mmapcnt].m_vaddr = (pptr->p_vaddr + base); 2688 mmaps[mmapcnt].m_msize = pptr->p_memsz; 2689 mmaps[mmapcnt].m_fsize = pptr->p_filesz; 2690 mmaps[mmapcnt].m_perm = (PROT_READ | PROT_EXEC); 2691 mmapcnt++; 2692 2693 if (!(pptr->p_flags & PF_W)) { 2694 fmap->fm_etext = (ulong_t)pptr->p_vaddr + 2695 (ulong_t)pptr->p_memsz + 2696 (ulong_t)(fixed ? 0 : faddr); 2697 } 2698 } 2699 } else { 2700 /* 2701 * Map the file. 2702 */ 2703 if (!(faddr = elf_map_it(pname, memsize, ehdr, fph, lph, 2704 &phdr, &paddr, &plen, fixed, fd, align, mmaps, &mmapcnt))) 2705 return (0); 2706 } 2707 2708 /* 2709 * Calculate absolute base addresses and entry points. 2710 */ 2711 if (!fixed) { 2712 if (mld) 2713 /* LINTED */ 2714 mld = (Dyn *)((Off)mld + faddr); 2715 if (cap) 2716 /* LINTED */ 2717 cap = (Cap *)((Off)cap + faddr); 2718 mentry += (Off)faddr; 2719 } 2720 2721 /* 2722 * Create new link map structure for newly mapped shared object. 2723 */ 2724 if (!(lmp = elf_new_lm(lml, pname, oname, mld, (ulong_t)faddr, 2725 fmap->fm_etext, lmco, memsize, mentry, (ulong_t)paddr, plen, mmaps, 2726 mmapcnt))) { 2727 (void) munmap((caddr_t)faddr, memsize); 2728 return (0); 2729 } 2730 2731 /* 2732 * Start the system loading in the ELF information we'll be processing. 2733 */ 2734 if (REL(lmp)) { 2735 (void) madvise((void *)ADDR(lmp), (uintptr_t)REL(lmp) + 2736 (uintptr_t)RELSZ(lmp) - (uintptr_t)ADDR(lmp), 2737 MADV_WILLNEED); 2738 } 2739 2740 /* 2741 * If this shared object contains a any special segments, record them. 2742 */ 2743 if (swph) { 2744 FLAGS(lmp) |= FLG_RT_SUNWBSS; 2745 SUNWBSS(lmp) = phdr + (swph - phdr0); 2746 } 2747 if (tlph) { 2748 PTTLS(lmp) = phdr + (tlph - phdr0); 2749 tls_assign_soffset(lmp); 2750 } 2751 2752 if (unwindph) 2753 PTUNWIND(lmp) = phdr + (unwindph - phdr0); 2754 2755 if (cap) 2756 cap_assign(cap, lmp); 2757 2758 return (lmp); 2759 } 2760 2761 2762 /* 2763 * Function to correct protection settings. Segments are all mapped initially 2764 * with permissions as given in the segment header. We need to turn on write 2765 * permissions on a text segment if there are any relocations against that 2766 * segment, and them turn write permission back off again before returning 2767 * control to the user. This function turns the permission on or off depending 2768 * on the value of the argument. 2769 */ 2770 int 2771 elf_set_prot(Rt_map * lmp, int permission) 2772 { 2773 Mmap *mmaps; 2774 2775 /* 2776 * If this is an allocated image (ie. a relocatable object) we can't 2777 * mprotect() anything. 2778 */ 2779 if (FLAGS(lmp) & FLG_RT_IMGALLOC) 2780 return (1); 2781 2782 DBG_CALL(Dbg_file_prot(NAME(lmp), permission)); 2783 2784 for (mmaps = MMAPS(lmp); mmaps->m_vaddr; mmaps++) { 2785 if (mmaps->m_perm & PROT_WRITE) 2786 continue; 2787 2788 if (mprotect(mmaps->m_vaddr, mmaps->m_msize, 2789 (mmaps->m_perm | permission)) == -1) { 2790 int err = errno; 2791 eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_MPROT), 2792 NAME(lmp), strerror(err)); 2793 return (0); 2794 } 2795 } 2796 return (1); 2797 } 2798 2799 /* 2800 * Build full pathname of shared object from given directory name and filename. 2801 */ 2802 static char * 2803 elf_get_so(const char *dir, const char *file) 2804 { 2805 static char pname[PATH_MAX]; 2806 2807 (void) snprintf(pname, PATH_MAX, MSG_ORIG(MSG_FMT_PATH), dir, file); 2808 return (pname); 2809 } 2810 2811 /* 2812 * The copy relocation is recorded in a copy structure which will be applied 2813 * after all other relocations are carried out. This provides for copying data 2814 * that must be relocated itself (ie. pointers in shared objects). This 2815 * structure also provides a means of binding RTLD_GROUP dependencies to any 2816 * copy relocations that have been taken from any group members. 2817 * 2818 * If the size of the .bss area available for the copy information is not the 2819 * same as the source of the data inform the user if we're under ldd(1) control 2820 * (this checking was only established in 5.3, so by only issuing an error via 2821 * ldd(1) we maintain the standard set by previous releases). 2822 */ 2823 int 2824 elf_copy_reloc(char *name, Sym *rsym, Rt_map *rlmp, void *radd, Sym *dsym, 2825 Rt_map *dlmp, const void *dadd) 2826 { 2827 Rel_copy rc; 2828 Lm_list *lml = LIST(rlmp); 2829 2830 rc.r_name = name; 2831 rc.r_rsym = rsym; /* the new reference symbol and its */ 2832 rc.r_rlmp = rlmp; /* associated link-map */ 2833 rc.r_dlmp = dlmp; /* the defining link-map */ 2834 rc.r_dsym = dsym; /* the original definition */ 2835 rc.r_radd = radd; 2836 rc.r_dadd = dadd; 2837 2838 if (rsym->st_size > dsym->st_size) 2839 rc.r_size = (size_t)dsym->st_size; 2840 else 2841 rc.r_size = (size_t)rsym->st_size; 2842 2843 if (alist_append(©(dlmp), &rc, sizeof (Rel_copy), 2844 AL_CNT_COPYREL) == 0) { 2845 if (!(lml->lm_flags & LML_FLG_TRC_WARN)) 2846 return (0); 2847 else 2848 return (1); 2849 } 2850 if (!(FLAGS1(dlmp) & FL1_RT_COPYTOOK)) { 2851 if (alist_append(©(rlmp), &dlmp, 2852 sizeof (Rt_map *), AL_CNT_COPYREL) == 0) { 2853 if (!(lml->lm_flags & LML_FLG_TRC_WARN)) 2854 return (0); 2855 else 2856 return (1); 2857 } 2858 FLAGS1(dlmp) |= FL1_RT_COPYTOOK; 2859 } 2860 2861 /* 2862 * If we are tracing (ldd), warn the user if 2863 * 1) the size from the reference symbol differs from the 2864 * copy definition. We can only copy as much data as the 2865 * reference (dynamic executables) entry allows. 2866 * 2) the copy definition has STV_PROTECTED visibility. 2867 */ 2868 if (lml->lm_flags & LML_FLG_TRC_WARN) { 2869 if (rsym->st_size != dsym->st_size) { 2870 (void) printf(MSG_INTL(MSG_LDD_CPY_SIZDIF), 2871 _conv_reloc_type_str(M_R_COPY), demangle(name), 2872 NAME(rlmp), EC_XWORD(rsym->st_size), 2873 NAME(dlmp), EC_XWORD(dsym->st_size)); 2874 if (rsym->st_size > dsym->st_size) 2875 (void) printf(MSG_INTL(MSG_LDD_CPY_INSDATA), 2876 NAME(dlmp)); 2877 else 2878 (void) printf(MSG_INTL(MSG_LDD_CPY_DATRUNC), 2879 NAME(rlmp)); 2880 } 2881 2882 if (ELF_ST_VISIBILITY(dsym->st_other) == STV_PROTECTED) { 2883 (void) printf(MSG_INTL(MSG_LDD_CPY_PROT), 2884 _conv_reloc_type_str(M_R_COPY), demangle(name), 2885 NAME(dlmp)); 2886 } 2887 } 2888 2889 DBG_CALL(Dbg_reloc_apply((Xword)radd, (Xword)rc.r_size)); 2890 return (1); 2891 } 2892 2893 /* 2894 * Determine the symbol location of an address within a link-map. Look for 2895 * the nearest symbol (whose value is less than or equal to the required 2896 * address). This is the object specific part of dladdr(). 2897 */ 2898 static void 2899 elf_dladdr(ulong_t addr, Rt_map *lmp, Dl_info *dlip, void **info, int flags) 2900 { 2901 ulong_t ndx, cnt, base, _value; 2902 Sym *sym, *_sym; 2903 const char *str; 2904 2905 /* 2906 * If we don't have a .hash table there are no symbols to look at. 2907 */ 2908 if (HASH(lmp) == 0) 2909 return; 2910 2911 cnt = HASH(lmp)[1]; 2912 str = STRTAB(lmp); 2913 sym = SYMTAB(lmp); 2914 2915 if (FLAGS(lmp) & FLG_RT_FIXED) 2916 base = 0; 2917 else 2918 base = ADDR(lmp); 2919 2920 for (_sym = 0, _value = 0, sym++, ndx = 1; ndx < cnt; ndx++, sym++) { 2921 ulong_t value; 2922 2923 if (sym->st_shndx == SHN_UNDEF) 2924 continue; 2925 2926 value = sym->st_value + base; 2927 if (value > addr) 2928 continue; 2929 if (value < _value) 2930 continue; 2931 2932 _sym = sym; 2933 _value = value; 2934 2935 /* 2936 * Note, because we accept local and global symbols we could 2937 * find a section symbol that matches the associated address, 2938 * which means that the symbol name will be null. In this 2939 * case continue the search in case we can find a global 2940 * symbol of the same value. 2941 */ 2942 if ((value == addr) && 2943 (ELF_ST_TYPE(sym->st_info) != STT_SECTION)) 2944 break; 2945 } 2946 2947 if (_sym) { 2948 int _flags = flags & RTLD_DL_MASK; 2949 2950 if (_flags == RTLD_DL_SYMENT) 2951 *info = (void *)_sym; 2952 else if (_flags == RTLD_DL_LINKMAP) 2953 *info = (void *)lmp; 2954 2955 dlip->dli_sname = str + _sym->st_name; 2956 dlip->dli_saddr = (void *)_value; 2957 } 2958 } 2959 2960 static void 2961 elf_lazy_cleanup(Alist * alp) 2962 { 2963 Rt_map ** lmpp; 2964 Aliste off; 2965 2966 /* 2967 * Cleanup any link-maps added to this dynamic list and free it. 2968 */ 2969 for (ALIST_TRAVERSE(alp, off, lmpp)) 2970 FLAGS(*lmpp) &= ~FLG_RT_DLSYM; 2971 free(alp); 2972 } 2973 2974 /* 2975 * This routine is called upon to search for a symbol from the dependencies of 2976 * the initial link-map. To maintain lazy loadings goal of reducing the number 2977 * of objects mapped, any symbol search is first carried out using the objects 2978 * that already exist in the process (either on a link-map list or handle). 2979 * If a symbol can't be found, and lazy dependencies are still pending, this 2980 * routine loads the dependencies in an attempt to locate the symbol. 2981 * 2982 * Only new objects are inspected as we will have already inspected presently 2983 * loaded objects before calling this routine. However, a new object may not 2984 * be new - although the di_lmp might be zero, the object may have been mapped 2985 * as someone elses dependency. Thus there's a possibility of some symbol 2986 * search duplication. 2987 */ 2988 2989 Sym * 2990 elf_lazy_find_sym(Slookup *slp, Rt_map **_lmp, uint_t *binfo) 2991 { 2992 Sym *sym = 0; 2993 Alist * alist = 0; 2994 Aliste off; 2995 Rt_map ** lmpp, * lmp = slp->sl_imap; 2996 const char *name = slp->sl_name; 2997 2998 if (alist_append(&alist, &lmp, sizeof (Rt_map *), AL_CNT_LAZYFIND) == 0) 2999 return (0); 3000 FLAGS(lmp) |= FLG_RT_DLSYM; 3001 3002 for (ALIST_TRAVERSE(alist, off, lmpp)) { 3003 uint_t cnt = 0; 3004 Slookup sl = *slp; 3005 Dyninfo *dip; 3006 3007 /* 3008 * Loop through the DT_NEEDED entries examining each object for 3009 * the symbol. If the symbol is not found the object is in turn 3010 * added to the alist, so that its DT_NEEDED entires may be 3011 * examined. 3012 */ 3013 lmp = *lmpp; 3014 for (dip = DYNINFO(lmp); cnt < DYNINFOCNT(lmp); cnt++, dip++) { 3015 Rt_map *nlmp; 3016 3017 if (((dip->di_flags & FLG_DI_NEEDED) == 0) || 3018 dip->di_info) 3019 continue; 3020 3021 /* 3022 * If this entry defines a lazy dependency try loading 3023 * it. If the file can't be loaded, consider this 3024 * non-fatal and continue the search (lazy loaded 3025 * dependencies need not exist and their loading should 3026 * only be fatal if called from a relocation). 3027 * 3028 * If the file is already loaded and relocated we must 3029 * still inspect it for symbols, even though it might 3030 * have already been searched. This lazy load operation 3031 * might have promoted the permissions of the object, 3032 * and thus made the object applicable for this symbol 3033 * search, whereas before the object might have been 3034 * skipped. 3035 */ 3036 if ((nlmp = elf_lazy_load(lmp, cnt, name)) == 0) 3037 continue; 3038 3039 /* 3040 * If this object isn't yet a part of the dynamic list 3041 * then inspect it for the symbol. If the symbol isn't 3042 * found add the object to the dynamic list so that we 3043 * can inspect its dependencies. 3044 */ 3045 if (FLAGS(nlmp) & FLG_RT_DLSYM) 3046 continue; 3047 3048 sl.sl_imap = nlmp; 3049 if (sym = LM_LOOKUP_SYM(sl.sl_cmap)(&sl, _lmp, binfo)) 3050 break; 3051 3052 /* 3053 * Some dlsym() operations are already traversing a 3054 * link-map (dlopen(0)), and thus there's no need to 3055 * build our own dynamic dependency list. 3056 */ 3057 if ((sl.sl_flags & LKUP_NODESCENT) == 0) { 3058 if (alist_append(&alist, &nlmp, 3059 sizeof (Rt_map *), AL_CNT_LAZYFIND) == 0) { 3060 elf_lazy_cleanup(alist); 3061 return (0); 3062 } 3063 FLAGS(nlmp) |= FLG_RT_DLSYM; 3064 } 3065 } 3066 if (sym) 3067 break; 3068 } 3069 3070 elf_lazy_cleanup(alist); 3071 return (sym); 3072 } 3073 3074 /* 3075 * Warning message for bad r_offset. 3076 */ 3077 void 3078 elf_reloc_bad(Rt_map *lmp, void *rel, uchar_t rtype, ulong_t roffset, 3079 ulong_t rsymndx) 3080 { 3081 const char *name = (char *)0; 3082 int trace; 3083 3084 if ((LIST(lmp)->lm_flags & LML_FLG_TRC_ENABLE) && 3085 (((rtld_flags & RT_FL_SILENCERR) == 0) || 3086 (LIST(lmp)->lm_flags & LML_FLG_TRC_VERBOSE))) 3087 trace = 1; 3088 else 3089 trace = 0; 3090 3091 if ((trace == 0) && (dbg_mask == 0)) 3092 return; 3093 3094 if (rsymndx) { 3095 Sym *symref = (Sym *)((ulong_t)SYMTAB(lmp) + 3096 (rsymndx * SYMENT(lmp))); 3097 3098 if (ELF_ST_BIND(symref->st_info) != STB_LOCAL) 3099 name = (char *)(STRTAB(lmp) + symref->st_name); 3100 } 3101 3102 if (name == 0) 3103 name = MSG_ORIG(MSG_STR_EMPTY); 3104 3105 if (trace) { 3106 const char *rstr; 3107 3108 rstr = _conv_reloc_type_str((uint_t)rtype); 3109 (void) printf(MSG_INTL(MSG_LDD_REL_ERR1), rstr, name, 3110 EC_ADDR(roffset)); 3111 return; 3112 } 3113 3114 Dbg_reloc_error(M_MACH, M_REL_SHT_TYPE, rel, name, 3115 MSG_ORIG(MSG_REL_BADROFFSET)); 3116 } 3117