1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 1988 AT&T 24 * All Rights Reserved 25 * 26 * 27 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. 28 */ 29 30 /* 31 * Symbol table management routines 32 */ 33 34 #define ELF_TARGET_AMD64 35 36 /* We deliberately choose a locale unaware ctype */ 37 #include <sys/ctype.h> 38 39 #include <stdio.h> 40 #include <string.h> 41 #include <debug.h> 42 #include "msg.h" 43 #include "_libld.h" 44 45 /* 46 * AVL tree comparator function: 47 * 48 * The primary key is the symbol name hash with a secondary key of the symbol 49 * name itself. 50 */ 51 int 52 ld_sym_avl_comp(const void *elem1, const void *elem2) 53 { 54 Sym_avlnode *sav1 = (Sym_avlnode *)elem1; 55 Sym_avlnode *sav2 = (Sym_avlnode *)elem2; 56 int res; 57 58 res = sav1->sav_hash - sav2->sav_hash; 59 60 if (res < 0) 61 return (-1); 62 if (res > 0) 63 return (1); 64 65 /* 66 * Hash is equal - now compare name 67 */ 68 res = strcmp(sav1->sav_name, sav2->sav_name); 69 if (res == 0) 70 return (0); 71 if (res > 0) 72 return (1); 73 return (-1); 74 } 75 76 /* 77 * Focal point for verifying symbol names. 78 */ 79 inline static const char * 80 string(Ofl_desc *ofl, Ifl_desc *ifl, Sym *sym, const char *strs, size_t strsize, 81 int symndx, Word shndx, Word symsecndx, const char *symsecname, 82 const char *strsecname, sd_flag_t *flags) 83 { 84 Word name = sym->st_name; 85 86 if (name) { 87 if ((ifl->ifl_flags & FLG_IF_HSTRTAB) == 0) { 88 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_NOSTRTABLE), 89 ifl->ifl_name, EC_WORD(symsecndx), symsecname, 90 symndx, EC_XWORD(name)); 91 return (NULL); 92 } 93 if (name >= (Word)strsize) { 94 ld_eprintf(ofl, ERR_FATAL, 95 MSG_INTL(MSG_FIL_EXCSTRTABLE), ifl->ifl_name, 96 EC_WORD(symsecndx), symsecname, symndx, 97 EC_XWORD(name), strsecname, EC_XWORD(strsize)); 98 return (NULL); 99 } 100 } 101 102 /* 103 * Determine if we're dealing with a register and if so validate it. 104 * If it's a scratch register, a fabricated name will be returned. 105 */ 106 if (ld_targ.t_ms.ms_is_regsym != NULL) { 107 const char *regname = (*ld_targ.t_ms.ms_is_regsym)(ofl, ifl, 108 sym, strs, symndx, shndx, symsecname, flags); 109 110 if (regname == (const char *)S_ERROR) { 111 return (NULL); 112 } 113 if (regname) 114 return (regname); 115 } 116 117 /* 118 * If this isn't a register, but we have a global symbol with a null 119 * name, we're not going to be able to hash this, search for it, or 120 * do anything interesting. However, we've been accepting a symbol of 121 * this kind for ages now, so give the user a warning (rather than a 122 * fatal error), just in case this instance exists somewhere in the 123 * world and hasn't, as yet, been a problem. 124 */ 125 if ((name == 0) && (ELF_ST_BIND(sym->st_info) != STB_LOCAL)) { 126 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_NONAMESYM), 127 ifl->ifl_name, EC_WORD(symsecndx), symsecname, symndx, 128 EC_XWORD(name)); 129 } 130 return (strs + name); 131 } 132 133 /* 134 * For producing symbol names strings to use in error messages. 135 * If the symbol has a non-null name, then the string returned by 136 * this function is the output from demangle(), surrounded by 137 * single quotes. For null names, a descriptive string giving 138 * the symbol section and index is generated. 139 * 140 * This function uses an internal static buffer to hold the resulting 141 * string. The value returned is usable by the caller until the next 142 * call, at which point it is overwritten. 143 */ 144 static const char * 145 demangle_symname(const char *name, const char *symtab_name, Word symndx) 146 { 147 #define INIT_BUFSIZE 256 148 149 static char *buf; 150 static size_t bufsize = 0; 151 size_t len; 152 int use_name; 153 154 use_name = (name != NULL) && (*name != '\0'); 155 156 if (use_name) { 157 name = demangle(name); 158 len = strlen(name) + 2; /* Include room for quotes */ 159 } else { 160 name = MSG_ORIG(MSG_STR_EMPTY); 161 len = strlen(symtab_name) + 2 + CONV_INV_BUFSIZE; 162 } 163 len++; /* Null termination */ 164 165 /* If our buffer is too small, double it until it is big enough */ 166 if (len > bufsize) { 167 size_t new_bufsize = bufsize; 168 char *new_buf; 169 170 if (new_bufsize == 0) 171 new_bufsize = INIT_BUFSIZE; 172 while (len > new_bufsize) 173 new_bufsize *= 2; 174 if ((new_buf = libld_malloc(new_bufsize)) == NULL) 175 return (name); 176 buf = new_buf; 177 bufsize = new_bufsize; 178 } 179 180 if (use_name) { 181 (void) snprintf(buf, bufsize, MSG_ORIG(MSG_FMT_SYMNAM), name); 182 } else { 183 (void) snprintf(buf, bufsize, MSG_ORIG(MSG_FMT_NULLSYMNAM), 184 symtab_name, EC_WORD(symndx)); 185 } 186 187 return (buf); 188 189 #undef INIT_BUFSIZE 190 } 191 192 /* 193 * Shared objects can be built that define specific symbols that can not be 194 * directly bound to. These objects have a syminfo section (and an associated 195 * DF_1_NODIRECT dynamic flags entry). Scan this table looking for symbols 196 * that can't be bound to directly, and if this files symbol is presently 197 * referenced, mark it so that we don't directly bind to it. 198 */ 199 uintptr_t 200 ld_sym_nodirect(Is_desc *isp, Ifl_desc *ifl, Ofl_desc *ofl) 201 { 202 Shdr *sifshdr, *symshdr; 203 Syminfo *sifdata; 204 Sym *symdata; 205 char *strdata; 206 ulong_t cnt, _cnt; 207 208 /* 209 * Get the syminfo data, and determine the number of entries. 210 */ 211 sifshdr = isp->is_shdr; 212 sifdata = (Syminfo *)isp->is_indata->d_buf; 213 cnt = sifshdr->sh_size / sifshdr->sh_entsize; 214 215 /* 216 * Get the associated symbol table. 217 */ 218 if ((sifshdr->sh_link == 0) || (sifshdr->sh_link >= ifl->ifl_shnum)) { 219 /* 220 * Broken input file 221 */ 222 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO), 223 ifl->ifl_name, isp->is_name, EC_XWORD(sifshdr->sh_link)); 224 return (0); 225 } 226 symshdr = ifl->ifl_isdesc[sifshdr->sh_link]->is_shdr; 227 symdata = ifl->ifl_isdesc[sifshdr->sh_link]->is_indata->d_buf; 228 229 /* 230 * Get the string table associated with the symbol table. 231 */ 232 strdata = ifl->ifl_isdesc[symshdr->sh_link]->is_indata->d_buf; 233 234 /* 235 * Traverse the syminfo data for symbols that can't be directly 236 * bound to. 237 */ 238 for (_cnt = 1, sifdata++; _cnt < cnt; _cnt++, sifdata++) { 239 Sym *sym; 240 char *str; 241 Sym_desc *sdp; 242 243 if ((sifdata->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0) 244 continue; 245 246 sym = (Sym *)(symdata + _cnt); 247 str = (char *)(strdata + sym->st_name); 248 249 if ((sdp = ld_sym_find(str, SYM_NOHASH, NULL, ofl)) != NULL) { 250 if (ifl != sdp->sd_file) 251 continue; 252 253 sdp->sd_flags &= ~FLG_SY_DIR; 254 sdp->sd_flags |= FLG_SY_NDIR; 255 } 256 } 257 return (0); 258 } 259 260 /* 261 * If, during symbol processing, it is necessary to update a local symbols 262 * contents before we have generated the symbol tables in the output image, 263 * create a new symbol structure and copy the original symbol contents. While 264 * we are processing the input files, their local symbols are part of the 265 * read-only mapped image. Commonly, these symbols are copied to the new output 266 * file image and then updated to reflect their new address and any change in 267 * attributes. However, sometimes during relocation counting, it is necessary 268 * to adjust the symbols information. This routine provides for the generation 269 * of a new symbol image so that this update can be performed. 270 * All global symbols are copied to an internal symbol table to improve locality 271 * of reference and hence performance, and thus this copying is not necessary. 272 */ 273 uintptr_t 274 ld_sym_copy(Sym_desc *sdp) 275 { 276 Sym *nsym; 277 278 if (sdp->sd_flags & FLG_SY_CLEAN) { 279 if ((nsym = libld_malloc(sizeof (Sym))) == NULL) 280 return (S_ERROR); 281 *nsym = *(sdp->sd_sym); 282 sdp->sd_sym = nsym; 283 sdp->sd_flags &= ~FLG_SY_CLEAN; 284 } 285 return (1); 286 } 287 288 /* 289 * Finds a given name in the link editors internal symbol table. If no 290 * hash value is specified it is calculated. A pointer to the located 291 * Sym_desc entry is returned, or NULL if the symbol is not found. 292 */ 293 Sym_desc * 294 ld_sym_find(const char *name, Word hash, avl_index_t *where, Ofl_desc *ofl) 295 { 296 Sym_avlnode qsav, *sav; 297 298 if (hash == SYM_NOHASH) 299 /* LINTED */ 300 hash = (Word)elf_hash((const char *)name); 301 qsav.sav_hash = hash; 302 qsav.sav_name = name; 303 304 /* 305 * Perform search for symbol in AVL tree. Note that the 'where' field 306 * is passed in from the caller. If a 'where' is present, it can be 307 * used in subsequent 'ld_sym_enter()' calls if required. 308 */ 309 sav = avl_find(&ofl->ofl_symavl, &qsav, where); 310 311 /* 312 * If symbol was not found in the avl tree, return null to show that. 313 */ 314 if (sav == NULL) 315 return (NULL); 316 317 /* 318 * Return symbol found. 319 */ 320 return (sav->sav_sdp); 321 } 322 323 /* 324 * GCC sometimes emits local aliases for otherwise global symbols, such that 325 * it has a guaranteed way to refer to a symbol from the current object 326 * regardless of interposition. 327 * 328 * The only way we can match on these aliases is by them ending either 329 * ".localalias" or ".localalias.N" where N is any integer. 330 */ 331 static inline Boolean 332 is_gcc_localalias(Sym_desc *sdp) 333 { 334 char *p; 335 336 if (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL) 337 return (FALSE); 338 339 if ((p = strstr(sdp->sd_name, MSG_ORIG(MSG_SYM_LOCALALIAS))) != NULL) { 340 p += MSG_SYM_LOCALALIAS_SIZE; 341 switch (*p++) { 342 case '\0': /* unnumbered */ 343 return (TRUE); 344 case '.': /* numbered? */ 345 if (*p == '\0') /* no integer */ 346 return (FALSE); 347 while (ISDIGIT(*p)) /* skip integer */ 348 p++; 349 if (*p != '\0') /* non-integer chars */ 350 return (FALSE); 351 return (TRUE); 352 } 353 } 354 355 return (FALSE); 356 } 357 358 /* 359 * Enter a new symbol into the link editors internal symbol table. 360 * If the symbol is from an input file, information regarding the input file 361 * and input section is also recorded. Otherwise (file == NULL) the symbol 362 * has been internally generated (ie. _etext, _edata, etc.). 363 */ 364 Sym_desc * 365 ld_sym_enter(const char *name, Sym *osym, Word hash, Ifl_desc *ifl, 366 Ofl_desc *ofl, Word ndx, Word shndx, sd_flag_t sdflags, avl_index_t *where) 367 { 368 Sym_desc *sdp; 369 Sym_aux *sap; 370 Sym_avlnode *savl; 371 char *_name; 372 Sym *nsym; 373 Half etype; 374 uchar_t vis; 375 avl_index_t _where; 376 377 /* 378 * Establish the file type. 379 */ 380 if (ifl) 381 etype = ifl->ifl_ehdr->e_type; 382 else 383 etype = ET_NONE; 384 385 ofl->ofl_entercnt++; 386 387 /* 388 * Allocate a Sym Descriptor, Auxiliary Descriptor, and a Sym AVLNode - 389 * contiguously. 390 */ 391 if ((savl = libld_calloc(S_DROUND(sizeof (Sym_avlnode)) + 392 S_DROUND(sizeof (Sym_desc)) + 393 S_DROUND(sizeof (Sym_aux)), 1)) == NULL) 394 return ((Sym_desc *)S_ERROR); 395 sdp = (Sym_desc *)((uintptr_t)savl + 396 S_DROUND(sizeof (Sym_avlnode))); 397 sap = (Sym_aux *)((uintptr_t)sdp + 398 S_DROUND(sizeof (Sym_desc))); 399 400 savl->sav_sdp = sdp; 401 sdp->sd_file = ifl; 402 sdp->sd_aux = sap; 403 savl->sav_hash = sap->sa_hash = hash; 404 405 /* 406 * Copy the symbol table entry from the input file into the internal 407 * entry and have the symbol descriptor use it. 408 */ 409 sdp->sd_sym = nsym = &sap->sa_sym; 410 *nsym = *osym; 411 sdp->sd_shndx = shndx; 412 sdp->sd_flags |= sdflags; 413 414 if ((_name = libld_malloc(strlen(name) + 1)) == NULL) 415 return ((Sym_desc *)S_ERROR); 416 savl->sav_name = sdp->sd_name = (const char *)strcpy(_name, name); 417 418 /* 419 * Enter Symbol in AVL tree. 420 */ 421 if (where == 0) { 422 /* LINTED */ 423 Sym_avlnode *_savl; 424 /* 425 * If a previous ld_sym_find() hasn't initialized 'where' do it 426 * now. 427 */ 428 where = &_where; 429 _savl = avl_find(&ofl->ofl_symavl, savl, where); 430 assert(_savl == NULL); 431 } 432 avl_insert(&ofl->ofl_symavl, savl, *where); 433 434 /* 435 * Record the section index. This is possible because the 436 * `ifl_isdesc' table is filled before we start symbol processing. 437 */ 438 if ((sdflags & FLG_SY_SPECSEC) || (nsym->st_shndx == SHN_UNDEF)) 439 sdp->sd_isc = NULL; 440 else { 441 sdp->sd_isc = ifl->ifl_isdesc[shndx]; 442 443 /* 444 * If this symbol is from a relocatable object, make sure that 445 * it is still associated with a section. For example, an 446 * unknown section type (SHT_NULL) would have been rejected on 447 * input with a warning. Here, we make the use of the symbol 448 * fatal. A symbol descriptor is still returned, so that the 449 * caller can continue processing all symbols, and hence flush 450 * out as many error conditions as possible. 451 */ 452 if ((etype == ET_REL) && (sdp->sd_isc == NULL)) { 453 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYM_INVSEC), 454 name, ifl->ifl_name, EC_XWORD(shndx)); 455 return (sdp); 456 } 457 } 458 459 /* 460 * Mark any COMMON symbols as 'tentative'. 461 */ 462 if (sdflags & FLG_SY_SPECSEC) { 463 if (nsym->st_shndx == SHN_COMMON) 464 sdp->sd_flags |= FLG_SY_TENTSYM; 465 #if defined(_ELF64) 466 else if ((ld_targ.t_m.m_mach == EM_AMD64) && 467 (nsym->st_shndx == SHN_X86_64_LCOMMON)) 468 sdp->sd_flags |= FLG_SY_TENTSYM; 469 #endif 470 } 471 472 /* 473 * Establish the symbols visibility and reference. 474 */ 475 vis = ELF_ST_VISIBILITY(nsym->st_other); 476 477 if ((etype == ET_NONE) || (etype == ET_REL)) { 478 switch (vis) { 479 case STV_DEFAULT: 480 sdp->sd_flags |= FLG_SY_DEFAULT; 481 break; 482 case STV_INTERNAL: 483 case STV_HIDDEN: 484 sdp->sd_flags |= FLG_SY_HIDDEN; 485 break; 486 case STV_PROTECTED: 487 sdp->sd_flags |= FLG_SY_PROTECT; 488 break; 489 case STV_EXPORTED: 490 sdp->sd_flags |= FLG_SY_EXPORT; 491 break; 492 case STV_SINGLETON: 493 sdp->sd_flags |= (FLG_SY_SINGLE | FLG_SY_NDIR); 494 ofl->ofl_flags1 |= (FLG_OF1_NDIRECT | FLG_OF1_NGLBDIR); 495 break; 496 case STV_ELIMINATE: 497 sdp->sd_flags |= (FLG_SY_HIDDEN | FLG_SY_ELIM); 498 break; 499 default: 500 assert(vis <= STV_ELIMINATE); 501 } 502 503 sdp->sd_ref = REF_REL_NEED; 504 505 /* 506 * Under -Bnodirect, all exported interfaces that have not 507 * explicitly been defined protected or directly bound to, are 508 * tagged to prevent direct binding. 509 */ 510 if ((ofl->ofl_flags1 & FLG_OF1_ALNODIR) && 511 ((sdp->sd_flags & (FLG_SY_PROTECT | FLG_SY_DIR)) == 0) && 512 (nsym->st_shndx != SHN_UNDEF)) { 513 sdp->sd_flags |= FLG_SY_NDIR; 514 } 515 } else { 516 sdp->sd_ref = REF_DYN_SEEN; 517 518 /* 519 * If this is a protected symbol, remember this. Note, this 520 * state is different from the FLG_SY_PROTECT used to establish 521 * a symbol definitions visibility. This state is used to warn 522 * against possible copy relocations against this referenced 523 * symbol. 524 */ 525 if (vis == STV_PROTECTED) 526 sdp->sd_flags |= FLG_SY_PROT; 527 528 /* 529 * If this is a SINGLETON definition, then indicate the symbol 530 * can not be directly bound to, and retain the visibility. 531 * This visibility will be inherited by any references made to 532 * this symbol. 533 */ 534 if ((vis == STV_SINGLETON) && (nsym->st_shndx != SHN_UNDEF)) 535 sdp->sd_flags |= (FLG_SY_SINGLE | FLG_SY_NDIR); 536 537 /* 538 * If the new symbol is from a shared library and is associated 539 * with a SHT_NOBITS section then this symbol originated from a 540 * tentative symbol. 541 */ 542 if (sdp->sd_isc && 543 (sdp->sd_isc->is_shdr->sh_type == SHT_NOBITS)) 544 sdp->sd_flags |= FLG_SY_TENTSYM; 545 } 546 547 /* 548 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF so as to 549 * simplify future processing. 550 */ 551 if (nsym->st_shndx == SHN_SUNW_IGNORE) { 552 sdp->sd_shndx = shndx = SHN_UNDEF; 553 sdp->sd_flags |= (FLG_SY_REDUCED | 554 FLG_SY_HIDDEN | FLG_SY_IGNORE | FLG_SY_ELIM); 555 } 556 557 /* 558 * If this is an undefined, or common symbol from a relocatable object 559 * determine whether it is a global or weak reference (see build_osym(), 560 * where REF_DYN_NEED definitions are returned back to undefines). 561 */ 562 if ((etype == ET_REL) && 563 (ELF_ST_BIND(nsym->st_info) == STB_GLOBAL) && 564 ((nsym->st_shndx == SHN_UNDEF) || ((sdflags & FLG_SY_SPECSEC) && 565 #if defined(_ELF64) 566 ((nsym->st_shndx == SHN_COMMON) || 567 ((ld_targ.t_m.m_mach == EM_AMD64) && 568 (nsym->st_shndx == SHN_X86_64_LCOMMON)))))) 569 #else 570 /* BEGIN CSTYLED */ 571 (nsym->st_shndx == SHN_COMMON)))) 572 /* END CSTYLED */ 573 #endif 574 sdp->sd_flags |= FLG_SY_GLOBREF; 575 576 /* 577 * Record the input filename on the referenced or defined files list 578 * for possible later diagnostics. The `sa_rfile' pointer contains the 579 * name of the file that first referenced this symbol and is used to 580 * generate undefined symbol diagnostics (refer to sym_undef_entry()). 581 * Note that this entry can be overridden if a reference from a 582 * relocatable object is found after a reference from a shared object 583 * (refer to sym_override()). 584 * The `sa_dfiles' list is used to maintain the list of files that 585 * define the same symbol. This list can be used for two reasons: 586 * 587 * - To save the first definition of a symbol that is not available 588 * for this link-edit. 589 * 590 * - To save all definitions of a symbol when the -m option is in 591 * effect. This is optional as it is used to list multiple 592 * (interposed) definitions of a symbol (refer to ldmap_out()), 593 * and can be quite expensive. 594 */ 595 if (nsym->st_shndx == SHN_UNDEF) { 596 sap->sa_rfile = ifl->ifl_name; 597 } else { 598 if (sdp->sd_ref == REF_DYN_SEEN) { 599 /* 600 * A symbol is determined to be unavailable if it 601 * belongs to a version of a shared object that this 602 * user does not wish to use, or if it belongs to an 603 * implicit shared object. 604 */ 605 if (ifl->ifl_vercnt) { 606 Ver_index *vip; 607 Half vndx = ifl->ifl_versym[ndx]; 608 609 sap->sa_dverndx = vndx; 610 vip = &ifl->ifl_verndx[vndx]; 611 if (!(vip->vi_flags & FLG_VER_AVAIL)) { 612 sdp->sd_flags |= FLG_SY_NOTAVAIL; 613 sap->sa_vfile = ifl->ifl_name; 614 } 615 } 616 if (!(ifl->ifl_flags & FLG_IF_NEEDED)) 617 sdp->sd_flags |= FLG_SY_NOTAVAIL; 618 619 } else if (etype == ET_REL) { 620 /* 621 * If this symbol has been obtained from a versioned 622 * input relocatable object then the new symbol must be 623 * promoted to the versioning of the output file. 624 */ 625 if (ifl->ifl_versym) 626 ld_vers_promote(sdp, ndx, ifl, ofl); 627 } 628 629 if ((ofl->ofl_flags & FLG_OF_GENMAP) && 630 ((sdflags & FLG_SY_SPECSEC) == 0)) 631 if (aplist_append(&sap->sa_dfiles, ifl->ifl_name, 632 AL_CNT_SDP_DFILES) == NULL) 633 return ((Sym_desc *)S_ERROR); 634 } 635 636 /* 637 * Provided we're not processing a mapfile, diagnose the entered symbol. 638 * Mapfile processing requires the symbol to be updated with additional 639 * information, therefore the diagnosing of the symbol is deferred until 640 * later (see Dbg_map_symbol()). 641 */ 642 if ((ifl == NULL) || ((ifl->ifl_flags & FLG_IF_MAPFILE) == 0)) 643 DBG_CALL(Dbg_syms_entered(ofl, nsym, sdp)); 644 645 return (sdp); 646 } 647 648 /* 649 * Add a special symbol to the symbol table. Takes special symbol name with 650 * and without underscores. This routine is called, after all other symbol 651 * resolution has completed, to generate a reserved absolute symbol (the 652 * underscore version). Special symbols are updated with the appropriate 653 * values in update_osym(). If the user has already defined this symbol 654 * issue a warning and leave the symbol as is. If the non-underscore symbol 655 * is referenced then turn it into a weak alias of the underscored symbol. 656 * 657 * The bits in sdflags_u are OR'd into the flags field of the symbol for the 658 * underscored symbol. 659 * 660 * If this is a global symbol, and it hasn't explicitly been defined as being 661 * directly bound to, indicate that it can't be directly bound to. 662 * Historically, most special symbols only have meaning to the object in which 663 * they exist, however, they've always been global. To ensure compatibility 664 * with any unexpected use presently in effect, ensure these symbols don't get 665 * directly bound to. Note, that establishing this state here isn't sufficient 666 * to create a syminfo table, only if a syminfo table is being created by some 667 * other symbol directives will the nodirect binding be recorded. This ensures 668 * we don't create syminfo sections for all objects we create, as this might add 669 * unnecessary bloat to users who haven't explicitly requested extra symbol 670 * information. 671 */ 672 static uintptr_t 673 sym_add_spec(const char *name, const char *uname, Word sdaux_id, 674 sd_flag_t sdflags_u, sd_flag_t sdflags, Ofl_desc *ofl) 675 { 676 Sym_desc *sdp; 677 Sym_desc *usdp; 678 Sym *sym; 679 Word hash; 680 avl_index_t where; 681 682 /* LINTED */ 683 hash = (Word)elf_hash(uname); 684 if (usdp = ld_sym_find(uname, hash, &where, ofl)) { 685 /* 686 * If the underscore symbol exists and is undefined, or was 687 * defined in a shared library, convert it to a local symbol. 688 * Otherwise leave it as is and warn the user. 689 */ 690 if ((usdp->sd_shndx == SHN_UNDEF) || 691 (usdp->sd_ref != REF_REL_NEED)) { 692 usdp->sd_ref = REF_REL_NEED; 693 usdp->sd_shndx = usdp->sd_sym->st_shndx = SHN_ABS; 694 usdp->sd_flags |= FLG_SY_SPECSEC | sdflags_u; 695 usdp->sd_sym->st_info = 696 ELF_ST_INFO(STB_GLOBAL, STT_OBJECT); 697 usdp->sd_isc = NULL; 698 usdp->sd_sym->st_size = 0; 699 usdp->sd_sym->st_value = 0; 700 /* LINTED */ 701 usdp->sd_aux->sa_symspec = (Half)sdaux_id; 702 703 /* 704 * If a user hasn't specifically indicated that the 705 * scope of this symbol be made local, then leave it 706 * as global (ie. prevent automatic scoping). The GOT 707 * should be defined protected, whereas all other 708 * special symbols are tagged as no-direct. 709 */ 710 if (!SYM_IS_HIDDEN(usdp) && 711 (sdflags & FLG_SY_DEFAULT)) { 712 usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL; 713 if (sdaux_id == SDAUX_ID_GOT) { 714 usdp->sd_flags &= ~FLG_SY_NDIR; 715 usdp->sd_flags |= FLG_SY_PROTECT; 716 usdp->sd_sym->st_other = STV_PROTECTED; 717 } else if ( 718 ((usdp->sd_flags & FLG_SY_DIR) == 0) && 719 ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) { 720 usdp->sd_flags |= FLG_SY_NDIR; 721 } 722 } 723 usdp->sd_flags |= sdflags; 724 725 /* 726 * If the reference originated from a mapfile ensure 727 * we mark the symbol as used. 728 */ 729 if (usdp->sd_flags & FLG_SY_MAPREF) 730 usdp->sd_flags |= FLG_SY_MAPUSED; 731 732 DBG_CALL(Dbg_syms_updated(ofl, usdp, uname)); 733 } else { 734 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_RESERVE), 735 uname, usdp->sd_file->ifl_name); 736 } 737 } else { 738 /* 739 * If the symbol does not exist create it. 740 */ 741 if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL) 742 return (S_ERROR); 743 sym->st_shndx = SHN_ABS; 744 sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT); 745 sym->st_size = 0; 746 sym->st_value = 0; 747 DBG_CALL(Dbg_syms_created(ofl->ofl_lml, uname)); 748 if ((usdp = ld_sym_enter(uname, sym, hash, (Ifl_desc *)NULL, 749 ofl, 0, SHN_ABS, (FLG_SY_SPECSEC | sdflags_u), &where)) == 750 (Sym_desc *)S_ERROR) 751 return (S_ERROR); 752 usdp->sd_ref = REF_REL_NEED; 753 /* LINTED */ 754 usdp->sd_aux->sa_symspec = (Half)sdaux_id; 755 756 usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL; 757 758 if (sdaux_id == SDAUX_ID_GOT) { 759 usdp->sd_flags |= FLG_SY_PROTECT; 760 usdp->sd_sym->st_other = STV_PROTECTED; 761 } else if ((sdflags & FLG_SY_DEFAULT) && 762 ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) { 763 usdp->sd_flags |= FLG_SY_NDIR; 764 } 765 usdp->sd_flags |= sdflags; 766 } 767 768 if (name && (sdp = ld_sym_find(name, SYM_NOHASH, NULL, ofl)) && 769 (sdp->sd_sym->st_shndx == SHN_UNDEF)) { 770 uchar_t bind; 771 772 /* 773 * If the non-underscore symbol exists and is undefined 774 * convert it to be a local. If the underscore has 775 * sa_symspec set (ie. it was created above) then simulate this 776 * as a weak alias. 777 */ 778 sdp->sd_ref = REF_REL_NEED; 779 sdp->sd_shndx = sdp->sd_sym->st_shndx = SHN_ABS; 780 sdp->sd_flags |= FLG_SY_SPECSEC; 781 sdp->sd_isc = NULL; 782 sdp->sd_sym->st_size = 0; 783 sdp->sd_sym->st_value = 0; 784 /* LINTED */ 785 sdp->sd_aux->sa_symspec = (Half)sdaux_id; 786 if (usdp->sd_aux->sa_symspec) { 787 usdp->sd_aux->sa_linkndx = 0; 788 sdp->sd_aux->sa_linkndx = 0; 789 bind = STB_WEAK; 790 } else 791 bind = STB_GLOBAL; 792 sdp->sd_sym->st_info = ELF_ST_INFO(bind, STT_OBJECT); 793 794 /* 795 * If a user hasn't specifically indicated the scope of this 796 * symbol be made local then leave it as global (ie. prevent 797 * automatic scoping). The GOT should be defined protected, 798 * whereas all other special symbols are tagged as no-direct. 799 */ 800 if (!SYM_IS_HIDDEN(sdp) && 801 (sdflags & FLG_SY_DEFAULT)) { 802 sdp->sd_aux->sa_overndx = VER_NDX_GLOBAL; 803 if (sdaux_id == SDAUX_ID_GOT) { 804 sdp->sd_flags &= ~FLG_SY_NDIR; 805 sdp->sd_flags |= FLG_SY_PROTECT; 806 sdp->sd_sym->st_other = STV_PROTECTED; 807 } else if (((sdp->sd_flags & FLG_SY_DIR) == 0) && 808 ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) { 809 sdp->sd_flags |= FLG_SY_NDIR; 810 } 811 } 812 sdp->sd_flags |= sdflags; 813 814 /* 815 * If the reference originated from a mapfile ensure 816 * we mark the symbol as used. 817 */ 818 if (sdp->sd_flags & FLG_SY_MAPREF) 819 sdp->sd_flags |= FLG_SY_MAPUSED; 820 821 DBG_CALL(Dbg_syms_updated(ofl, sdp, name)); 822 } 823 return (1); 824 } 825 826 827 /* 828 * Undefined symbols can fall into one of four types: 829 * 830 * - the symbol is really undefined (SHN_UNDEF). 831 * 832 * - versioning has been enabled, however this symbol has not been assigned 833 * to one of the defined versions. 834 * 835 * - the symbol has been defined by an implicitly supplied library, ie. one 836 * which was encounted because it was NEEDED by another library, rather 837 * than from a command line supplied library which would become the only 838 * dependency of the output file being produced. 839 * 840 * - the symbol has been defined by a version of a shared object that is 841 * not permitted for this link-edit. 842 * 843 * In all cases the file who made the first reference to this symbol will have 844 * been recorded via the `sa_rfile' pointer. 845 */ 846 typedef enum { 847 UNDEF, NOVERSION, IMPLICIT, NOTAVAIL, 848 BNDLOCAL 849 } Type; 850 851 static const Msg format[] = { 852 MSG_SYM_UND_UNDEF, /* MSG_INTL(MSG_SYM_UND_UNDEF) */ 853 MSG_SYM_UND_NOVER, /* MSG_INTL(MSG_SYM_UND_NOVER) */ 854 MSG_SYM_UND_IMPL, /* MSG_INTL(MSG_SYM_UND_IMPL) */ 855 MSG_SYM_UND_NOTA, /* MSG_INTL(MSG_SYM_UND_NOTA) */ 856 MSG_SYM_UND_BNDLOCAL /* MSG_INTL(MSG_SYM_UND_BNDLOCAL) */ 857 }; 858 859 /* 860 * Issue an undefined symbol message for the given symbol. 861 * 862 * entry: 863 * ofl - Output descriptor 864 * sdp - Undefined symbol to report 865 * type - Type of undefined symbol 866 * ofl_flag - One of 0, FLG_OF_FATAL, or FLG_OF_WARN. 867 * undef_state - Address of variable to be initialized to 0 868 * before the first call to sym_undef_entry, and passed 869 * to each subsequent call. A non-zero value for *undef_state 870 * indicates that this is not the first call in the series. 871 * 872 * exit: 873 * If *undef_state is 0, a title is issued. 874 * 875 * A message for the undefined symbol is issued. 876 * 877 * If ofl_flag is non-zero, its value is OR'd into *undef_state. Otherwise, 878 * all bits other than FLG_OF_FATAL and FLG_OF_WARN are set, in order to 879 * provide *undef_state with a non-zero value. These other bits have 880 * no meaning beyond that, and serve to ensure that *undef_state is 881 * non-zero if sym_undef_entry() has been called. 882 */ 883 static void 884 sym_undef_entry(Ofl_desc *ofl, Sym_desc *sdp, Type type, ofl_flag_t ofl_flag, 885 ofl_flag_t *undef_state) 886 { 887 const char *name1, *name2, *name3; 888 Ifl_desc *ifl = sdp->sd_file; 889 Sym_aux *sap = sdp->sd_aux; 890 891 if (*undef_state == 0) 892 ld_eprintf(ofl, ERR_NONE, MSG_INTL(MSG_SYM_FMT_UNDEF), 893 MSG_INTL(MSG_SYM_UNDEF_ITM_11), 894 MSG_INTL(MSG_SYM_UNDEF_ITM_21), 895 MSG_INTL(MSG_SYM_UNDEF_ITM_12), 896 MSG_INTL(MSG_SYM_UNDEF_ITM_22)); 897 898 ofl->ofl_flags |= ofl_flag; 899 *undef_state |= ofl_flag ? ofl_flag : ~(FLG_OF_FATAL | FLG_OF_WARN); 900 901 switch (type) { 902 case UNDEF: 903 case BNDLOCAL: 904 name1 = sap->sa_rfile; 905 break; 906 case NOVERSION: 907 name1 = ifl->ifl_name; 908 break; 909 case IMPLICIT: 910 name1 = sap->sa_rfile; 911 name2 = ifl->ifl_name; 912 break; 913 case NOTAVAIL: 914 name1 = sap->sa_rfile; 915 name2 = sap->sa_vfile; 916 name3 = ifl->ifl_verndx[sap->sa_dverndx].vi_name; 917 break; 918 default: 919 return; 920 } 921 922 ld_eprintf(ofl, ERR_NONE, MSG_INTL(format[type]), 923 demangle(sdp->sd_name), name1, name2, name3); 924 } 925 926 /* 927 * If an undef symbol exists naming a bound for the output section, 928 * turn it into a defined symbol with the correct value. 929 * 930 * We set an arbitrary 1KB limit on the resulting symbol names. 931 */ 932 static void 933 sym_add_bounds(Ofl_desc *ofl, Os_desc *osp, Word bound) 934 { 935 Sym_desc *bsdp; 936 char symn[1024]; 937 size_t nsz; 938 939 switch (bound) { 940 case SDAUX_ID_SECBOUND_START: 941 nsz = snprintf(symn, sizeof (symn), "%s%s", 942 MSG_ORIG(MSG_SYM_SECBOUND_START), osp->os_name); 943 if (nsz >= sizeof (symn)) 944 return; 945 break; 946 case SDAUX_ID_SECBOUND_STOP: 947 nsz = snprintf(symn, sizeof (symn), "%s%s", 948 MSG_ORIG(MSG_SYM_SECBOUND_STOP), osp->os_name); 949 if (nsz >= sizeof (symn)) 950 return; 951 break; 952 default: 953 assert(0); 954 } 955 956 if ((bsdp = ld_sym_find(symn, SYM_NOHASH, NULL, ofl)) != NULL) { 957 if ((bsdp->sd_shndx != SHN_UNDEF) && 958 (bsdp->sd_ref == REF_REL_NEED)) { 959 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_RESERVE), 960 symn, bsdp->sd_file->ifl_name); 961 return; 962 } 963 964 DBG_CALL(Dbg_syms_updated(ofl, bsdp, symn)); 965 966 bsdp->sd_aux->sa_symspec = bound; 967 bsdp->sd_aux->sa_boundsec = osp; 968 bsdp->sd_flags |= FLG_SY_SPECSEC; 969 bsdp->sd_ref = REF_REL_NEED; 970 bsdp->sd_sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE); 971 bsdp->sd_sym->st_other = STV_PROTECTED; 972 bsdp->sd_isc = NULL; 973 bsdp->sd_sym->st_size = 0; 974 bsdp->sd_sym->st_value = 0; 975 bsdp->sd_shndx = bsdp->sd_sym->st_shndx = SHN_ABS; 976 } 977 } 978 979 static Boolean 980 is_cname(const char *name) 981 { 982 if (strlen(name) == strspn(name, 983 "abcdefghijklmnopqrstuvwxyz" 984 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 985 "0123456789" 986 "_")) 987 return (TRUE); 988 else 989 return (FALSE); 990 } 991 992 /* 993 * At this point all symbol input processing has been completed, therefore 994 * complete the symbol table entries by generating any necessary internal 995 * symbols. 996 */ 997 uintptr_t 998 ld_sym_spec(Ofl_desc *ofl) 999 { 1000 Sym_desc *sdp; 1001 Sg_desc *sgp; 1002 1003 DBG_CALL(Dbg_syms_spec_title(ofl->ofl_lml)); 1004 1005 /* 1006 * For each section in the output file, look for symbols named for the 1007 * __start/__stop patterns. If references exist, flesh the symbols to 1008 * be defined. 1009 * 1010 * The symbols are given values at the same time as the other special 1011 * symbols. 1012 */ 1013 if (!(ofl->ofl_flags & FLG_OF_RELOBJ) || 1014 (ofl->ofl_flags & FLG_OF_KMOD)) { 1015 Aliste idx1; 1016 1017 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) { 1018 Os_desc *osp; 1019 Aliste idx2; 1020 1021 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) { 1022 if (is_cname(osp->os_name)) { 1023 sym_add_bounds(ofl, osp, 1024 SDAUX_ID_SECBOUND_START); 1025 sym_add_bounds(ofl, osp, 1026 SDAUX_ID_SECBOUND_STOP); 1027 } 1028 } 1029 } 1030 } 1031 1032 if (ofl->ofl_flags & FLG_OF_RELOBJ) 1033 return (1); 1034 1035 if (sym_add_spec(MSG_ORIG(MSG_SYM_ETEXT), MSG_ORIG(MSG_SYM_ETEXT_U), 1036 SDAUX_ID_ETEXT, 0, (FLG_SY_DEFAULT | FLG_SY_EXPDEF), 1037 ofl) == S_ERROR) 1038 return (S_ERROR); 1039 if (sym_add_spec(MSG_ORIG(MSG_SYM_EDATA), MSG_ORIG(MSG_SYM_EDATA_U), 1040 SDAUX_ID_EDATA, 0, (FLG_SY_DEFAULT | FLG_SY_EXPDEF), 1041 ofl) == S_ERROR) 1042 return (S_ERROR); 1043 if (sym_add_spec(MSG_ORIG(MSG_SYM_END), MSG_ORIG(MSG_SYM_END_U), 1044 SDAUX_ID_END, FLG_SY_DYNSORT, (FLG_SY_DEFAULT | FLG_SY_EXPDEF), 1045 ofl) == S_ERROR) 1046 return (S_ERROR); 1047 if (sym_add_spec(MSG_ORIG(MSG_SYM_L_END), MSG_ORIG(MSG_SYM_L_END_U), 1048 SDAUX_ID_END, 0, FLG_SY_HIDDEN, ofl) == S_ERROR) 1049 return (S_ERROR); 1050 if (sym_add_spec(MSG_ORIG(MSG_SYM_L_START), MSG_ORIG(MSG_SYM_L_START_U), 1051 SDAUX_ID_START, 0, FLG_SY_HIDDEN, ofl) == S_ERROR) 1052 return (S_ERROR); 1053 1054 /* 1055 * Historically we've always produced a _DYNAMIC symbol, even for 1056 * static executables (in which case its value will be 0). 1057 */ 1058 if (sym_add_spec(MSG_ORIG(MSG_SYM_DYNAMIC), MSG_ORIG(MSG_SYM_DYNAMIC_U), 1059 SDAUX_ID_DYN, FLG_SY_DYNSORT, (FLG_SY_DEFAULT | FLG_SY_EXPDEF), 1060 ofl) == S_ERROR) 1061 return (S_ERROR); 1062 1063 if (OFL_ALLOW_DYNSYM(ofl)) 1064 if (sym_add_spec(MSG_ORIG(MSG_SYM_PLKTBL), 1065 MSG_ORIG(MSG_SYM_PLKTBL_U), SDAUX_ID_PLT, 1066 FLG_SY_DYNSORT, (FLG_SY_DEFAULT | FLG_SY_EXPDEF), 1067 ofl) == S_ERROR) 1068 return (S_ERROR); 1069 1070 /* 1071 * A GOT reference will be accompanied by the associated GOT symbol. 1072 * Make sure it gets assigned the appropriate special attributes. 1073 */ 1074 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), 1075 SYM_NOHASH, NULL, ofl)) != NULL) && (sdp->sd_ref != REF_DYN_SEEN)) { 1076 if (sym_add_spec(MSG_ORIG(MSG_SYM_GOFTBL), 1077 MSG_ORIG(MSG_SYM_GOFTBL_U), SDAUX_ID_GOT, FLG_SY_DYNSORT, 1078 (FLG_SY_DEFAULT | FLG_SY_EXPDEF), ofl) == S_ERROR) 1079 return (S_ERROR); 1080 } 1081 1082 return (1); 1083 } 1084 1085 /* 1086 * Determine a potential capability symbol's visibility. 1087 * 1088 * The -z symbolcap option transforms an object capabilities relocatable object 1089 * into a symbol capabilities relocatable object. Any global function symbols, 1090 * or initialized global data symbols are candidates for transforming into local 1091 * symbol capabilities definitions. However, if a user indicates that a symbol 1092 * should be demoted to local using a mapfile, then there is no need to 1093 * transform the associated global symbol. 1094 * 1095 * Normally, a symbol's visibility is determined after the symbol resolution 1096 * process, after all symbol state has been gathered and resolved. However, 1097 * for -z symbolcap, this determination is too late. When a global symbol is 1098 * read from an input file we need to determine it's visibility so as to decide 1099 * whether to create a local or not. 1100 * 1101 * If a user has explicitly defined this symbol as having local scope within a 1102 * mapfile, then a symbol of the same name already exists. However, explicit 1103 * local definitions are uncommon, as most mapfiles define the global symbol 1104 * requirements together with an auto-reduction directive '*'. If this state 1105 * has been defined, then we must make sure that the new symbol isn't a type 1106 * that can not be demoted to local. 1107 */ 1108 static int 1109 sym_cap_vis(const char *name, Word hash, Sym *sym, Ofl_desc *ofl) 1110 { 1111 Sym_desc *sdp; 1112 uchar_t vis; 1113 avl_index_t where; 1114 sd_flag_t sdflags = 0; 1115 1116 /* 1117 * Determine the visibility of the new symbol. 1118 */ 1119 vis = ELF_ST_VISIBILITY(sym->st_other); 1120 switch (vis) { 1121 case STV_EXPORTED: 1122 sdflags |= FLG_SY_EXPORT; 1123 break; 1124 case STV_SINGLETON: 1125 sdflags |= FLG_SY_SINGLE; 1126 break; 1127 case STV_HIDDEN: 1128 sdflags |= FLG_SY_HIDDEN; 1129 break; 1130 } 1131 1132 /* 1133 * Determine whether a symbol definition already exists, and if so 1134 * obtain the visibility. 1135 */ 1136 if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL) 1137 sdflags |= sdp->sd_flags; 1138 1139 /* 1140 * Determine whether the symbol flags indicate this symbol should be 1141 * hidden. 1142 */ 1143 if ((ofl->ofl_flags & (FLG_OF_AUTOLCL | FLG_OF_AUTOELM)) && 1144 ((sdflags & MSK_SY_NOAUTO) == 0)) 1145 sdflags |= FLG_SY_HIDDEN; 1146 1147 return ((sdflags & FLG_SY_HIDDEN) == 0); 1148 } 1149 1150 /* 1151 * This routine checks to see if a symbols visibility needs to be reduced to 1152 * either SYMBOLIC or LOCAL. This routine can be called from either 1153 * reloc_init() or sym_validate(). 1154 */ 1155 void 1156 ld_sym_adjust_vis(Sym_desc *sdp, Ofl_desc *ofl) 1157 { 1158 ofl_flag_t oflags = ofl->ofl_flags; 1159 Sym *sym = sdp->sd_sym; 1160 1161 if ((sdp->sd_ref == REF_REL_NEED) && 1162 (sdp->sd_sym->st_shndx != SHN_UNDEF)) { 1163 /* 1164 * If auto-reduction/elimination is enabled, reduce any 1165 * non-versioned, and non-local capabilities global symbols. 1166 * A symbol is a candidate for auto-reduction/elimination if: 1167 * 1168 * - the symbol wasn't explicitly defined within a mapfile 1169 * (in which case all the necessary state has been applied 1170 * to the symbol), or 1171 * - the symbol isn't one of the family of reserved 1172 * special symbols (ie. _end, _etext, etc.), or 1173 * - the symbol isn't a SINGLETON, or 1174 * - the symbol wasn't explicitly defined within a version 1175 * definition associated with an input relocatable object. 1176 * 1177 * Indicate that the symbol has been reduced as it may be 1178 * necessary to print these symbols later. 1179 */ 1180 if ((oflags & (FLG_OF_AUTOLCL | FLG_OF_AUTOELM)) && 1181 ((sdp->sd_flags & MSK_SY_NOAUTO) == 0)) { 1182 if ((sdp->sd_flags & FLG_SY_HIDDEN) == 0) { 1183 sdp->sd_flags |= 1184 (FLG_SY_REDUCED | FLG_SY_HIDDEN); 1185 } 1186 1187 if (oflags & (FLG_OF_REDLSYM | FLG_OF_AUTOELM)) { 1188 sdp->sd_flags |= FLG_SY_ELIM; 1189 sym->st_other = STV_ELIMINATE | 1190 (sym->st_other & ~MSK_SYM_VISIBILITY); 1191 } else if (ELF_ST_VISIBILITY(sym->st_other) != 1192 STV_INTERNAL) 1193 sym->st_other = STV_HIDDEN | 1194 (sym->st_other & ~MSK_SYM_VISIBILITY); 1195 } 1196 1197 /* 1198 * If -Bsymbolic is in effect, and the symbol hasn't explicitly 1199 * been defined nodirect (via a mapfile), then bind the global 1200 * symbol symbolically and assign the STV_PROTECTED visibility 1201 * attribute. 1202 */ 1203 if ((oflags & FLG_OF_SYMBOLIC) && 1204 ((sdp->sd_flags & (FLG_SY_HIDDEN | FLG_SY_NDIR)) == 0)) { 1205 sdp->sd_flags |= FLG_SY_PROTECT; 1206 if (ELF_ST_VISIBILITY(sym->st_other) == STV_DEFAULT) 1207 sym->st_other = STV_PROTECTED | 1208 (sym->st_other & ~MSK_SYM_VISIBILITY); 1209 } 1210 } 1211 1212 /* 1213 * Indicate that this symbol has had it's visibility checked so that 1214 * we don't need to do this investigation again. 1215 */ 1216 sdp->sd_flags |= FLG_SY_VISIBLE; 1217 } 1218 1219 /* 1220 * Make sure a symbol definition is local to the object being built. 1221 */ 1222 inline static int 1223 ensure_sym_local(Ofl_desc *ofl, Sym_desc *sdp, const char *str) 1224 { 1225 if (sdp->sd_sym->st_shndx == SHN_UNDEF) { 1226 if (str) { 1227 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYM_UNDEF), 1228 str, demangle((char *)sdp->sd_name)); 1229 } 1230 return (1); 1231 } 1232 if (sdp->sd_ref != REF_REL_NEED) { 1233 if (str) { 1234 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYM_EXTERN), 1235 str, demangle((char *)sdp->sd_name), 1236 sdp->sd_file->ifl_name); 1237 } 1238 return (1); 1239 } 1240 1241 sdp->sd_flags |= FLG_SY_UPREQD; 1242 if (sdp->sd_isc) { 1243 sdp->sd_isc->is_flags |= FLG_IS_SECTREF; 1244 sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF; 1245 } 1246 return (0); 1247 } 1248 1249 /* 1250 * Make sure all the symbol definitions required for initarray, finiarray, or 1251 * preinitarray's are local to the object being built. 1252 */ 1253 static int 1254 ensure_array_local(Ofl_desc *ofl, APlist *apl, const char *str) 1255 { 1256 Aliste idx; 1257 Sym_desc *sdp; 1258 int ret = 0; 1259 1260 for (APLIST_TRAVERSE(apl, idx, sdp)) 1261 ret += ensure_sym_local(ofl, sdp, str); 1262 1263 return (ret); 1264 } 1265 1266 /* 1267 * After all symbol table input processing has been finished, and all relocation 1268 * counting has been carried out (ie. no more symbols will be read, generated, 1269 * or modified), validate and count the relevant entries: 1270 * 1271 * - check and print any undefined symbols remaining. Note that if a symbol 1272 * has been defined by virtue of the inclusion of an implicit shared 1273 * library, it is still classed as undefined. 1274 * 1275 * - count the number of global needed symbols together with the size of 1276 * their associated name strings (if scoping has been indicated these 1277 * symbols may be reduced to locals). 1278 * 1279 * - establish the size and alignment requirements for the global .bss 1280 * section (the alignment of this section is based on the first symbol 1281 * that it will contain). 1282 */ 1283 uintptr_t 1284 ld_sym_validate(Ofl_desc *ofl) 1285 { 1286 Sym_avlnode *sav; 1287 Sym_desc *sdp; 1288 Sym *sym; 1289 ofl_flag_t oflags = ofl->ofl_flags; 1290 ofl_flag_t undef = 0, needed = 0, verdesc = 0; 1291 Xword bssalign = 0, tlsalign = 0; 1292 Boolean need_bss, need_tlsbss; 1293 Xword bsssize = 0, tlssize = 0; 1294 #if defined(_ELF64) 1295 Xword lbssalign = 0, lbsssize = 0; 1296 Boolean need_lbss; 1297 #endif 1298 int ret, allow_ldynsym; 1299 uchar_t type; 1300 ofl_flag_t undef_state = 0; 1301 1302 DBG_CALL(Dbg_basic_validate(ofl->ofl_lml)); 1303 1304 /* 1305 * The need_XXX booleans are used to determine whether we need to 1306 * create each type of bss section. We used to create these sections 1307 * if the sum of the required sizes for each type were non-zero. 1308 * However, it is possible for a compiler to generate COMMON variables 1309 * of zero-length and this tricks that logic --- even zero-length 1310 * symbols need an output section. 1311 */ 1312 need_bss = need_tlsbss = FALSE; 1313 #if defined(_ELF64) 1314 need_lbss = FALSE; 1315 #endif 1316 1317 /* 1318 * Determine how undefined symbols are handled: 1319 * 1320 * fatal: 1321 * If this link-edit calls for no undefined symbols to remain 1322 * (this is the default case when generating an executable but 1323 * can be enforced for any object using -z defs), a fatal error 1324 * condition will be indicated. 1325 * 1326 * warning: 1327 * If we're creating a shared object, and either the -Bsymbolic 1328 * flag is set, or the user has turned on the -z guidance feature, 1329 * then a non-fatal warning is issued for each symbol. 1330 * 1331 * ignore: 1332 * In all other cases, undefined symbols are quietly allowed. 1333 */ 1334 if (oflags & FLG_OF_NOUNDEF) { 1335 undef = FLG_OF_FATAL; 1336 } else if (oflags & FLG_OF_SHAROBJ) { 1337 if ((oflags & FLG_OF_SYMBOLIC) || 1338 OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS)) 1339 undef = FLG_OF_WARN; 1340 } 1341 1342 /* 1343 * If the symbol is referenced from an implicitly included shared object 1344 * (ie. it's not on the NEEDED list) then the symbol is also classified 1345 * as undefined and a fatal error condition will be indicated. 1346 */ 1347 if ((oflags & FLG_OF_NOUNDEF) || !(oflags & FLG_OF_SHAROBJ)) 1348 needed = FLG_OF_FATAL; 1349 else if ((oflags & FLG_OF_SHAROBJ) && 1350 OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS)) 1351 needed = FLG_OF_WARN; 1352 1353 /* 1354 * If the output image is being versioned, then all symbol definitions 1355 * must be associated with a version. Any symbol that isn't associated 1356 * with a version is classified as undefined, and a fatal error 1357 * condition is indicated. 1358 */ 1359 if ((oflags & FLG_OF_VERDEF) && (ofl->ofl_vercnt > VER_NDX_GLOBAL)) 1360 verdesc = FLG_OF_FATAL; 1361 1362 allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl); 1363 1364 if (allow_ldynsym) { 1365 /* 1366 * Normally, we disallow symbols with 0 size from appearing 1367 * in a dyn[sym|tls]sort section. However, there are some 1368 * symbols that serve special purposes that we want to exempt 1369 * from this rule. Look them up, and set their 1370 * FLG_SY_DYNSORT flag. 1371 */ 1372 static const char *special[] = { 1373 MSG_ORIG(MSG_SYM_INIT_U), /* _init */ 1374 MSG_ORIG(MSG_SYM_FINI_U), /* _fini */ 1375 MSG_ORIG(MSG_SYM_START), /* _start */ 1376 NULL 1377 }; 1378 int i; 1379 1380 for (i = 0; special[i] != NULL; i++) { 1381 if (((sdp = ld_sym_find(special[i], 1382 SYM_NOHASH, NULL, ofl)) != NULL) && 1383 (sdp->sd_sym->st_size == 0)) { 1384 if (ld_sym_copy(sdp) == S_ERROR) 1385 return (S_ERROR); 1386 sdp->sd_flags |= FLG_SY_DYNSORT; 1387 } 1388 } 1389 } 1390 1391 /* 1392 * Collect and validate the globals from the internal symbol table. 1393 */ 1394 for (sav = avl_first(&ofl->ofl_symavl); sav; 1395 sav = AVL_NEXT(&ofl->ofl_symavl, sav)) { 1396 Is_desc *isp; 1397 int undeferr = 0; 1398 uchar_t vis; 1399 1400 sdp = sav->sav_sdp; 1401 1402 /* 1403 * If undefined symbols are allowed, and we're not being 1404 * asked to supply guidance, ignore any symbols that are 1405 * not needed. 1406 */ 1407 if (!(oflags & FLG_OF_NOUNDEF) && 1408 !OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS) && 1409 (sdp->sd_ref == REF_DYN_SEEN)) 1410 continue; 1411 1412 /* 1413 * If the symbol originates from an external or parent mapfile 1414 * reference and hasn't been matched to a reference from a 1415 * relocatable object, ignore it. 1416 */ 1417 if ((sdp->sd_flags & (FLG_SY_EXTERN | FLG_SY_PARENT)) && 1418 ((sdp->sd_flags & FLG_SY_MAPUSED) == 0)) { 1419 sdp->sd_flags |= FLG_SY_INVALID; 1420 continue; 1421 } 1422 1423 sym = sdp->sd_sym; 1424 type = ELF_ST_TYPE(sym->st_info); 1425 1426 /* 1427 * Sanity check TLS. 1428 */ 1429 if ((type == STT_TLS) && (sym->st_size != 0) && 1430 (sym->st_shndx != SHN_UNDEF) && 1431 (sym->st_shndx != SHN_COMMON)) { 1432 Is_desc *isp = sdp->sd_isc; 1433 Ifl_desc *ifl = sdp->sd_file; 1434 1435 if ((isp == NULL) || (isp->is_shdr == NULL) || 1436 ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) { 1437 ld_eprintf(ofl, ERR_FATAL, 1438 MSG_INTL(MSG_SYM_TLS), 1439 demangle(sdp->sd_name), ifl->ifl_name); 1440 continue; 1441 } 1442 } 1443 1444 if ((sdp->sd_flags & FLG_SY_VISIBLE) == 0) 1445 ld_sym_adjust_vis(sdp, ofl); 1446 1447 if ((sdp->sd_flags & FLG_SY_REDUCED) && 1448 (oflags & FLG_OF_PROCRED)) { 1449 DBG_CALL(Dbg_syms_reduce(ofl, DBG_SYM_REDUCE_GLOBAL, 1450 sdp, 0, 0)); 1451 } 1452 1453 /* 1454 * Record any STV_SINGLETON existence. 1455 */ 1456 if ((vis = ELF_ST_VISIBILITY(sym->st_other)) == STV_SINGLETON) 1457 ofl->ofl_dtflags_1 |= DF_1_SINGLETON; 1458 1459 /* 1460 * If building a shared object or executable, and this is a 1461 * non-weak UNDEF symbol with reduced visibility (STV_*), then 1462 * give a fatal error. 1463 */ 1464 if (((oflags & FLG_OF_RELOBJ) == 0) && 1465 (sym->st_shndx == SHN_UNDEF) && 1466 (ELF_ST_BIND(sym->st_info) != STB_WEAK)) { 1467 if (vis && (vis != STV_SINGLETON)) { 1468 sym_undef_entry(ofl, sdp, BNDLOCAL, 1469 FLG_OF_FATAL, &undef_state); 1470 continue; 1471 } 1472 } 1473 1474 /* 1475 * If this symbol is defined in a non-allocatable section, 1476 * reduce it to local symbol. 1477 */ 1478 if (((isp = sdp->sd_isc) != 0) && isp->is_shdr && 1479 ((isp->is_shdr->sh_flags & SHF_ALLOC) == 0)) { 1480 sdp->sd_flags |= (FLG_SY_REDUCED | FLG_SY_HIDDEN); 1481 } 1482 1483 /* 1484 * If this symbol originated as a SHN_SUNW_IGNORE, it will have 1485 * been processed as an SHN_UNDEF. Return the symbol to its 1486 * original index for validation, and propagation to the output 1487 * file. 1488 */ 1489 if (sdp->sd_flags & FLG_SY_IGNORE) 1490 sdp->sd_shndx = SHN_SUNW_IGNORE; 1491 1492 if (undef) { 1493 /* 1494 * If a non-weak reference remains undefined, or if a 1495 * mapfile reference is not bound to the relocatable 1496 * objects that make up the object being built, we have 1497 * a fatal error. 1498 * 1499 * The exceptions are symbols which are defined to be 1500 * found in the parent (FLG_SY_PARENT), which is really 1501 * only meaningful for direct binding, or are defined 1502 * external (FLG_SY_EXTERN) so as to suppress -zdefs 1503 * errors. 1504 * 1505 * Register symbols are always allowed to be UNDEF. 1506 * 1507 * Note that we don't include references created via -u 1508 * in the same shared object binding test. This is for 1509 * backward compatibility, in that a number of archive 1510 * makefile rules used -u to cause archive extraction. 1511 * These same rules have been cut and pasted to apply 1512 * to shared objects, and thus although the -u reference 1513 * is redundant, flagging it as fatal could cause some 1514 * build to fail. Also we have documented the use of 1515 * -u as a mechanism to cause binding to weak version 1516 * definitions, thus giving users an error condition 1517 * would be incorrect. 1518 */ 1519 if (!(sdp->sd_flags & FLG_SY_REGSYM) && 1520 ((sym->st_shndx == SHN_UNDEF) && 1521 ((ELF_ST_BIND(sym->st_info) != STB_WEAK) && 1522 ((sdp->sd_flags & 1523 (FLG_SY_PARENT | FLG_SY_EXTERN)) == 0)) || 1524 ((sdp->sd_flags & 1525 (FLG_SY_MAPREF | FLG_SY_MAPUSED | FLG_SY_HIDDEN | 1526 FLG_SY_PROTECT)) == FLG_SY_MAPREF))) { 1527 sym_undef_entry(ofl, sdp, UNDEF, undef, 1528 &undef_state); 1529 undeferr = 1; 1530 } 1531 1532 } else { 1533 /* 1534 * For building things like shared objects (or anything 1535 * -znodefs), undefined symbols are allowed. 1536 * 1537 * If a mapfile reference remains undefined the user 1538 * would probably like a warning at least (they've 1539 * usually mis-spelt the reference). Refer to the above 1540 * comments for discussion on -u references, which 1541 * are not tested for in the same manner. 1542 */ 1543 if ((sdp->sd_flags & 1544 (FLG_SY_MAPREF | FLG_SY_MAPUSED)) == 1545 FLG_SY_MAPREF) { 1546 sym_undef_entry(ofl, sdp, UNDEF, FLG_OF_WARN, 1547 &undef_state); 1548 undeferr = 1; 1549 } 1550 } 1551 1552 /* 1553 * If this symbol comes from a dependency mark the dependency 1554 * as required (-z ignore can result in unused dependencies 1555 * being dropped). If we need to record dependency versioning 1556 * information indicate what version of the needed shared object 1557 * this symbol is part of. Flag the symbol as undefined if it 1558 * has not been made available to us. 1559 */ 1560 if ((sdp->sd_ref == REF_DYN_NEED) && 1561 (!(sdp->sd_flags & FLG_SY_REFRSD))) { 1562 sdp->sd_file->ifl_flags |= FLG_IF_DEPREQD; 1563 1564 /* 1565 * Capture that we've bound to a symbol that doesn't 1566 * allow being directly bound to. 1567 */ 1568 if (sdp->sd_flags & FLG_SY_NDIR) 1569 ofl->ofl_flags1 |= FLG_OF1_NGLBDIR; 1570 1571 if (sdp->sd_file->ifl_vercnt) { 1572 int vndx; 1573 Ver_index *vip; 1574 1575 vndx = sdp->sd_aux->sa_dverndx; 1576 vip = &sdp->sd_file->ifl_verndx[vndx]; 1577 if (vip->vi_flags & FLG_VER_AVAIL) { 1578 vip->vi_flags |= FLG_VER_REFER; 1579 } else { 1580 sym_undef_entry(ofl, sdp, NOTAVAIL, 1581 FLG_OF_FATAL, &undef_state); 1582 continue; 1583 } 1584 } 1585 } 1586 1587 /* 1588 * Test that we do not bind to symbol supplied from an implicit 1589 * shared object. If a binding is from a weak reference it can 1590 * be ignored. 1591 */ 1592 if (needed && !undeferr && (sdp->sd_flags & FLG_SY_GLOBREF) && 1593 (sdp->sd_ref == REF_DYN_NEED) && 1594 (sdp->sd_flags & FLG_SY_NOTAVAIL)) { 1595 sym_undef_entry(ofl, sdp, IMPLICIT, needed, 1596 &undef_state); 1597 if (needed == FLG_OF_FATAL) 1598 continue; 1599 } 1600 1601 /* 1602 * Test that a symbol isn't going to be reduced to local scope 1603 * which actually wants to bind to a shared object - if so it's 1604 * a fatal error. 1605 */ 1606 if ((sdp->sd_ref == REF_DYN_NEED) && 1607 (sdp->sd_flags & (FLG_SY_HIDDEN | FLG_SY_PROTECT))) { 1608 sym_undef_entry(ofl, sdp, BNDLOCAL, FLG_OF_FATAL, 1609 &undef_state); 1610 continue; 1611 } 1612 1613 /* 1614 * If the output image is to be versioned then all symbol 1615 * definitions must be associated with a version. Remove any 1616 * versioning that might be left associated with an undefined 1617 * symbol. 1618 */ 1619 if (verdesc && (sdp->sd_ref == REF_REL_NEED)) { 1620 if (sym->st_shndx == SHN_UNDEF) { 1621 if (sdp->sd_aux && sdp->sd_aux->sa_overndx) 1622 sdp->sd_aux->sa_overndx = 0; 1623 } else { 1624 if (!SYM_IS_HIDDEN(sdp) && sdp->sd_aux && 1625 (sdp->sd_aux->sa_overndx == 0)) { 1626 sym_undef_entry(ofl, sdp, NOVERSION, 1627 verdesc, &undef_state); 1628 continue; 1629 } 1630 } 1631 } 1632 1633 /* 1634 * If we don't need the symbol there's no need to process it 1635 * any further. 1636 */ 1637 if (sdp->sd_ref == REF_DYN_SEEN) 1638 continue; 1639 1640 /* 1641 * Calculate the size and alignment requirements for the global 1642 * .bss and .tls sections. If we're building a relocatable 1643 * object only account for scoped COMMON symbols (these will 1644 * be converted to .bss references). 1645 * 1646 * When -z nopartial is in effect, partially initialized 1647 * symbols are directed to the special .data section 1648 * created for that purpose (ofl->ofl_isparexpn). 1649 * Otherwise, partially initialized symbols go to .bss. 1650 * 1651 * Also refer to make_mvsections() in sunwmove.c 1652 */ 1653 if ((sym->st_shndx == SHN_COMMON) && 1654 (((oflags & FLG_OF_RELOBJ) == 0) || 1655 ld_sym_reducable(ofl, sdp))) { 1656 if ((sdp->sd_move == NULL) || 1657 ((sdp->sd_flags & FLG_SY_PAREXPN) == 0)) { 1658 if (type != STT_TLS) { 1659 need_bss = TRUE; 1660 bsssize = (Xword)S_ROUND(bsssize, 1661 sym->st_value) + sym->st_size; 1662 if (sym->st_value > bssalign) 1663 bssalign = sym->st_value; 1664 } else { 1665 need_tlsbss = TRUE; 1666 tlssize = (Xword)S_ROUND(tlssize, 1667 sym->st_value) + sym->st_size; 1668 if (sym->st_value > tlsalign) 1669 tlsalign = sym->st_value; 1670 } 1671 } 1672 } 1673 1674 #if defined(_ELF64) 1675 /* 1676 * Calculate the size and alignment requirement for the global 1677 * .lbss. TLS or partially initialized symbols do not need to be 1678 * considered yet. 1679 */ 1680 if ((ld_targ.t_m.m_mach == EM_AMD64) && 1681 (sym->st_shndx == SHN_X86_64_LCOMMON)) { 1682 need_lbss = TRUE; 1683 lbsssize = (Xword)S_ROUND(lbsssize, sym->st_value) + 1684 sym->st_size; 1685 if (sym->st_value > lbssalign) 1686 lbssalign = sym->st_value; 1687 } 1688 #endif 1689 /* 1690 * If a symbol was referenced via the command line 1691 * (ld -u <>, ...), then this counts as a reference against the 1692 * symbol. Mark any section that symbol is defined in. 1693 */ 1694 if (((isp = sdp->sd_isc) != 0) && 1695 (sdp->sd_flags & FLG_SY_CMDREF)) { 1696 isp->is_flags |= FLG_IS_SECTREF; 1697 isp->is_file->ifl_flags |= FLG_IF_FILEREF; 1698 } 1699 1700 /* 1701 * Update the symbol count and the associated name string size. 1702 * Note, a capabilities symbol must remain as visible as a 1703 * global symbol. However, the runtime linker recognizes the 1704 * hidden requirement and ensures the symbol isn't made globally 1705 * available at runtime. 1706 */ 1707 if (ld_sym_reducable(ofl, sdp)) { 1708 /* 1709 * If any reductions are being processed, keep a count 1710 * of eliminated symbols, and if the symbol is being 1711 * reduced to local, count it's size for the .symtab. 1712 */ 1713 if (sdp->sd_flags & FLG_SY_ELIM) { 1714 ofl->ofl_elimcnt++; 1715 } else { 1716 ofl->ofl_scopecnt++; 1717 if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) || 1718 sym->st_name) && (st_insert(ofl->ofl_strtab, 1719 sdp->sd_name) == -1)) 1720 return (S_ERROR); 1721 if (allow_ldynsym && ldynsym_symtype[type] && 1722 ((sym->st_name != 0) || 1723 (type == STT_FILE))) { 1724 ofl->ofl_dynscopecnt++; 1725 if (st_insert(ofl->ofl_dynstrtab, 1726 sdp->sd_name) == -1) 1727 return (S_ERROR); 1728 /* Include it in sort section? */ 1729 DYNSORT_COUNT(sdp, sym, type, ++); 1730 } 1731 } 1732 } else { 1733 ofl->ofl_globcnt++; 1734 1735 /* 1736 * Check to see if this global variable should go into 1737 * a sort section. Sort sections require a 1738 * .SUNW_ldynsym section, so, don't check unless a 1739 * .SUNW_ldynsym is allowed. 1740 */ 1741 if (allow_ldynsym) 1742 DYNSORT_COUNT(sdp, sym, type, ++); 1743 1744 /* 1745 * If global direct bindings are in effect, or this 1746 * symbol has bound to a dependency which was specified 1747 * as requiring direct bindings, and it hasn't 1748 * explicitly been defined as a non-direct binding 1749 * symbol, mark it. 1750 */ 1751 if (((ofl->ofl_dtflags_1 & DF_1_DIRECT) || (isp && 1752 (isp->is_file->ifl_flags & FLG_IF_DIRECT))) && 1753 ((sdp->sd_flags & FLG_SY_NDIR) == 0)) 1754 sdp->sd_flags |= FLG_SY_DIR; 1755 1756 /* 1757 * Insert the symbol name. 1758 */ 1759 if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) || 1760 sym->st_name) { 1761 if (st_insert(ofl->ofl_strtab, 1762 sdp->sd_name) == -1) 1763 return (S_ERROR); 1764 1765 if (!(ofl->ofl_flags & FLG_OF_RELOBJ) && 1766 (st_insert(ofl->ofl_dynstrtab, 1767 sdp->sd_name) == -1)) 1768 return (S_ERROR); 1769 } 1770 1771 /* 1772 * If this section offers a global symbol - record that 1773 * fact. 1774 */ 1775 if (isp) { 1776 isp->is_flags |= FLG_IS_SECTREF; 1777 isp->is_file->ifl_flags |= FLG_IF_FILEREF; 1778 } 1779 } 1780 } 1781 1782 /* 1783 * Guidance: Use -z defs|nodefs when building shared objects. 1784 * 1785 * Our caller issues this, unless we mask it out here. So we mask it 1786 * out unless we've issued at least one warnings or fatal error. 1787 */ 1788 if (!((oflags & FLG_OF_SHAROBJ) && OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS) && 1789 (undef_state & (FLG_OF_FATAL | FLG_OF_WARN)))) 1790 ofl->ofl_guideflags |= FLG_OFG_NO_DEFS; 1791 1792 /* 1793 * If we've encountered a fatal error during symbol validation then 1794 * return now. 1795 */ 1796 if (ofl->ofl_flags & FLG_OF_FATAL) 1797 return (1); 1798 1799 /* 1800 * Now that symbol resolution is completed, scan any register symbols. 1801 * From now on, we're only interested in those that contribute to the 1802 * output file. 1803 */ 1804 if (ofl->ofl_regsyms) { 1805 int ndx; 1806 1807 for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) { 1808 if ((sdp = ofl->ofl_regsyms[ndx]) == NULL) 1809 continue; 1810 if (sdp->sd_ref != REF_REL_NEED) { 1811 ofl->ofl_regsyms[ndx] = NULL; 1812 continue; 1813 } 1814 1815 ofl->ofl_regsymcnt++; 1816 if (sdp->sd_sym->st_name == 0) 1817 sdp->sd_name = MSG_ORIG(MSG_STR_EMPTY); 1818 1819 if (SYM_IS_HIDDEN(sdp) || 1820 (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL)) 1821 ofl->ofl_lregsymcnt++; 1822 } 1823 } 1824 1825 /* 1826 * Generate the .bss section now that we know its size and alignment. 1827 */ 1828 if (need_bss) { 1829 if (ld_make_bss(ofl, bsssize, bssalign, 1830 ld_targ.t_id.id_bss) == S_ERROR) 1831 return (S_ERROR); 1832 } 1833 if (need_tlsbss) { 1834 if (ld_make_bss(ofl, tlssize, tlsalign, 1835 ld_targ.t_id.id_tlsbss) == S_ERROR) 1836 return (S_ERROR); 1837 } 1838 #if defined(_ELF64) 1839 if ((ld_targ.t_m.m_mach == EM_AMD64) && 1840 need_lbss && !(oflags & FLG_OF_RELOBJ)) { 1841 if (ld_make_bss(ofl, lbsssize, lbssalign, 1842 ld_targ.t_id.id_lbss) == S_ERROR) 1843 return (S_ERROR); 1844 } 1845 #endif 1846 /* 1847 * Determine what entry point symbol we need, and if found save its 1848 * symbol descriptor so that we can update the ELF header entry with the 1849 * symbols value later (see update_oehdr). Make sure the symbol is 1850 * tagged to ensure its update in case -s is in effect. Use any -e 1851 * option first, or the default entry points `_start' and `main'. 1852 */ 1853 ret = 0; 1854 if (ofl->ofl_entry) { 1855 if ((sdp = ld_sym_find(ofl->ofl_entry, SYM_NOHASH, 1856 NULL, ofl)) == NULL) { 1857 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_NOENTRY), 1858 ofl->ofl_entry); 1859 ret++; 1860 } else if (ensure_sym_local(ofl, sdp, 1861 MSG_INTL(MSG_SYM_ENTRY)) != 0) { 1862 ret++; 1863 } else { 1864 ofl->ofl_entry = (void *)sdp; 1865 } 1866 } else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_START), 1867 SYM_NOHASH, NULL, ofl)) != NULL) && (ensure_sym_local(ofl, 1868 sdp, 0) == 0)) { 1869 ofl->ofl_entry = (void *)sdp; 1870 1871 } else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_MAIN), 1872 SYM_NOHASH, NULL, ofl)) != NULL) && (ensure_sym_local(ofl, 1873 sdp, 0) == 0)) { 1874 ofl->ofl_entry = (void *)sdp; 1875 } 1876 1877 /* 1878 * If ld -zdtrace=<sym> was given, then validate that the symbol is 1879 * defined within the current object being built. 1880 */ 1881 if ((sdp = ofl->ofl_dtracesym) != 0) 1882 ret += ensure_sym_local(ofl, sdp, MSG_ORIG(MSG_STR_DTRACE)); 1883 1884 /* 1885 * If any initarray, finiarray or preinitarray functions have been 1886 * requested, make sure they are defined within the current object 1887 * being built. 1888 */ 1889 if (ofl->ofl_initarray) { 1890 ret += ensure_array_local(ofl, ofl->ofl_initarray, 1891 MSG_ORIG(MSG_SYM_INITARRAY)); 1892 } 1893 if (ofl->ofl_finiarray) { 1894 ret += ensure_array_local(ofl, ofl->ofl_finiarray, 1895 MSG_ORIG(MSG_SYM_FINIARRAY)); 1896 } 1897 if (ofl->ofl_preiarray) { 1898 ret += ensure_array_local(ofl, ofl->ofl_preiarray, 1899 MSG_ORIG(MSG_SYM_PREINITARRAY)); 1900 } 1901 1902 if (ret) 1903 return (S_ERROR); 1904 1905 /* 1906 * If we're required to record any needed dependencies versioning 1907 * information calculate it now that all symbols have been validated. 1908 */ 1909 if ((oflags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED) 1910 return (ld_vers_check_need(ofl)); 1911 else 1912 return (1); 1913 } 1914 1915 /* 1916 * qsort(3c) comparison function. As an optimization for associating weak 1917 * symbols to their strong counterparts sort global symbols according to their 1918 * section index, address and binding. 1919 */ 1920 static int 1921 compare(const void *sdpp1, const void *sdpp2) 1922 { 1923 Sym_desc *sdp1 = *((Sym_desc **)sdpp1); 1924 Sym_desc *sdp2 = *((Sym_desc **)sdpp2); 1925 Sym *sym1, *sym2; 1926 uchar_t bind1, bind2; 1927 1928 /* 1929 * Symbol descriptors may be zero, move these to the front of the 1930 * sorted array. 1931 */ 1932 if (sdp1 == NULL) 1933 return (-1); 1934 if (sdp2 == NULL) 1935 return (1); 1936 1937 sym1 = sdp1->sd_sym; 1938 sym2 = sdp2->sd_sym; 1939 1940 /* 1941 * Compare the symbols section index. This is important when sorting 1942 * the symbol tables of relocatable objects. In this case, a symbols 1943 * value is the offset within the associated section, and thus many 1944 * symbols can have the same value, but are effectively different 1945 * addresses. 1946 */ 1947 if (sym1->st_shndx > sym2->st_shndx) 1948 return (1); 1949 if (sym1->st_shndx < sym2->st_shndx) 1950 return (-1); 1951 1952 /* 1953 * Compare the symbols value (address). 1954 */ 1955 if (sym1->st_value > sym2->st_value) 1956 return (1); 1957 if (sym1->st_value < sym2->st_value) 1958 return (-1); 1959 1960 bind1 = ELF_ST_BIND(sym1->st_info); 1961 bind2 = ELF_ST_BIND(sym2->st_info); 1962 1963 /* 1964 * If two symbols have the same address place the weak symbol before 1965 * any strong counterpart. 1966 */ 1967 if (bind1 > bind2) 1968 return (-1); 1969 if (bind1 < bind2) 1970 return (1); 1971 1972 return (0); 1973 } 1974 1975 /* 1976 * Issue a MSG_SYM_BADADDR error from ld_sym_process(). This error 1977 * is issued when a symbol address/size is not contained by the 1978 * target section. 1979 * 1980 * Such objects are at least partially corrupt, and the user would 1981 * be well advised to be skeptical of them, and to ask their compiler 1982 * supplier to fix the problem. However, a distinction needs to be 1983 * made between symbols that reference readonly text, and those that 1984 * access writable data. Other than throwing off profiling results, 1985 * the readonly section case is less serious. We have encountered 1986 * such objects in the field. In order to allow existing objects 1987 * to continue working, we issue a warning rather than a fatal error 1988 * if the symbol is against readonly text. Other cases are fatal. 1989 */ 1990 static void 1991 issue_badaddr_msg(Ifl_desc *ifl, Ofl_desc *ofl, Sym_desc *sdp, 1992 Sym *sym, Word shndx) 1993 { 1994 Error err; 1995 const char *msg; 1996 1997 if ((sdp->sd_isc->is_shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == 1998 SHF_ALLOC) { 1999 msg = MSG_INTL(MSG_SYM_BADADDR_ROTXT); 2000 err = ERR_WARNING; 2001 } else { 2002 msg = MSG_INTL(MSG_SYM_BADADDR); 2003 err = ERR_FATAL; 2004 } 2005 2006 ld_eprintf(ofl, err, msg, demangle(sdp->sd_name), 2007 ifl->ifl_name, shndx, sdp->sd_isc->is_name, 2008 EC_XWORD(sdp->sd_isc->is_shdr->sh_size), 2009 EC_XWORD(sym->st_value), EC_XWORD(sym->st_size)); 2010 } 2011 2012 /* 2013 * Global symbols that are candidates for translation to local capability 2014 * symbols under -z symbolcap, are maintained on a local symbol list. Once 2015 * all symbols of a file are processed, this list is traversed to cull any 2016 * unnecessary weak symbol aliases. 2017 */ 2018 typedef struct { 2019 Sym_desc *c_nsdp; /* new lead symbol */ 2020 Sym_desc *c_osdp; /* original symbol */ 2021 Cap_group *c_group; /* symbol capability group */ 2022 Word c_ndx; /* symbol index */ 2023 } Cap_pair; 2024 2025 /* 2026 * Process the symbol table for the specified input file. At this point all 2027 * input sections from this input file have been assigned an input section 2028 * descriptor which is saved in the `ifl_isdesc' array. 2029 * 2030 * - local symbols are saved (as is) if the input file is a relocatable 2031 * object 2032 * 2033 * - global symbols are added to the linkers internal symbol table if they 2034 * are not already present, otherwise a symbol resolution function is 2035 * called upon to resolve the conflict. 2036 */ 2037 uintptr_t 2038 ld_sym_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl) 2039 { 2040 /* 2041 * This macro tests the given symbol to see if it is out of 2042 * range relative to the section it references. 2043 * 2044 * entry: 2045 * - ifl is a relative object (ET_REL) 2046 * _sdp - Symbol descriptor 2047 * _sym - Symbol 2048 * _type - Symbol type 2049 * 2050 * The following are tested: 2051 * - Symbol length is non-zero 2052 * - Symbol type is a type that references code or data 2053 * - Referenced section is not 0 (indicates an UNDEF symbol) 2054 * and is not in the range of special values above SHN_LORESERVE 2055 * (excluding SHN_XINDEX, which is OK). 2056 * - We have a valid section header for the target section 2057 * 2058 * If the above are all true, and the symbol position is not 2059 * contained by the target section, this macro evaluates to 2060 * True (1). Otherwise, False(0). 2061 */ 2062 #define SYM_LOC_BADADDR(_sdp, _sym, _type) \ 2063 (_sym->st_size && dynsymsort_symtype[_type] && \ 2064 (_sym->st_shndx != SHN_UNDEF) && \ 2065 ((_sym->st_shndx < SHN_LORESERVE) || \ 2066 (_sym->st_shndx == SHN_XINDEX)) && \ 2067 _sdp->sd_isc && _sdp->sd_isc->is_shdr && \ 2068 ((_sym->st_value + _sym->st_size) > _sdp->sd_isc->is_shdr->sh_size)) 2069 2070 Conv_inv_buf_t inv_buf; 2071 Sym *sym = (Sym *)isc->is_indata->d_buf; 2072 Word *symshndx = NULL; 2073 Shdr *shdr = isc->is_shdr; 2074 Sym_desc *sdp; 2075 size_t strsize; 2076 char *strs; 2077 uchar_t type, bind; 2078 Word ndx, hash, local, total; 2079 uchar_t osabi = ifl->ifl_ehdr->e_ident[EI_OSABI]; 2080 Half mach = ifl->ifl_ehdr->e_machine; 2081 Half etype = ifl->ifl_ehdr->e_type; 2082 const char *symsecname, *strsecname; 2083 Word symsecndx; 2084 avl_index_t where; 2085 int test_gnu_hidden_bit, weak; 2086 Cap_desc *cdp = NULL; 2087 Alist *cappairs = NULL; 2088 2089 /* 2090 * Its possible that a file may contain more that one symbol table, 2091 * ie. .dynsym and .symtab in a shared library. Only process the first 2092 * table (here, we assume .dynsym comes before .symtab). 2093 */ 2094 if (ifl->ifl_symscnt) 2095 return (1); 2096 2097 if (isc->is_symshndx) 2098 symshndx = isc->is_symshndx->is_indata->d_buf; 2099 2100 DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl)); 2101 2102 symsecndx = isc->is_scnndx; 2103 if (isc->is_name) 2104 symsecname = isc->is_name; 2105 else 2106 symsecname = MSG_ORIG(MSG_STR_EMPTY); 2107 2108 /* 2109 * From the symbol tables section header information determine which 2110 * strtab table is needed to locate the actual symbol names. 2111 */ 2112 if (ifl->ifl_flags & FLG_IF_HSTRTAB) { 2113 ndx = shdr->sh_link; 2114 if ((ndx == 0) || (ndx >= ifl->ifl_shnum)) { 2115 ld_eprintf(ofl, ERR_FATAL, 2116 MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name, 2117 EC_WORD(symsecndx), symsecname, EC_XWORD(ndx)); 2118 return (S_ERROR); 2119 } 2120 strsize = ifl->ifl_isdesc[ndx]->is_shdr->sh_size; 2121 strs = ifl->ifl_isdesc[ndx]->is_indata->d_buf; 2122 if (ifl->ifl_isdesc[ndx]->is_name) 2123 strsecname = ifl->ifl_isdesc[ndx]->is_name; 2124 else 2125 strsecname = MSG_ORIG(MSG_STR_EMPTY); 2126 } else { 2127 /* 2128 * There is no string table section in this input file 2129 * although there are symbols in this symbol table section. 2130 * This means that these symbols do not have names. 2131 * Currently, only scratch register symbols are allowed 2132 * not to have names. 2133 */ 2134 strsize = 0; 2135 strs = (char *)MSG_ORIG(MSG_STR_EMPTY); 2136 strsecname = MSG_ORIG(MSG_STR_EMPTY); 2137 } 2138 2139 /* 2140 * Determine the number of local symbols together with the total 2141 * number we have to process. 2142 */ 2143 total = (Word)(shdr->sh_size / shdr->sh_entsize); 2144 local = shdr->sh_info; 2145 2146 /* 2147 * Allocate a symbol table index array and a local symbol array 2148 * (global symbols are processed and added to the ofl->ofl_symbkt[] 2149 * array). If we are dealing with a relocatable object, allocate the 2150 * local symbol descriptors. If this isn't a relocatable object we 2151 * still have to process any shared object locals to determine if any 2152 * register symbols exist. Although these aren't added to the output 2153 * image, they are used as part of symbol resolution. 2154 */ 2155 if ((ifl->ifl_oldndx = libld_malloc((size_t)(total * 2156 sizeof (Sym_desc *)))) == NULL) 2157 return (S_ERROR); 2158 if ((etype == ET_REL) && (local != 0)) { 2159 if ((ifl->ifl_locs = 2160 libld_calloc(sizeof (Sym_desc), local)) == NULL) 2161 return (S_ERROR); 2162 /* LINTED */ 2163 ifl->ifl_locscnt = local; 2164 } 2165 ifl->ifl_symscnt = total; 2166 2167 /* 2168 * If there are local symbols to save add them to the symbol table 2169 * index array. 2170 */ 2171 if (local != 0) { 2172 int allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl); 2173 Sym_desc *last_file_sdp = NULL; 2174 int last_file_ndx = 0; 2175 2176 for (sym++, ndx = 1; ndx < local; sym++, ndx++) { 2177 sd_flag_t sdflags = FLG_SY_CLEAN; 2178 Word shndx; 2179 const char *name; 2180 Sym_desc *rsdp; 2181 int shndx_bad = 0; 2182 int symtab_enter = 1; 2183 2184 /* 2185 * Determine and validate the associated section index. 2186 */ 2187 if (symshndx && (sym->st_shndx == SHN_XINDEX)) { 2188 shndx = symshndx[ndx]; 2189 } else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) { 2190 sdflags |= FLG_SY_SPECSEC; 2191 } else if (shndx > ifl->ifl_shnum) { 2192 /* We need the name before we can issue error */ 2193 shndx_bad = 1; 2194 } 2195 2196 /* 2197 * Check if st_name has a valid value or not. 2198 */ 2199 if ((name = string(ofl, ifl, sym, strs, strsize, ndx, 2200 shndx, symsecndx, symsecname, strsecname, 2201 &sdflags)) == NULL) 2202 continue; 2203 2204 /* 2205 * Now that we have the name, if the section index 2206 * was bad, report it. 2207 */ 2208 if (shndx_bad) { 2209 ld_eprintf(ofl, ERR_WARNING, 2210 MSG_INTL(MSG_SYM_INVSHNDX), 2211 demangle_symname(name, symsecname, ndx), 2212 ifl->ifl_name, 2213 conv_sym_shndx(osabi, mach, sym->st_shndx, 2214 CONV_FMT_DECIMAL, &inv_buf)); 2215 continue; 2216 } 2217 2218 /* 2219 * If this local symbol table originates from a shared 2220 * object, then we're only interested in recording 2221 * register symbols. As local symbol descriptors aren't 2222 * allocated for shared objects, one will be allocated 2223 * to associated with the register symbol. This symbol 2224 * won't become part of the output image, but we must 2225 * process it to test for register conflicts. 2226 */ 2227 rsdp = sdp = NULL; 2228 if (sdflags & FLG_SY_REGSYM) { 2229 /* 2230 * The presence of FLG_SY_REGSYM means that 2231 * the pointers in ld_targ.t_ms are non-NULL. 2232 */ 2233 rsdp = (*ld_targ.t_ms.ms_reg_find)(sym, ofl); 2234 if (rsdp != 0) { 2235 /* 2236 * The fact that another register def- 2237 * inition has been found is fatal. 2238 * Call the verification routine to get 2239 * the error message and move on. 2240 */ 2241 (void) (*ld_targ.t_ms.ms_reg_check) 2242 (rsdp, sym, name, ifl, ofl); 2243 continue; 2244 } 2245 2246 if (etype == ET_DYN) { 2247 if ((sdp = libld_calloc( 2248 sizeof (Sym_desc), 1)) == NULL) 2249 return (S_ERROR); 2250 sdp->sd_ref = REF_DYN_SEEN; 2251 2252 /* Will not appear in output object */ 2253 symtab_enter = 0; 2254 } 2255 } else if (etype == ET_DYN) { 2256 continue; 2257 } 2258 2259 /* 2260 * Fill in the remaining symbol descriptor information. 2261 */ 2262 if (sdp == NULL) { 2263 sdp = &(ifl->ifl_locs[ndx]); 2264 sdp->sd_ref = REF_REL_NEED; 2265 sdp->sd_symndx = ndx; 2266 } 2267 if (rsdp == NULL) { 2268 sdp->sd_name = name; 2269 sdp->sd_sym = sym; 2270 sdp->sd_shndx = shndx; 2271 sdp->sd_flags = sdflags; 2272 sdp->sd_file = ifl; 2273 ifl->ifl_oldndx[ndx] = sdp; 2274 } 2275 2276 DBG_CALL(Dbg_syms_entry(ofl->ofl_lml, ndx, sdp)); 2277 2278 /* 2279 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF 2280 * so as to simplify future processing. 2281 */ 2282 if (sym->st_shndx == SHN_SUNW_IGNORE) { 2283 sdp->sd_shndx = shndx = SHN_UNDEF; 2284 sdp->sd_flags |= (FLG_SY_IGNORE | FLG_SY_ELIM); 2285 } 2286 2287 /* 2288 * Process any register symbols. 2289 */ 2290 if (sdp->sd_flags & FLG_SY_REGSYM) { 2291 /* 2292 * Add a diagnostic to indicate we've caught a 2293 * register symbol, as this can be useful if a 2294 * register conflict is later discovered. 2295 */ 2296 DBG_CALL(Dbg_syms_entered(ofl, sym, sdp)); 2297 2298 /* 2299 * If this register symbol hasn't already been 2300 * recorded, enter it now. 2301 * 2302 * The presence of FLG_SY_REGSYM means that 2303 * the pointers in ld_targ.t_ms are non-NULL. 2304 */ 2305 if ((rsdp == NULL) && 2306 ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) == 2307 0)) 2308 return (S_ERROR); 2309 } 2310 2311 /* 2312 * Assign an input section. 2313 */ 2314 if ((sym->st_shndx != SHN_UNDEF) && 2315 ((sdp->sd_flags & FLG_SY_SPECSEC) == 0)) 2316 sdp->sd_isc = ifl->ifl_isdesc[shndx]; 2317 2318 /* 2319 * If this symbol falls within the range of a section 2320 * being discarded, then discard the symbol itself. 2321 * There is no reason to keep this local symbol. 2322 */ 2323 if (sdp->sd_isc && 2324 (sdp->sd_isc->is_flags & FLG_IS_DISCARD)) { 2325 sdp->sd_flags |= FLG_SY_ISDISC; 2326 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp)); 2327 continue; 2328 } 2329 2330 /* 2331 * Skip any section symbols as new versions of these 2332 * will be created. 2333 */ 2334 if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION) { 2335 if (sym->st_shndx == SHN_UNDEF) { 2336 ld_eprintf(ofl, ERR_WARNING, 2337 MSG_INTL(MSG_SYM_INVSHNDX), 2338 demangle_symname(name, symsecname, 2339 ndx), ifl->ifl_name, 2340 conv_sym_shndx(osabi, mach, 2341 sym->st_shndx, CONV_FMT_DECIMAL, 2342 &inv_buf)); 2343 } 2344 continue; 2345 } 2346 2347 /* 2348 * For a relocatable object, if this symbol is defined 2349 * and has non-zero length and references an address 2350 * within an associated section, then check its extents 2351 * to make sure the section boundaries encompass it. 2352 * If they don't, the ELF file is corrupt. 2353 */ 2354 if (etype == ET_REL) { 2355 if (SYM_LOC_BADADDR(sdp, sym, type)) { 2356 issue_badaddr_msg(ifl, ofl, sdp, 2357 sym, shndx); 2358 if (ofl->ofl_flags & FLG_OF_FATAL) 2359 continue; 2360 } 2361 2362 /* 2363 * We have observed relocatable objects 2364 * containing identical adjacent STT_FILE 2365 * symbols. Discard any other than the first, 2366 * as they are all equivalent and the extras 2367 * do not add information. 2368 * 2369 * For the purpose of this test, we assume 2370 * that only the symbol type and the string 2371 * table offset (st_name) matter. 2372 */ 2373 if (type == STT_FILE) { 2374 int toss = (last_file_sdp != NULL) && 2375 ((ndx - 1) == last_file_ndx) && 2376 (sym->st_name == 2377 last_file_sdp->sd_sym->st_name); 2378 2379 last_file_sdp = sdp; 2380 last_file_ndx = ndx; 2381 if (toss) { 2382 sdp->sd_flags |= FLG_SY_INVALID; 2383 DBG_CALL(Dbg_syms_dup_discarded( 2384 ofl->ofl_lml, ndx, sdp)); 2385 continue; 2386 } 2387 } 2388 } 2389 2390 /* 2391 * If this symbol comes from a relocatable object and 2392 * looks like a GCC local function alias, don't 2393 * include it in dynsort sections, since the global 2394 * name will always be preferable. 2395 */ 2396 if ((etype == ET_REL) && is_gcc_localalias(sdp)) 2397 sdp->sd_flags |= FLG_SY_NODYNSORT; 2398 2399 /* 2400 * Sanity check for TLS 2401 */ 2402 if ((sym->st_size != 0) && ((type == STT_TLS) && 2403 (sym->st_shndx != SHN_COMMON))) { 2404 Is_desc *isp = sdp->sd_isc; 2405 2406 if ((isp == NULL) || (isp->is_shdr == NULL) || 2407 ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) { 2408 ld_eprintf(ofl, ERR_FATAL, 2409 MSG_INTL(MSG_SYM_TLS), 2410 demangle(sdp->sd_name), 2411 ifl->ifl_name); 2412 continue; 2413 } 2414 } 2415 2416 /* 2417 * Carry our some basic sanity checks (these are just 2418 * some of the erroneous symbol entries we've come 2419 * across, there's probably a lot more). The symbol 2420 * will not be carried forward to the output file, which 2421 * won't be a problem unless a relocation is required 2422 * against it. 2423 */ 2424 if (((sdp->sd_flags & FLG_SY_SPECSEC) && 2425 ((sym->st_shndx == SHN_COMMON)) || 2426 ((type == STT_FILE) && 2427 (sym->st_shndx != SHN_ABS))) || 2428 (sdp->sd_isc && (sdp->sd_isc->is_osdesc == NULL))) { 2429 ld_eprintf(ofl, ERR_WARNING, 2430 MSG_INTL(MSG_SYM_INVSHNDX), 2431 demangle_symname(name, symsecname, ndx), 2432 ifl->ifl_name, 2433 conv_sym_shndx(osabi, mach, sym->st_shndx, 2434 CONV_FMT_DECIMAL, &inv_buf)); 2435 sdp->sd_isc = NULL; 2436 sdp->sd_flags |= FLG_SY_INVALID; 2437 continue; 2438 } 2439 2440 /* 2441 * As these local symbols will become part of the output 2442 * image, record their number and name string size. 2443 * Globals are counted after all input file processing 2444 * (and hence symbol resolution) is complete during 2445 * sym_validate(). 2446 */ 2447 if (!(ofl->ofl_flags & FLG_OF_REDLSYM) && 2448 symtab_enter) { 2449 ofl->ofl_locscnt++; 2450 2451 if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) || 2452 sym->st_name) && (st_insert(ofl->ofl_strtab, 2453 sdp->sd_name) == -1)) 2454 return (S_ERROR); 2455 2456 /* 2457 * STT_FILE symbols must always remain, to 2458 * maintain the ordering semantics of symbol 2459 * tables. 2460 */ 2461 if (allow_ldynsym && ldynsym_symtype[type] && 2462 ((sym->st_name != 0) || 2463 (type == STT_FILE))) { 2464 ofl->ofl_dynlocscnt++; 2465 if (st_insert(ofl->ofl_dynstrtab, 2466 sdp->sd_name) == -1) 2467 return (S_ERROR); 2468 /* Include it in sort section? */ 2469 DYNSORT_COUNT(sdp, sym, type, ++); 2470 } 2471 } 2472 } 2473 } 2474 2475 /* 2476 * The GNU ld interprets the top bit of the 16-bit Versym value 2477 * (0x8000) as the "hidden" bit. If this bit is set, the linker 2478 * is supposed to act as if that symbol does not exist. The Solaris 2479 * linker does not support this mechanism, or the model of interface 2480 * evolution that it allows, but we honor it in GNU ld produced 2481 * objects in order to interoperate with them. 2482 * 2483 * Determine if we should honor the GNU hidden bit for this file. 2484 */ 2485 test_gnu_hidden_bit = ((ifl->ifl_flags & FLG_IF_GNUVER) != 0) && 2486 (ifl->ifl_versym != NULL); 2487 2488 /* 2489 * Determine whether object capabilities for this file are being 2490 * converted into symbol capabilities. If so, global function symbols, 2491 * and initialized global data symbols, need special translation and 2492 * processing. 2493 */ 2494 if ((etype == ET_REL) && (ifl->ifl_flags & FLG_IF_OTOSCAP)) 2495 cdp = ifl->ifl_caps; 2496 2497 /* 2498 * Now scan the global symbols entering them in the internal symbol 2499 * table or resolving them as necessary. 2500 */ 2501 sym = (Sym *)isc->is_indata->d_buf; 2502 sym += local; 2503 weak = 0; 2504 /* LINTED */ 2505 for (ndx = (int)local; ndx < total; sym++, ndx++) { 2506 const char *name; 2507 sd_flag_t sdflags = 0; 2508 Word shndx; 2509 int shndx_bad = 0; 2510 Sym *nsym = sym; 2511 Cap_pair *cpp = NULL; 2512 uchar_t ntype; 2513 2514 /* 2515 * Determine and validate the associated section index. 2516 */ 2517 if (symshndx && (nsym->st_shndx == SHN_XINDEX)) { 2518 shndx = symshndx[ndx]; 2519 } else if ((shndx = nsym->st_shndx) >= SHN_LORESERVE) { 2520 sdflags |= FLG_SY_SPECSEC; 2521 } else if (shndx > ifl->ifl_shnum) { 2522 /* We need the name before we can issue error */ 2523 shndx_bad = 1; 2524 } 2525 2526 /* 2527 * Check if st_name has a valid value or not. 2528 */ 2529 if ((name = string(ofl, ifl, nsym, strs, strsize, ndx, shndx, 2530 symsecndx, symsecname, strsecname, &sdflags)) == NULL) 2531 continue; 2532 2533 /* 2534 * Now that we have the name, report an erroneous section index. 2535 */ 2536 if (shndx_bad) { 2537 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_INVSHNDX), 2538 demangle_symname(name, symsecname, ndx), 2539 ifl->ifl_name, 2540 conv_sym_shndx(osabi, mach, nsym->st_shndx, 2541 CONV_FMT_DECIMAL, &inv_buf)); 2542 continue; 2543 } 2544 2545 /* 2546 * Test for the GNU hidden bit, and ignore symbols that 2547 * have it set. 2548 */ 2549 if (test_gnu_hidden_bit && 2550 ((ifl->ifl_versym[ndx] & 0x8000) != 0)) 2551 continue; 2552 2553 /* 2554 * The linker itself will generate symbols for _end, _etext, 2555 * _edata, _DYNAMIC and _PROCEDURE_LINKAGE_TABLE_, so don't 2556 * bother entering these symbols from shared objects. This 2557 * results in some wasted resolution processing, which is hard 2558 * to feel, but if nothing else, pollutes diagnostic relocation 2559 * output. 2560 */ 2561 if (name[0] && (etype == ET_DYN) && (nsym->st_size == 0) && 2562 (ELF_ST_TYPE(nsym->st_info) == STT_OBJECT) && 2563 (name[0] == '_') && ((name[1] == 'e') || 2564 (name[1] == 'D') || (name[1] == 'P')) && 2565 ((strcmp(name, MSG_ORIG(MSG_SYM_ETEXT_U)) == 0) || 2566 (strcmp(name, MSG_ORIG(MSG_SYM_EDATA_U)) == 0) || 2567 (strcmp(name, MSG_ORIG(MSG_SYM_END_U)) == 0) || 2568 (strcmp(name, MSG_ORIG(MSG_SYM_DYNAMIC_U)) == 0) || 2569 (strcmp(name, MSG_ORIG(MSG_SYM_PLKTBL_U)) == 0))) { 2570 ifl->ifl_oldndx[ndx] = 0; 2571 continue; 2572 } 2573 2574 /* 2575 * The '-z wrap=XXX' option emulates the GNU ld --wrap=XXX 2576 * option. When XXX is the symbol to be wrapped: 2577 * 2578 * - An undefined reference to XXX is converted to __wrap_XXX 2579 * - An undefined reference to __real_XXX is converted to XXX 2580 * 2581 * The idea is that the user can supply a wrapper function 2582 * __wrap_XXX that does some work, and then uses the name 2583 * __real_XXX to pass the call on to the real function. The 2584 * wrapper objects are linked with the original unmodified 2585 * objects to produce a wrapped version of the output object. 2586 */ 2587 if (ofl->ofl_wrap && name[0] && (shndx == SHN_UNDEF)) { 2588 WrapSymNode wsn, *wsnp; 2589 2590 /* 2591 * If this is the __real_XXX form, advance the 2592 * pointer to reference the wrapped name. 2593 */ 2594 wsn.wsn_name = name; 2595 if ((*name == '_') && 2596 (strncmp(name, MSG_ORIG(MSG_STR_UU_REAL_U), 2597 MSG_STR_UU_REAL_U_SIZE) == 0)) 2598 wsn.wsn_name += MSG_STR_UU_REAL_U_SIZE; 2599 2600 /* 2601 * Is this symbol in the wrap AVL tree? If so, map 2602 * XXX to __wrap_XXX, and __real_XXX to XXX. Note that 2603 * wsn.wsn_name will equal the current value of name 2604 * if the __real_ prefix is not present. 2605 */ 2606 if ((wsnp = avl_find(ofl->ofl_wrap, &wsn, 0)) != NULL) { 2607 const char *old_name = name; 2608 2609 name = (wsn.wsn_name == name) ? 2610 wsnp->wsn_wrapname : wsn.wsn_name; 2611 DBG_CALL(Dbg_syms_wrap(ofl->ofl_lml, ndx, 2612 old_name, name)); 2613 } 2614 } 2615 2616 /* 2617 * Determine and validate the symbols binding. 2618 */ 2619 bind = ELF_ST_BIND(nsym->st_info); 2620 if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) { 2621 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_NONGLOB), 2622 demangle_symname(name, symsecname, ndx), 2623 ifl->ifl_name, 2624 conv_sym_info_bind(bind, 0, &inv_buf)); 2625 continue; 2626 } 2627 if (bind == STB_WEAK) 2628 weak++; 2629 2630 /* 2631 * If this symbol falls within the range of a section being 2632 * discarded, then discard the symbol itself. 2633 */ 2634 if (((sdflags & FLG_SY_SPECSEC) == 0) && 2635 (nsym->st_shndx != SHN_UNDEF)) { 2636 Is_desc *isp; 2637 2638 if (shndx >= ifl->ifl_shnum) { 2639 /* 2640 * Carry out some basic sanity checks. 2641 * The symbol will not be carried forward to 2642 * the output file, which won't be a problem 2643 * unless a relocation is required against it. 2644 */ 2645 ld_eprintf(ofl, ERR_WARNING, 2646 MSG_INTL(MSG_SYM_INVSHNDX), 2647 demangle_symname(name, symsecname, ndx), 2648 ifl->ifl_name, 2649 conv_sym_shndx(osabi, mach, nsym->st_shndx, 2650 CONV_FMT_DECIMAL, &inv_buf)); 2651 continue; 2652 } 2653 2654 isp = ifl->ifl_isdesc[shndx]; 2655 if ((isp != NULL) && 2656 (isp->is_flags & FLG_IS_DISCARD) && 2657 (isp->is_flags & FLG_IS_COMDAT)) { 2658 Sym *rsym; 2659 2660 /* 2661 * Replace the original symbol definition with 2662 * a symbol reference, so that resolution can 2663 * occur. 2664 * 2665 * It is common due to compiler issues or 2666 * build system intricacy for COMDAT groups to 2667 * refer to symbols of different visibility, 2668 * even though this is erroneous. By 2669 * replacing the discarded members of these 2670 * groups with symbol references we allow 2671 * symbol resolution to occur and the most 2672 * restrictive visibility to be chosen. 2673 */ 2674 if ((rsym = libld_malloc(sizeof (Sym))) == NULL) 2675 return (S_ERROR); 2676 2677 *rsym = *nsym; 2678 rsym->st_shndx = shndx = SHN_UNDEF; 2679 rsym->st_value = 0x0; 2680 rsym->st_size = 0x0; 2681 2682 nsym = rsym; 2683 2684 sdflags |= FLG_SY_ISDISC; 2685 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp)); 2686 } else if ((isp != NULL) && 2687 (isp->is_flags & FLG_IS_DISCARD)) { 2688 /* Discarded but not via COMDAT */ 2689 if ((sdp = 2690 libld_calloc(sizeof (Sym_desc), 1)) == NULL) 2691 return (S_ERROR); 2692 2693 /* 2694 * Create a dummy symbol entry so that if we 2695 * find any references to this discarded symbol 2696 * we can compensate. 2697 */ 2698 sdp->sd_name = name; 2699 sdp->sd_sym = nsym; 2700 sdp->sd_file = ifl; 2701 sdp->sd_isc = isp; 2702 sdp->sd_flags = FLG_SY_ISDISC; 2703 ifl->ifl_oldndx[ndx] = sdp; 2704 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp)); 2705 continue; 2706 } 2707 } 2708 2709 /* 2710 * If object capabilities for this file are being converted 2711 * into symbol capabilities, then: 2712 * 2713 * - Any global function, or initialized global data symbol 2714 * definitions (ie., those that are not associated with 2715 * special symbol types, ie., ABS, COMMON, etc.), and which 2716 * have not been reduced to locals, are converted to symbol 2717 * references (UNDEF). This ensures that any reference to 2718 * the original symbol, for example from a relocation, get 2719 * associated to a capabilities family lead symbol, ie., a 2720 * generic instance. 2721 * 2722 * - For each global function, or object symbol definition, 2723 * a new local symbol is created. The function or object 2724 * is renamed using the capabilities CA_SUNW_ID definition 2725 * (which might have been fabricated for this purpose - 2726 * see get_cap_group()). The new symbol name is: 2727 * 2728 * <original name>%<capability group identifier> 2729 * 2730 * This symbol is associated to the same location, and 2731 * becomes a capabilities family member. 2732 */ 2733 /* LINTED */ 2734 hash = (Word)elf_hash(name); 2735 2736 ntype = ELF_ST_TYPE(nsym->st_info); 2737 if (cdp && (nsym->st_shndx != SHN_UNDEF) && 2738 ((sdflags & FLG_SY_SPECSEC) == 0) && 2739 ((ntype == STT_FUNC) || (ntype == STT_OBJECT))) { 2740 /* 2741 * Determine this symbol's visibility. If a mapfile has 2742 * indicated this symbol should be local, then there's 2743 * no point in transforming this global symbol to a 2744 * capabilities symbol. Otherwise, create a symbol 2745 * capability pair descriptor to record this symbol as 2746 * a candidate for translation. 2747 */ 2748 if (sym_cap_vis(name, hash, sym, ofl) && 2749 ((cpp = alist_append(&cappairs, NULL, 2750 sizeof (Cap_pair), AL_CNT_CAP_PAIRS)) == NULL)) 2751 return (S_ERROR); 2752 } 2753 2754 if (cpp) { 2755 Sym *rsym; 2756 2757 DBG_CALL(Dbg_syms_cap_convert(ofl, ndx, name, nsym)); 2758 2759 /* 2760 * Allocate a new symbol descriptor to represent the 2761 * transformed global symbol. The descriptor points 2762 * to the original symbol information (which might 2763 * indicate a global or weak visibility). The symbol 2764 * information will be transformed into a local symbol 2765 * later, after any weak aliases are culled. 2766 */ 2767 if ((cpp->c_osdp = 2768 libld_malloc(sizeof (Sym_desc))) == NULL) 2769 return (S_ERROR); 2770 2771 cpp->c_osdp->sd_name = name; 2772 cpp->c_osdp->sd_sym = nsym; 2773 cpp->c_osdp->sd_shndx = shndx; 2774 cpp->c_osdp->sd_file = ifl; 2775 cpp->c_osdp->sd_isc = ifl->ifl_isdesc[shndx]; 2776 cpp->c_osdp->sd_ref = REF_REL_NEED; 2777 2778 /* 2779 * Save the capabilities group this symbol belongs to, 2780 * and the original symbol index. 2781 */ 2782 cpp->c_group = cdp->ca_groups->apl_data[0]; 2783 cpp->c_ndx = ndx; 2784 2785 /* 2786 * Replace the original symbol definition with a symbol 2787 * reference. Make sure this reference isn't left as a 2788 * weak. 2789 */ 2790 if ((rsym = libld_malloc(sizeof (Sym))) == NULL) 2791 return (S_ERROR); 2792 2793 *rsym = *nsym; 2794 2795 rsym->st_info = ELF_ST_INFO(STB_GLOBAL, ntype); 2796 rsym->st_shndx = shndx = SHN_UNDEF; 2797 rsym->st_value = 0; 2798 rsym->st_size = 0; 2799 2800 sdflags |= FLG_SY_CAP; 2801 2802 nsym = rsym; 2803 } 2804 2805 /* 2806 * If the symbol does not already exist in the internal symbol 2807 * table add it, otherwise resolve the conflict. If the symbol 2808 * from this file is kept, retain its symbol table index for 2809 * possible use in associating a global alias. 2810 */ 2811 if ((sdp = ld_sym_find(name, hash, &where, ofl)) == NULL) { 2812 DBG_CALL(Dbg_syms_global(ofl->ofl_lml, ndx, name)); 2813 if ((sdp = ld_sym_enter(name, nsym, hash, ifl, ofl, ndx, 2814 shndx, sdflags, &where)) == (Sym_desc *)S_ERROR) 2815 return (S_ERROR); 2816 } else if (ld_sym_resolve(sdp, nsym, ifl, ofl, ndx, shndx, 2817 sdflags) == S_ERROR) { 2818 return (S_ERROR); 2819 } 2820 2821 /* 2822 * Now that we have a symbol descriptor, retain the descriptor 2823 * for later use by symbol capabilities processing. 2824 */ 2825 if (cpp) 2826 cpp->c_nsdp = sdp; 2827 2828 /* 2829 * After we've compared a defined symbol in one shared 2830 * object, flag the symbol so we don't compare it again. 2831 */ 2832 if ((etype == ET_DYN) && (nsym->st_shndx != SHN_UNDEF) && 2833 ((sdp->sd_flags & FLG_SY_SOFOUND) == 0)) 2834 sdp->sd_flags |= FLG_SY_SOFOUND; 2835 2836 /* 2837 * If the symbol is accepted from this file retain the symbol 2838 * index for possible use in aliasing. 2839 */ 2840 if (sdp->sd_file == ifl) 2841 sdp->sd_symndx = ndx; 2842 2843 ifl->ifl_oldndx[ndx] = sdp; 2844 2845 /* 2846 * If we've accepted a register symbol, continue to validate 2847 * it. 2848 */ 2849 if (sdp->sd_flags & FLG_SY_REGSYM) { 2850 Sym_desc *rsdp; 2851 2852 /* 2853 * The presence of FLG_SY_REGSYM means that 2854 * the pointers in ld_targ.t_ms are non-NULL. 2855 */ 2856 rsdp = (*ld_targ.t_ms.ms_reg_find)(sdp->sd_sym, ofl); 2857 if (rsdp == NULL) { 2858 if ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) == 0) 2859 return (S_ERROR); 2860 } else if (rsdp != sdp) { 2861 (void) (*ld_targ.t_ms.ms_reg_check)(rsdp, 2862 sdp->sd_sym, sdp->sd_name, ifl, ofl); 2863 } 2864 } 2865 2866 /* 2867 * For a relocatable object, if this symbol is defined 2868 * and has non-zero length and references an address 2869 * within an associated section, then check its extents 2870 * to make sure the section boundaries encompass it. 2871 * If they don't, the ELF file is corrupt. Note that this 2872 * global symbol may have come from another file to satisfy 2873 * an UNDEF symbol of the same name from this one. In that 2874 * case, we don't check it, because it was already checked 2875 * as part of its own file. 2876 */ 2877 if ((etype == ET_REL) && (sdp->sd_file == ifl)) { 2878 Sym *tsym = sdp->sd_sym; 2879 2880 if (SYM_LOC_BADADDR(sdp, tsym, 2881 ELF_ST_TYPE(tsym->st_info))) { 2882 issue_badaddr_msg(ifl, ofl, sdp, 2883 tsym, tsym->st_shndx); 2884 continue; 2885 } 2886 } 2887 } 2888 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD)); 2889 2890 /* 2891 * Associate weak (alias) symbols to their non-weak counterparts by 2892 * scanning the global symbols one more time. 2893 * 2894 * This association is needed when processing the symbols from a shared 2895 * object dependency when a a weak definition satisfies a reference: 2896 * 2897 * - When building a dynamic executable, if a referenced symbol is a 2898 * data item, the symbol data is copied to the executables address 2899 * space. In this copy-relocation case, we must also reassociate 2900 * the alias symbol with its new location in the executable. 2901 * 2902 * - If the referenced symbol is a function then we may need to 2903 * promote the symbols binding from undefined weak to undefined, 2904 * otherwise the run-time linker will not generate the correct 2905 * relocation error should the symbol not be found. 2906 * 2907 * Weak alias association is also required when a local dynsym table 2908 * is being created. This table should only contain one instance of a 2909 * symbol that is associated to a given address. 2910 * 2911 * The true association between a weak/strong symbol pair is that both 2912 * symbol entries are identical, thus first we create a sorted symbol 2913 * list keyed off of the symbols section index and value. If the symbol 2914 * belongs to the same section and has the same value, then the chances 2915 * are that the rest of the symbols data is the same. This list is then 2916 * scanned for weak symbols, and if one is found then any strong 2917 * association will exist in the entries that follow. Thus we just have 2918 * to scan one (typically a single alias) or more (in the uncommon 2919 * instance of multiple weak to strong associations) entries to 2920 * determine if a match exists. 2921 */ 2922 if (weak && (OFL_ALLOW_LDYNSYM(ofl) || (etype == ET_DYN)) && 2923 (total > local)) { 2924 static Sym_desc **sort; 2925 static size_t osize = 0; 2926 size_t nsize = (total - local) * sizeof (Sym_desc *); 2927 2928 /* 2929 * As we might be processing many input files, and many symbols, 2930 * try and reuse a static sort buffer. Note, presently we're 2931 * playing the game of never freeing any buffers as there's a 2932 * belief this wastes time. 2933 */ 2934 if ((osize == 0) || (nsize > osize)) { 2935 if ((sort = libld_malloc(nsize)) == NULL) 2936 return (S_ERROR); 2937 osize = nsize; 2938 } 2939 (void) memcpy((void *)sort, &ifl->ifl_oldndx[local], nsize); 2940 2941 qsort(sort, (total - local), sizeof (Sym_desc *), compare); 2942 2943 for (ndx = 0; ndx < (total - local); ndx++) { 2944 Sym_desc *wsdp = sort[ndx]; 2945 Sym *wsym; 2946 int sndx; 2947 2948 /* 2949 * Ignore any empty symbol descriptor, or the case where 2950 * the symbol has been resolved to a different file. 2951 */ 2952 if ((wsdp == NULL) || (wsdp->sd_file != ifl)) 2953 continue; 2954 2955 wsym = wsdp->sd_sym; 2956 2957 if ((wsym->st_shndx == SHN_UNDEF) || 2958 (wsdp->sd_flags & FLG_SY_SPECSEC) || 2959 (ELF_ST_BIND(wsym->st_info) != STB_WEAK)) 2960 continue; 2961 2962 /* 2963 * We have a weak symbol, if it has a strong alias it 2964 * will have been sorted to one of the following sort 2965 * table entries. Note that we could have multiple weak 2966 * symbols aliased to one strong (if this occurs then 2967 * the strong symbol only maintains one alias back to 2968 * the last weak). 2969 */ 2970 for (sndx = ndx + 1; sndx < (total - local); sndx++) { 2971 Sym_desc *ssdp = sort[sndx]; 2972 Sym *ssym; 2973 sd_flag_t w_dynbits, s_dynbits; 2974 2975 /* 2976 * Ignore any empty symbol descriptor, or the 2977 * case where the symbol has been resolved to a 2978 * different file. 2979 */ 2980 if ((ssdp == NULL) || (ssdp->sd_file != ifl)) 2981 continue; 2982 2983 ssym = ssdp->sd_sym; 2984 2985 if (ssym->st_shndx == SHN_UNDEF) 2986 continue; 2987 2988 if ((ssym->st_shndx != wsym->st_shndx) || 2989 (ssym->st_value != wsym->st_value)) 2990 break; 2991 2992 if ((ssym->st_size != wsym->st_size) || 2993 (ssdp->sd_flags & FLG_SY_SPECSEC) || 2994 (ELF_ST_BIND(ssym->st_info) == STB_WEAK)) 2995 continue; 2996 2997 /* 2998 * If a sharable object, set link fields so 2999 * that they reference each other.` 3000 */ 3001 if (etype == ET_DYN) { 3002 ssdp->sd_aux->sa_linkndx = 3003 (Word)wsdp->sd_symndx; 3004 wsdp->sd_aux->sa_linkndx = 3005 (Word)ssdp->sd_symndx; 3006 } 3007 3008 /* 3009 * Determine which of these two symbols go into 3010 * the sort section. If a mapfile has made 3011 * explicit settings of the FLG_SY_*DYNSORT 3012 * flags for both symbols, then we do what they 3013 * say. If one has the DYNSORT flags set, we 3014 * set the NODYNSORT bit in the other. And if 3015 * neither has an explicit setting, then we 3016 * favor the weak symbol because they usually 3017 * lack the leading underscore. 3018 */ 3019 w_dynbits = wsdp->sd_flags & 3020 (FLG_SY_DYNSORT | FLG_SY_NODYNSORT); 3021 s_dynbits = ssdp->sd_flags & 3022 (FLG_SY_DYNSORT | FLG_SY_NODYNSORT); 3023 if (!(w_dynbits && s_dynbits)) { 3024 if (s_dynbits) { 3025 if (s_dynbits == FLG_SY_DYNSORT) 3026 wsdp->sd_flags |= 3027 FLG_SY_NODYNSORT; 3028 } else if (w_dynbits != 3029 FLG_SY_NODYNSORT) { 3030 ssdp->sd_flags |= 3031 FLG_SY_NODYNSORT; 3032 } 3033 } 3034 break; 3035 } 3036 } 3037 } 3038 3039 /* 3040 * Having processed all symbols, under -z symbolcap, reprocess any 3041 * symbols that are being translated from global to locals. The symbol 3042 * pair that has been collected defines the original symbol (c_osdp), 3043 * which will become a local, and the new symbol (c_nsdp), which will 3044 * become a reference (UNDEF) for the original. 3045 * 3046 * Scan these symbol pairs looking for weak symbols, which have non-weak 3047 * aliases. There is no need to translate both of these symbols to 3048 * locals, only the global is necessary. 3049 */ 3050 if (cappairs) { 3051 Aliste idx1; 3052 Cap_pair *cpp1; 3053 3054 for (ALIST_TRAVERSE(cappairs, idx1, cpp1)) { 3055 Sym_desc *sdp1 = cpp1->c_osdp; 3056 Sym *sym1 = sdp1->sd_sym; 3057 uchar_t bind1 = ELF_ST_BIND(sym1->st_info); 3058 Aliste idx2; 3059 Cap_pair *cpp2; 3060 3061 /* 3062 * If this symbol isn't weak, it's capability member is 3063 * retained for the creation of a local symbol. 3064 */ 3065 if (bind1 != STB_WEAK) 3066 continue; 3067 3068 /* 3069 * If this is a weak symbol, traverse the capabilities 3070 * list again to determine if a corresponding non-weak 3071 * symbol exists. 3072 */ 3073 for (ALIST_TRAVERSE(cappairs, idx2, cpp2)) { 3074 Sym_desc *sdp2 = cpp2->c_osdp; 3075 Sym *sym2 = sdp2->sd_sym; 3076 uchar_t bind2 = 3077 ELF_ST_BIND(sym2->st_info); 3078 3079 if ((cpp1 == cpp2) || 3080 (cpp1->c_group != cpp2->c_group) || 3081 (sym1->st_value != sym2->st_value) || 3082 (bind2 == STB_WEAK)) 3083 continue; 3084 3085 /* 3086 * The weak symbol (sym1) has a non-weak (sym2) 3087 * counterpart. There's no point in translating 3088 * both of these equivalent symbols to locals. 3089 * Add this symbol capability alias to the 3090 * capabilities family information, and remove 3091 * the weak symbol. 3092 */ 3093 if (ld_cap_add_family(ofl, cpp2->c_nsdp, 3094 cpp1->c_nsdp, NULL, NULL) == S_ERROR) 3095 return (S_ERROR); 3096 3097 free((void *)cpp1->c_osdp); 3098 (void) alist_delete(cappairs, &idx1); 3099 } 3100 } 3101 3102 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD)); 3103 3104 /* 3105 * The capability pairs information now represents all the 3106 * global symbols that need transforming to locals. These 3107 * local symbols are renamed using their group identifiers. 3108 */ 3109 for (ALIST_TRAVERSE(cappairs, idx1, cpp1)) { 3110 Sym_desc *osdp = cpp1->c_osdp; 3111 Objcapset *capset; 3112 size_t nsize, tsize; 3113 const char *oname; 3114 char *cname, *idstr; 3115 Sym *csym; 3116 3117 /* 3118 * If the local symbol has not yet been translated 3119 * convert it to a local symbol with a name. 3120 */ 3121 if ((osdp->sd_flags & FLG_SY_CAP) != 0) 3122 continue; 3123 3124 /* 3125 * As we're converting object capabilities to symbol 3126 * capabilities, obtain the capabilities set for this 3127 * object, so as to retrieve the CA_SUNW_ID value. 3128 */ 3129 capset = &cpp1->c_group->cg_set; 3130 3131 /* 3132 * Create a new name from the existing symbol and the 3133 * capabilities group identifier. Note, the delimiter 3134 * between the symbol name and identifier name is hard- 3135 * coded here (%), so that we establish a convention 3136 * for transformed symbol names. 3137 */ 3138 oname = osdp->sd_name; 3139 3140 idstr = capset->oc_id.cs_str; 3141 nsize = strlen(oname); 3142 tsize = nsize + 1 + strlen(idstr) + 1; 3143 if ((cname = libld_malloc(tsize)) == 0) 3144 return (S_ERROR); 3145 3146 (void) strcpy(cname, oname); 3147 cname[nsize++] = '%'; 3148 (void) strcpy(&cname[nsize], idstr); 3149 3150 /* 3151 * Allocate a new symbol table entry, transform this 3152 * symbol to a local, and assign the new name. 3153 */ 3154 if ((csym = libld_malloc(sizeof (Sym))) == NULL) 3155 return (S_ERROR); 3156 3157 *csym = *osdp->sd_sym; 3158 csym->st_info = ELF_ST_INFO(STB_LOCAL, 3159 ELF_ST_TYPE(osdp->sd_sym->st_info)); 3160 3161 osdp->sd_name = cname; 3162 osdp->sd_sym = csym; 3163 osdp->sd_flags = FLG_SY_CAP; 3164 3165 /* 3166 * Keep track of this new local symbol. As -z symbolcap 3167 * can only be used to create a relocatable object, a 3168 * dynamic symbol table can't exist. Ensure there is 3169 * space reserved in the string table. 3170 */ 3171 ofl->ofl_caploclcnt++; 3172 if (st_insert(ofl->ofl_strtab, cname) == -1) 3173 return (S_ERROR); 3174 3175 DBG_CALL(Dbg_syms_cap_local(ofl, cpp1->c_ndx, 3176 cname, csym, osdp)); 3177 3178 /* 3179 * Establish this capability pair as a family. 3180 */ 3181 if (ld_cap_add_family(ofl, cpp1->c_nsdp, osdp, 3182 cpp1->c_group, &ifl->ifl_caps->ca_syms) == S_ERROR) 3183 return (S_ERROR); 3184 } 3185 } 3186 3187 return (1); 3188 3189 #undef SYM_LOC_BADADDR 3190 } 3191 3192 /* 3193 * Add an undefined symbol to the symbol table. The reference originates from 3194 * the location identified by the message id (mid). These references can 3195 * originate from command line options such as -e, -u, -initarray, etc. 3196 * (identified with MSG_INTL(MSG_STR_COMMAND)), or from internally generated 3197 * TLS relocation references (identified with MSG_INTL(MSG_STR_TLSREL)). 3198 */ 3199 Sym_desc * 3200 ld_sym_add_u(const char *name, Ofl_desc *ofl, Msg mid) 3201 { 3202 Sym *sym; 3203 Ifl_desc *ifl = NULL, *_ifl; 3204 Sym_desc *sdp; 3205 Word hash; 3206 Aliste idx; 3207 avl_index_t where; 3208 const char *reference = MSG_INTL(mid); 3209 3210 /* 3211 * As an optimization, determine whether we've already generated this 3212 * reference. If the symbol doesn't already exist we'll create it. 3213 * Or if the symbol does exist from a different source, we'll resolve 3214 * the conflict. 3215 */ 3216 /* LINTED */ 3217 hash = (Word)elf_hash(name); 3218 if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL) { 3219 if ((sdp->sd_sym->st_shndx == SHN_UNDEF) && 3220 (sdp->sd_file->ifl_name == reference)) 3221 return (sdp); 3222 } 3223 3224 /* 3225 * Determine whether a pseudo input file descriptor exists to represent 3226 * the command line, as any global symbol needs an input file descriptor 3227 * during any symbol resolution (refer to map_ifl() which provides a 3228 * similar method for adding symbols from mapfiles). 3229 */ 3230 for (APLIST_TRAVERSE(ofl->ofl_objs, idx, _ifl)) 3231 if (strcmp(_ifl->ifl_name, reference) == 0) { 3232 ifl = _ifl; 3233 break; 3234 } 3235 3236 /* 3237 * If no descriptor exists create one. 3238 */ 3239 if (ifl == NULL) { 3240 if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) == NULL) 3241 return ((Sym_desc *)S_ERROR); 3242 ifl->ifl_name = reference; 3243 ifl->ifl_flags = FLG_IF_NEEDED | FLG_IF_FILEREF; 3244 if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr), 1)) == NULL) 3245 return ((Sym_desc *)S_ERROR); 3246 ifl->ifl_ehdr->e_type = ET_REL; 3247 3248 if (aplist_append(&ofl->ofl_objs, ifl, AL_CNT_OFL_OBJS) == NULL) 3249 return ((Sym_desc *)S_ERROR); 3250 } 3251 3252 /* 3253 * Allocate a symbol structure and add it to the global symbol table. 3254 */ 3255 if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL) 3256 return ((Sym_desc *)S_ERROR); 3257 sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE); 3258 sym->st_shndx = SHN_UNDEF; 3259 3260 DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl)); 3261 if (sdp == NULL) { 3262 DBG_CALL(Dbg_syms_global(ofl->ofl_lml, 0, name)); 3263 if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, 0, SHN_UNDEF, 3264 0, &where)) == (Sym_desc *)S_ERROR) 3265 return ((Sym_desc *)S_ERROR); 3266 } else if (ld_sym_resolve(sdp, sym, ifl, ofl, 0, 3267 SHN_UNDEF, 0) == S_ERROR) 3268 return ((Sym_desc *)S_ERROR); 3269 3270 sdp->sd_flags &= ~FLG_SY_CLEAN; 3271 sdp->sd_flags |= FLG_SY_CMDREF; 3272 3273 return (sdp); 3274 } 3275 3276 /* 3277 * STT_SECTION symbols have their st_name field set to NULL, and consequently 3278 * have no name. Generate a name suitable for diagnostic use for such a symbol 3279 * and store it in the input section descriptor. The resulting name will be 3280 * of the form: 3281 * 3282 * "XXX (section)" 3283 * 3284 * where XXX is the name of the section. 3285 * 3286 * entry: 3287 * isc - Input section associated with the symbol. 3288 * fmt - NULL, or format string to use. 3289 * 3290 * exit: 3291 * Sets isp->is_sym_name to the allocated string. Returns the 3292 * string pointer, or NULL on allocation failure. 3293 */ 3294 const char * 3295 ld_stt_section_sym_name(Is_desc *isp) 3296 { 3297 const char *fmt; 3298 char *str; 3299 size_t len; 3300 3301 if ((isp == NULL) || (isp->is_name == NULL)) 3302 return (NULL); 3303 3304 if (isp->is_sym_name == NULL) { 3305 fmt = (isp->is_flags & FLG_IS_GNSTRMRG) ? 3306 MSG_INTL(MSG_STR_SECTION_MSTR) : MSG_INTL(MSG_STR_SECTION); 3307 3308 len = strlen(fmt) + strlen(isp->is_name) + 1; 3309 3310 if ((str = libld_malloc(len)) == NULL) 3311 return (NULL); 3312 (void) snprintf(str, len, fmt, isp->is_name); 3313 isp->is_sym_name = str; 3314 } 3315 3316 return (isp->is_sym_name); 3317 } 3318 3319 /* 3320 * If we're producing a relocatable object and the symbol is eligible for 3321 * COMDAT section, it shouldn't be reduced in scope as that will break the 3322 * COMDAT matching when the output object is later consumed. Leave it alone, 3323 * and any reduction (and COMDAT) processing will occur then. 3324 * 3325 * Otherwise, any hidden symbol is reduced when reductions are being processed. 3326 */ 3327 Boolean 3328 ld_sym_reducable(Ofl_desc *ofl, Sym_desc *sdp) 3329 { 3330 Is_desc *isc = sdp->sd_isc; 3331 3332 if (((ofl->ofl_flags & FLG_OF_RELOBJ) != 0) && 3333 (isc != NULL) && 3334 ((isc->is_flags & FLG_IS_COMDAT) != 0)) { 3335 return (FALSE); 3336 } else { 3337 return (SYM_IS_HIDDEN(sdp) && 3338 (ofl->ofl_flags & FLG_OF_PROCRED)); 3339 } 3340 } 3341