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 2009 Sun Microsystems, Inc. All rights reserved. 28 * Use is subject to license terms. 29 */ 30 31 /* 32 * Symbol table management routines 33 */ 34 35 #define ELF_TARGET_AMD64 36 37 #include <stdio.h> 38 #include <string.h> 39 #include <debug.h> 40 #include "msg.h" 41 #include "_libld.h" 42 43 /* 44 * AVL tree comparator function: 45 * 46 * The primary key is the symbol name hash with a secondary key of the symbol 47 * name itself. 48 */ 49 int 50 ld_sym_avl_comp(const void *elem1, const void *elem2) 51 { 52 Sym_avlnode *sav1 = (Sym_avlnode *)elem1; 53 Sym_avlnode *sav2 = (Sym_avlnode *)elem2; 54 int res; 55 56 res = sav1->sav_hash - sav2->sav_hash; 57 58 if (res < 0) 59 return (-1); 60 if (res > 0) 61 return (1); 62 63 /* 64 * Hash is equal - now compare name 65 */ 66 res = strcmp(sav1->sav_name, sav2->sav_name); 67 if (res == 0) 68 return (0); 69 if (res > 0) 70 return (1); 71 return (-1); 72 } 73 74 /* 75 * Focal point for verifying symbol names. 76 */ 77 inline static const char * 78 string(Ofl_desc *ofl, Ifl_desc *ifl, Sym *sym, const char *strs, size_t strsize, 79 int symndx, Word shndx, const char *symsecname, const char *strsecname, 80 Word *flags) 81 { 82 Word name = sym->st_name; 83 84 if (name) { 85 if ((ifl->ifl_flags & FLG_IF_HSTRTAB) == 0) { 86 eprintf(ofl->ofl_lml, ERR_FATAL, 87 MSG_INTL(MSG_FIL_NOSTRTABLE), ifl->ifl_name, 88 symsecname, symndx, EC_XWORD(name)); 89 return (NULL); 90 } 91 if (name >= (Word)strsize) { 92 eprintf(ofl->ofl_lml, ERR_FATAL, 93 MSG_INTL(MSG_FIL_EXCSTRTABLE), ifl->ifl_name, 94 symsecname, symndx, EC_XWORD(name), 95 strsecname, EC_XWORD(strsize)); 96 return (NULL); 97 } 98 } 99 100 /* 101 * Determine if we're dealing with a register and if so validate it. 102 * If it's a scratch register, a fabricated name will be returned. 103 */ 104 if (ld_targ.t_ms.ms_is_regsym != NULL) { 105 const char *regname = (*ld_targ.t_ms.ms_is_regsym)(ofl, ifl, 106 sym, strs, symndx, shndx, symsecname, flags); 107 108 if (regname == (const char *)S_ERROR) { 109 return (NULL); 110 } 111 if (regname) 112 return (regname); 113 } 114 115 /* 116 * If this isn't a register, but we have a global symbol with a null 117 * name, we're not going to be able to hash this, search for it, or 118 * do anything interesting. However, we've been accepting a symbol of 119 * this kind for ages now, so give the user a warning (rather than a 120 * fatal error), just in case this instance exists somewhere in the 121 * world and hasn't, as yet, been a problem. 122 */ 123 if ((name == 0) && (ELF_ST_BIND(sym->st_info) != STB_LOCAL)) { 124 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_NONAMESYM), 125 ifl->ifl_name, symsecname, symndx, EC_XWORD(name)); 126 } 127 return (strs + name); 128 } 129 130 /* 131 * For producing symbol names strings to use in error messages. 132 * If the symbol has a non-null name, then the string returned by 133 * this function is the output from demangle(), surrounded by 134 * single quotes. For null names, a descriptive string giving 135 * the symbol section and index is generated. 136 * 137 * This function uses an internal static buffer to hold the resulting 138 * string. The value returned is usable by the caller until the next 139 * call, at which point it is overwritten. 140 */ 141 static const char * 142 demangle_symname(const char *name, const char *symtab_name, Word symndx) 143 { 144 #define INIT_BUFSIZE 256 145 146 static char *buf; 147 static size_t bufsize = 0; 148 size_t len; 149 int use_name; 150 151 use_name = (name != NULL) && (*name != '\0'); 152 153 if (use_name) { 154 name = demangle(name); 155 len = strlen(name) + 2; /* Include room for quotes */ 156 } else { 157 name = MSG_ORIG(MSG_STR_EMPTY); 158 len = strlen(symtab_name) + 2 + CONV32_INV_BUFSIZE; 159 } 160 len++; /* Null termination */ 161 162 /* If our buffer is too small, double it until it is big enough */ 163 if (len > bufsize) { 164 size_t new_bufsize = bufsize; 165 char *new_buf; 166 167 if (new_bufsize == 0) 168 new_bufsize = INIT_BUFSIZE; 169 while (len > new_bufsize) 170 new_bufsize *= 2; 171 if ((new_buf = libld_malloc(new_bufsize)) == NULL) 172 return (name); 173 buf = new_buf; 174 bufsize = new_bufsize; 175 } 176 177 if (use_name) { 178 (void) snprintf(buf, bufsize, MSG_ORIG(MSG_FMT_SYMNAM), name); 179 } else { 180 (void) snprintf(buf, bufsize, MSG_ORIG(MSG_FMT_NULLSYMNAM), 181 symtab_name, EC_WORD(symndx)); 182 } 183 184 return (buf); 185 186 #undef INIT_BUFSIZE 187 } 188 189 /* 190 * Shared objects can be built that define specific symbols that can not be 191 * directly bound to. These objects have a syminfo section (and an associated 192 * DF_1_NODIRECT dynamic flags entry). Scan this table looking for symbols 193 * that can't be bound to directly, and if this files symbol is presently 194 * referenced, mark it so that we don't directly bind to it. 195 */ 196 uintptr_t 197 ld_sym_nodirect(Is_desc *isp, Ifl_desc *ifl, Ofl_desc *ofl) 198 { 199 Shdr *sifshdr, *symshdr; 200 Syminfo *sifdata; 201 Sym *symdata; 202 char *strdata; 203 ulong_t cnt, _cnt; 204 205 /* 206 * Get the syminfo data, and determine the number of entries. 207 */ 208 sifshdr = isp->is_shdr; 209 sifdata = (Syminfo *)isp->is_indata->d_buf; 210 cnt = sifshdr->sh_size / sifshdr->sh_entsize; 211 212 /* 213 * Get the associated symbol table. 214 */ 215 symshdr = ifl->ifl_isdesc[sifshdr->sh_link]->is_shdr; 216 symdata = ifl->ifl_isdesc[sifshdr->sh_link]->is_indata->d_buf; 217 218 /* 219 * Get the string table associated with the symbol table. 220 */ 221 strdata = ifl->ifl_isdesc[symshdr->sh_link]->is_indata->d_buf; 222 223 /* 224 * Traverse the syminfo data for symbols that can't be directly 225 * bound to. 226 */ 227 for (_cnt = 1, sifdata++; _cnt < cnt; _cnt++, sifdata++) { 228 Sym *sym; 229 char *str; 230 Sym_desc *sdp; 231 232 if ((sifdata->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0) 233 continue; 234 235 sym = (Sym *)(symdata + _cnt); 236 str = (char *)(strdata + sym->st_name); 237 238 if (sdp = ld_sym_find(str, SYM_NOHASH, 0, ofl)) { 239 if (ifl != sdp->sd_file) 240 continue; 241 242 sdp->sd_flags1 &= ~FLG_SY1_DIR; 243 sdp->sd_flags1 |= FLG_SY1_NDIR; 244 } 245 } 246 return (0); 247 } 248 249 /* 250 * If, during symbol processing, it is necessary to update a local symbols 251 * contents before we have generated the symbol tables in the output image, 252 * create a new symbol structure and copy the original symbol contents. While 253 * we are processing the input files, their local symbols are part of the 254 * read-only mapped image. Commonly, these symbols are copied to the new output 255 * file image and then updated to reflect their new address and any change in 256 * attributes. However, sometimes during relocation counting, it is necessary 257 * to adjust the symbols information. This routine provides for the generation 258 * of a new symbol image so that this update can be performed. 259 * All global symbols are copied to an internal symbol table to improve locality 260 * of reference and hence performance, and thus this copying is not necessary. 261 */ 262 uintptr_t 263 ld_sym_copy(Sym_desc *sdp) 264 { 265 Sym *nsym; 266 267 if (sdp->sd_flags & FLG_SY_CLEAN) { 268 if ((nsym = libld_malloc(sizeof (Sym))) == NULL) 269 return (S_ERROR); 270 *nsym = *(sdp->sd_sym); 271 sdp->sd_sym = nsym; 272 sdp->sd_flags &= ~FLG_SY_CLEAN; 273 } 274 return (1); 275 } 276 277 /* 278 * Finds a given name in the link editors internal symbol table. If no 279 * hash value is specified it is calculated. A pointer to the located 280 * Sym_desc entry is returned, or NULL if the symbol is not found. 281 */ 282 Sym_desc * 283 ld_sym_find(const char *name, Word hash, avl_index_t *where, Ofl_desc *ofl) 284 { 285 Sym_avlnode qsav, *sav; 286 287 if (hash == SYM_NOHASH) 288 /* LINTED */ 289 hash = (Word)elf_hash((const char *)name); 290 qsav.sav_hash = hash; 291 qsav.sav_name = name; 292 293 /* 294 * Perform search for symbol in AVL tree. Note that the 'where' field 295 * is passed in from the caller. If a 'where' is present, it can be 296 * used in subsequent 'ld_sym_enter()' calls if required. 297 */ 298 sav = avl_find(&ofl->ofl_symavl, &qsav, where); 299 300 /* 301 * If symbol was not found in the avl tree, return null to show that. 302 */ 303 if (sav == NULL) 304 return (NULL); 305 306 /* 307 * Return symbol found. 308 */ 309 return (sav->sav_symdesc); 310 } 311 312 /* 313 * Enter a new symbol into the link editors internal symbol table. 314 * If the symbol is from an input file, information regarding the input file 315 * and input section is also recorded. Otherwise (file == NULL) the symbol 316 * has been internally generated (ie. _etext, _edata, etc.). 317 */ 318 Sym_desc * 319 ld_sym_enter(const char *name, Sym *osym, Word hash, Ifl_desc *ifl, 320 Ofl_desc *ofl, Word ndx, Word shndx, Word sdflags, Half sdflags1, 321 avl_index_t *where) 322 { 323 Sym_desc *sdp; 324 Sym_aux *sap; 325 Sym_avlnode *savl; 326 char *_name; 327 Sym *nsym; 328 Half etype; 329 uchar_t vis; 330 avl_index_t _where; 331 332 /* 333 * Establish the file type. 334 */ 335 if (ifl) 336 etype = ifl->ifl_ehdr->e_type; 337 else 338 etype = ET_NONE; 339 340 ofl->ofl_entercnt++; 341 342 /* 343 * Allocate a Sym Descriptor, Auxiliary Descriptor, and a Sym AVLNode - 344 * contiguously. 345 */ 346 if ((savl = libld_calloc(sizeof (Sym_avlnode) + sizeof (Sym_desc) + 347 sizeof (Sym_aux), 1)) == NULL) 348 return ((Sym_desc *)S_ERROR); 349 sdp = (Sym_desc *)((uintptr_t)savl + sizeof (Sym_avlnode)); 350 sap = (Sym_aux *)((uintptr_t)sdp + sizeof (Sym_desc)); 351 352 savl->sav_symdesc = sdp; 353 sdp->sd_file = ifl; 354 sdp->sd_aux = sap; 355 savl->sav_hash = sap->sa_hash = hash; 356 357 /* 358 * Copy the symbol table entry from the input file into the internal 359 * entry and have the symbol descriptor use it. 360 */ 361 sdp->sd_sym = nsym = &sap->sa_sym; 362 *nsym = *osym; 363 sdp->sd_shndx = shndx; 364 sdp->sd_flags |= sdflags; 365 sdp->sd_flags1 |= sdflags1; 366 367 if ((_name = libld_malloc(strlen(name) + 1)) == NULL) 368 return ((Sym_desc *)S_ERROR); 369 savl->sav_name = sdp->sd_name = (const char *)strcpy(_name, name); 370 371 /* 372 * Enter Symbol in AVL tree. 373 */ 374 if (where == 0) { 375 /* LINTED */ 376 Sym_avlnode *_savl; 377 /* 378 * If a previous ld_sym_find() hasn't initialized 'where' do it 379 * now. 380 */ 381 where = &_where; 382 _savl = avl_find(&ofl->ofl_symavl, savl, where); 383 assert(_savl == NULL); 384 } 385 avl_insert(&ofl->ofl_symavl, savl, *where); 386 387 /* 388 * Record the section index. This is possible because the 389 * `ifl_isdesc' table is filled before we start symbol processing. 390 */ 391 if ((sdflags & FLG_SY_SPECSEC) || (nsym->st_shndx == SHN_UNDEF)) 392 sdp->sd_isc = NULL; 393 else { 394 sdp->sd_isc = ifl->ifl_isdesc[shndx]; 395 396 /* 397 * If this symbol is from a relocatable object, make sure that 398 * it is still associated with a section. For example, an 399 * unknown section type (SHT_NULL) would have been rejected on 400 * input with a warning. Here, we make the use of the symbol 401 * fatal. A symbol descriptor is still returned, so that the 402 * caller can continue processing all symbols, and hence flush 403 * out as many error conditions as possible. 404 */ 405 if ((etype == ET_REL) && (sdp->sd_isc == NULL)) { 406 eprintf(ofl->ofl_lml, ERR_FATAL, 407 MSG_INTL(MSG_SYM_INVSEC), name, ifl->ifl_name, 408 EC_XWORD(shndx)); 409 ofl->ofl_flags |= FLG_OF_FATAL; 410 return (sdp); 411 } 412 } 413 414 /* 415 * Mark any COMMON symbols as 'tentative'. 416 */ 417 if (sdflags & FLG_SY_SPECSEC) { 418 if (nsym->st_shndx == SHN_COMMON) 419 sdp->sd_flags |= FLG_SY_TENTSYM; 420 #if defined(_ELF64) 421 else if ((ld_targ.t_m.m_mach == EM_AMD64) && 422 (nsym->st_shndx == SHN_X86_64_LCOMMON)) 423 sdp->sd_flags |= FLG_SY_TENTSYM; 424 #endif 425 } 426 427 /* 428 * Establish the symbols visibility and reference. 429 */ 430 vis = ELF_ST_VISIBILITY(nsym->st_other); 431 432 if ((etype == ET_NONE) || (etype == ET_REL)) { 433 switch (vis) { 434 case STV_DEFAULT: 435 sdp->sd_flags1 |= FLG_SY1_DEFAULT; 436 break; 437 case STV_INTERNAL: 438 case STV_HIDDEN: 439 sdp->sd_flags1 |= FLG_SY1_HIDDEN; 440 break; 441 case STV_PROTECTED: 442 sdp->sd_flags1 |= FLG_SY1_PROTECT; 443 break; 444 case STV_EXPORTED: 445 sdp->sd_flags1 |= FLG_SY1_EXPORT; 446 break; 447 case STV_SINGLETON: 448 sdp->sd_flags1 |= (FLG_SY1_SINGLE | FLG_SY1_NDIR); 449 ofl->ofl_flags1 |= FLG_OF1_NDIRECT; 450 break; 451 case STV_ELIMINATE: 452 sdp->sd_flags1 |= (FLG_SY1_HIDDEN | FLG_SY1_ELIM); 453 break; 454 default: 455 assert(vis <= STV_ELIMINATE); 456 } 457 458 sdp->sd_ref = REF_REL_NEED; 459 460 /* 461 * Under -Bnodirect, all exported interfaces that have not 462 * explicitly been defined protected or directly bound to, are 463 * tagged to prevent direct binding. 464 */ 465 if ((ofl->ofl_flags1 & FLG_OF1_ALNODIR) && 466 ((sdp->sd_flags1 & (FLG_SY1_PROTECT | FLG_SY1_DIR)) == 0) && 467 (nsym->st_shndx != SHN_UNDEF)) { 468 sdp->sd_flags1 |= FLG_SY1_NDIR; 469 } 470 } else { 471 sdp->sd_ref = REF_DYN_SEEN; 472 473 /* 474 * Record the binding file for this symbol in the sa_bindto 475 * field. If this symbol is ever overridden by a REF_REL_NEED 476 * definition, sa_bindto is used when building a 'translator'. 477 */ 478 if (nsym->st_shndx != SHN_UNDEF) 479 sdp->sd_aux->sa_bindto = ifl; 480 481 /* 482 * If this is a protected symbol, remember this. Note, this 483 * state is different from the FLG_SY1_PROTECT used to establish 484 * a symbol definitions visibility. This state is used to warn 485 * against possible copy relocations against this referenced 486 * symbol. 487 */ 488 if (vis == STV_PROTECTED) 489 sdp->sd_flags |= FLG_SY_PROT; 490 491 /* 492 * If this is a SINGLETON definition, then indicate the symbol 493 * can not be directly bound to, and retain the visibility. 494 * This visibility will be inherited by any references made to 495 * this symbol. 496 */ 497 if ((vis == STV_SINGLETON) && (nsym->st_shndx != SHN_UNDEF)) 498 sdp->sd_flags1 |= (FLG_SY1_SINGLE | FLG_SY1_NDIR); 499 500 /* 501 * If the new symbol is from a shared library and is associated 502 * with a SHT_NOBITS section then this symbol originated from a 503 * tentative symbol. 504 */ 505 if (sdp->sd_isc && 506 (sdp->sd_isc->is_shdr->sh_type == SHT_NOBITS)) 507 sdp->sd_flags |= FLG_SY_TENTSYM; 508 } 509 510 /* 511 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF so as to 512 * simplify future processing. 513 */ 514 if (nsym->st_shndx == SHN_SUNW_IGNORE) { 515 sdp->sd_shndx = shndx = SHN_UNDEF; 516 sdp->sd_flags |= FLG_SY_REDUCED; 517 sdp->sd_flags1 |= 518 (FLG_SY1_HIDDEN | FLG_SY1_IGNORE | FLG_SY1_ELIM); 519 } 520 521 /* 522 * If this is an undefined, or common symbol from a relocatable object 523 * determine whether it is a global or weak reference (see build_osym(), 524 * where REF_DYN_NEED definitions are returned back to undefines). 525 */ 526 if ((etype == ET_REL) && 527 (ELF_ST_BIND(nsym->st_info) == STB_GLOBAL) && 528 ((nsym->st_shndx == SHN_UNDEF) || ((sdflags & FLG_SY_SPECSEC) && 529 #if defined(_ELF64) 530 ((nsym->st_shndx == SHN_COMMON) || 531 ((ld_targ.t_m.m_mach == EM_AMD64) && 532 (nsym->st_shndx == SHN_X86_64_LCOMMON)))))) 533 #else 534 /* BEGIN CSTYLED */ 535 (nsym->st_shndx == SHN_COMMON)))) 536 /* END CSTYLED */ 537 #endif 538 sdp->sd_flags |= FLG_SY_GLOBREF; 539 540 /* 541 * Record the input filename on the referenced or defined files list 542 * for possible later diagnostics. The `sa_rfile' pointer contains the 543 * name of the file that first referenced this symbol and is used to 544 * generate undefined symbol diagnostics (refer to sym_undef_entry()). 545 * Note that this entry can be overridden if a reference from a 546 * relocatable object is found after a reference from a shared object 547 * (refer to sym_override()). 548 * The `sa_dfiles' list is used to maintain the list of files that 549 * define the same symbol. This list can be used for two reasons: 550 * 551 * - To save the first definition of a symbol that is not available 552 * for this link-edit. 553 * 554 * - To save all definitions of a symbol when the -m option is in 555 * effect. This is optional as it is used to list multiple 556 * (interposed) definitions of a symbol (refer to ldmap_out()), 557 * and can be quite expensive. 558 */ 559 if (nsym->st_shndx == SHN_UNDEF) { 560 sap->sa_rfile = ifl->ifl_name; 561 } else { 562 if (sdp->sd_ref == REF_DYN_SEEN) { 563 /* 564 * A symbol is determined to be unavailable if it 565 * belongs to a version of a shared object that this 566 * user does not wish to use, or if it belongs to an 567 * implicit shared object. 568 */ 569 if (ifl->ifl_vercnt) { 570 Ver_index *vip; 571 Half vndx = ifl->ifl_versym[ndx]; 572 573 sap->sa_dverndx = vndx; 574 vip = &ifl->ifl_verndx[vndx]; 575 if (!(vip->vi_flags & FLG_VER_AVAIL)) { 576 sdp->sd_flags |= FLG_SY_NOTAVAIL; 577 sap->sa_vfile = ifl->ifl_name; 578 } 579 } 580 if (!(ifl->ifl_flags & FLG_IF_NEEDED)) 581 sdp->sd_flags |= FLG_SY_NOTAVAIL; 582 583 } else if (etype == ET_REL) { 584 /* 585 * If this symbol has been obtained from a versioned 586 * input relocatable object then the new symbol must be 587 * promoted to the versioning of the output file. 588 */ 589 if (ifl->ifl_versym) 590 ld_vers_promote(sdp, ndx, ifl, ofl); 591 } 592 593 if ((ofl->ofl_flags & FLG_OF_GENMAP) && 594 ((sdflags & FLG_SY_SPECSEC) == 0)) 595 if (list_appendc(&sap->sa_dfiles, ifl->ifl_name) == 0) 596 return ((Sym_desc *)S_ERROR); 597 } 598 599 /* 600 * Provided we're not processing a mapfile, diagnose the entered symbol. 601 * Mapfile processing requires the symbol to be updated with additional 602 * information, therefore the diagnosing of the symbol is deferred until 603 * later (see Dbg_map_symbol()). 604 */ 605 if ((ifl == NULL) || ((ifl->ifl_flags & FLG_IF_MAPFILE) == 0)) 606 DBG_CALL(Dbg_syms_entered(ofl, nsym, sdp)); 607 return (sdp); 608 } 609 610 /* 611 * Add a special symbol to the symbol table. Takes special symbol name with 612 * and without underscores. This routine is called, after all other symbol 613 * resolution has completed, to generate a reserved absolute symbol (the 614 * underscore version). Special symbols are updated with the appropriate 615 * values in update_osym(). If the user has already defined this symbol 616 * issue a warning and leave the symbol as is. If the non-underscore symbol 617 * is referenced then turn it into a weak alias of the underscored symbol. 618 * 619 * The bits in flags_u are OR'd into the flags field of the symbol 620 * for the underscored symbol. 621 * 622 * If this is a global symbol, and it hasn't explicitly been defined as being 623 * directly bound to, indicate that it can't be directly bound to. 624 * Historically, most special symbols only have meaning to the object in which 625 * they exist, however, they've always been global. To ensure compatibility 626 * with any unexpected use presently in effect, ensure these symbols don't get 627 * directly bound to. Note, that establishing this state here isn't sufficient 628 * to create a syminfo table, only if a syminfo table is being created by some 629 * other symbol directives will the nodirect binding be recorded. This ensures 630 * we don't create syminfo sections for all objects we create, as this might add 631 * unnecessary bloat to users who haven't explicitly requested extra symbol 632 * information. 633 */ 634 static uintptr_t 635 sym_add_spec(const char *name, const char *uname, Word sdaux_id, 636 Word flags_u, Half flags1, Ofl_desc *ofl) 637 { 638 Sym_desc *sdp; 639 Sym_desc *usdp; 640 Sym *sym; 641 Word hash; 642 avl_index_t where; 643 644 /* LINTED */ 645 hash = (Word)elf_hash(uname); 646 if (usdp = ld_sym_find(uname, hash, &where, ofl)) { 647 /* 648 * If the underscore symbol exists and is undefined, or was 649 * defined in a shared library, convert it to a local symbol. 650 * Otherwise leave it as is and warn the user. 651 */ 652 if ((usdp->sd_shndx == SHN_UNDEF) || 653 (usdp->sd_ref != REF_REL_NEED)) { 654 usdp->sd_ref = REF_REL_NEED; 655 usdp->sd_shndx = usdp->sd_sym->st_shndx = SHN_ABS; 656 usdp->sd_flags |= FLG_SY_SPECSEC | flags_u; 657 usdp->sd_sym->st_info = 658 ELF_ST_INFO(STB_GLOBAL, STT_OBJECT); 659 usdp->sd_isc = NULL; 660 usdp->sd_sym->st_size = 0; 661 usdp->sd_sym->st_value = 0; 662 /* LINTED */ 663 usdp->sd_aux->sa_symspec = (Half)sdaux_id; 664 665 /* 666 * If a user hasn't specifically indicated that the 667 * scope of this symbol be made local, then leave it 668 * as global (ie. prevent automatic scoping). The GOT 669 * should be defined protected, whereas all other 670 * special symbols are tagged as no-direct. 671 */ 672 if (((usdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) && 673 (flags1 & FLG_SY1_DEFAULT)) { 674 usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL; 675 if (sdaux_id == SDAUX_ID_GOT) { 676 usdp->sd_flags1 &= ~FLG_SY1_NDIR; 677 usdp->sd_flags1 |= FLG_SY1_PROTECT; 678 usdp->sd_sym->st_other = STV_PROTECTED; 679 } else if ( 680 ((usdp->sd_flags1 & FLG_SY1_DIR) == 0) && 681 ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) { 682 usdp->sd_flags1 |= FLG_SY1_NDIR; 683 } 684 } 685 usdp->sd_flags1 |= flags1; 686 687 /* 688 * If the reference originated from a mapfile ensure 689 * we mark the symbol as used. 690 */ 691 if (usdp->sd_flags & FLG_SY_MAPREF) 692 usdp->sd_flags |= FLG_SY_MAPUSED; 693 694 DBG_CALL(Dbg_syms_updated(ofl, usdp, uname)); 695 } else 696 eprintf(ofl->ofl_lml, ERR_WARNING, 697 MSG_INTL(MSG_SYM_RESERVE), uname, 698 usdp->sd_file->ifl_name); 699 } else { 700 /* 701 * If the symbol does not exist create it. 702 */ 703 if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL) 704 return (S_ERROR); 705 sym->st_shndx = SHN_ABS; 706 sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT); 707 sym->st_size = 0; 708 sym->st_value = 0; 709 DBG_CALL(Dbg_syms_created(ofl->ofl_lml, uname)); 710 if ((usdp = ld_sym_enter(uname, sym, hash, (Ifl_desc *)NULL, 711 ofl, 0, SHN_ABS, FLG_SY_SPECSEC | flags_u, 0, &where)) == 712 (Sym_desc *)S_ERROR) 713 return (S_ERROR); 714 usdp->sd_ref = REF_REL_NEED; 715 /* LINTED */ 716 usdp->sd_aux->sa_symspec = (Half)sdaux_id; 717 718 usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL; 719 720 if (sdaux_id == SDAUX_ID_GOT) { 721 usdp->sd_flags1 |= FLG_SY1_PROTECT; 722 usdp->sd_sym->st_other = STV_PROTECTED; 723 } else if ((flags1 & FLG_SY1_DEFAULT) && 724 ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) { 725 usdp->sd_flags1 |= FLG_SY1_NDIR; 726 } 727 usdp->sd_flags1 |= flags1; 728 } 729 730 if (name && (sdp = ld_sym_find(name, SYM_NOHASH, 0, ofl)) && 731 (sdp->sd_sym->st_shndx == SHN_UNDEF)) { 732 uchar_t bind; 733 734 /* 735 * If the non-underscore symbol exists and is undefined 736 * convert it to be a local. If the underscore has 737 * sa_symspec set (ie. it was created above) then simulate this 738 * as a weak alias. 739 */ 740 sdp->sd_ref = REF_REL_NEED; 741 sdp->sd_shndx = sdp->sd_sym->st_shndx = SHN_ABS; 742 sdp->sd_flags |= FLG_SY_SPECSEC; 743 sdp->sd_isc = NULL; 744 sdp->sd_sym->st_size = 0; 745 sdp->sd_sym->st_value = 0; 746 /* LINTED */ 747 sdp->sd_aux->sa_symspec = (Half)sdaux_id; 748 if (usdp->sd_aux->sa_symspec) { 749 usdp->sd_aux->sa_linkndx = 0; 750 sdp->sd_aux->sa_linkndx = 0; 751 bind = STB_WEAK; 752 } else 753 bind = STB_GLOBAL; 754 sdp->sd_sym->st_info = ELF_ST_INFO(bind, STT_OBJECT); 755 756 /* 757 * If a user hasn't specifically indicated the scope of this 758 * symbol be made local then leave it as global (ie. prevent 759 * automatic scoping). The GOT should be defined protected, 760 * whereas all other special symbols are tagged as no-direct. 761 */ 762 if (((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) && 763 (flags1 & FLG_SY1_DEFAULT)) { 764 sdp->sd_aux->sa_overndx = VER_NDX_GLOBAL; 765 if (sdaux_id == SDAUX_ID_GOT) { 766 sdp->sd_flags1 &= ~FLG_SY1_NDIR; 767 sdp->sd_flags1 |= FLG_SY1_PROTECT; 768 sdp->sd_sym->st_other = STV_PROTECTED; 769 } else if (((sdp->sd_flags1 & FLG_SY1_DIR) == 0) && 770 ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) { 771 sdp->sd_flags1 |= FLG_SY1_NDIR; 772 } 773 } 774 sdp->sd_flags1 |= flags1; 775 776 /* 777 * If the reference originated from a mapfile ensure 778 * we mark the symbol as used. 779 */ 780 if (sdp->sd_flags & FLG_SY_MAPREF) 781 sdp->sd_flags |= FLG_SY_MAPUSED; 782 783 DBG_CALL(Dbg_syms_updated(ofl, sdp, name)); 784 } 785 return (1); 786 } 787 788 789 /* 790 * Print undefined symbols. 791 */ 792 static Boolean undef_title = TRUE; 793 794 static void 795 sym_undef_title(Ofl_desc *ofl) 796 { 797 eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(MSG_SYM_FMT_UNDEF), 798 MSG_INTL(MSG_SYM_UNDEF_ITM_11), 799 MSG_INTL(MSG_SYM_UNDEF_ITM_21), 800 MSG_INTL(MSG_SYM_UNDEF_ITM_12), 801 MSG_INTL(MSG_SYM_UNDEF_ITM_22)); 802 803 undef_title = FALSE; 804 } 805 806 /* 807 * Undefined symbols can fall into one of four types: 808 * 809 * - the symbol is really undefined (SHN_UNDEF). 810 * 811 * - versioning has been enabled, however this symbol has not been assigned 812 * to one of the defined versions. 813 * 814 * - the symbol has been defined by an implicitly supplied library, ie. one 815 * which was encounted because it was NEEDED by another library, rather 816 * than from a command line supplied library which would become the only 817 * dependency of the output file being produced. 818 * 819 * - the symbol has been defined by a version of a shared object that is 820 * not permitted for this link-edit. 821 * 822 * In all cases the file who made the first reference to this symbol will have 823 * been recorded via the `sa_rfile' pointer. 824 */ 825 typedef enum { 826 UNDEF, NOVERSION, IMPLICIT, NOTAVAIL, 827 BNDLOCAL 828 } Type; 829 830 static const Msg format[] = { 831 MSG_SYM_UND_UNDEF, /* MSG_INTL(MSG_SYM_UND_UNDEF) */ 832 MSG_SYM_UND_NOVER, /* MSG_INTL(MSG_SYM_UND_NOVER) */ 833 MSG_SYM_UND_IMPL, /* MSG_INTL(MSG_SYM_UND_IMPL) */ 834 MSG_SYM_UND_NOTA, /* MSG_INTL(MSG_SYM_UND_NOTA) */ 835 MSG_SYM_UND_BNDLOCAL /* MSG_INTL(MSG_SYM_UND_BNDLOCAL) */ 836 }; 837 838 static void 839 sym_undef_entry(Ofl_desc *ofl, Sym_desc *sdp, Type type) 840 { 841 const char *name1, *name2, *name3; 842 Ifl_desc *ifl = sdp->sd_file; 843 Sym_aux *sap = sdp->sd_aux; 844 845 if (undef_title) 846 sym_undef_title(ofl); 847 848 switch (type) { 849 case UNDEF: 850 case BNDLOCAL: 851 name1 = sap->sa_rfile; 852 break; 853 case NOVERSION: 854 name1 = ifl->ifl_name; 855 break; 856 case IMPLICIT: 857 name1 = sap->sa_rfile; 858 name2 = ifl->ifl_name; 859 break; 860 case NOTAVAIL: 861 name1 = sap->sa_rfile; 862 name2 = sap->sa_vfile; 863 name3 = ifl->ifl_verndx[sap->sa_dverndx].vi_name; 864 break; 865 default: 866 return; 867 } 868 869 eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(format[type]), 870 demangle(sdp->sd_name), name1, name2, name3); 871 } 872 873 /* 874 * At this point all symbol input processing has been completed, therefore 875 * complete the symbol table entries by generating any necessary internal 876 * symbols. 877 */ 878 uintptr_t 879 ld_sym_spec(Ofl_desc *ofl) 880 { 881 Sym_desc *sdp; 882 883 if (ofl->ofl_flags & FLG_OF_RELOBJ) 884 return (1); 885 886 DBG_CALL(Dbg_syms_spec_title(ofl->ofl_lml)); 887 888 if (sym_add_spec(MSG_ORIG(MSG_SYM_ETEXT), MSG_ORIG(MSG_SYM_ETEXT_U), 889 SDAUX_ID_ETEXT, 0, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF), 890 ofl) == S_ERROR) 891 return (S_ERROR); 892 if (sym_add_spec(MSG_ORIG(MSG_SYM_EDATA), MSG_ORIG(MSG_SYM_EDATA_U), 893 SDAUX_ID_EDATA, 0, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF), 894 ofl) == S_ERROR) 895 return (S_ERROR); 896 if (sym_add_spec(MSG_ORIG(MSG_SYM_END), MSG_ORIG(MSG_SYM_END_U), 897 SDAUX_ID_END, FLG_SY_DYNSORT, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF), 898 ofl) == S_ERROR) 899 return (S_ERROR); 900 if (sym_add_spec(MSG_ORIG(MSG_SYM_L_END), MSG_ORIG(MSG_SYM_L_END_U), 901 SDAUX_ID_END, 0, FLG_SY1_HIDDEN, ofl) == S_ERROR) 902 return (S_ERROR); 903 if (sym_add_spec(MSG_ORIG(MSG_SYM_L_START), MSG_ORIG(MSG_SYM_L_START_U), 904 SDAUX_ID_START, 0, FLG_SY1_HIDDEN, ofl) == S_ERROR) 905 return (S_ERROR); 906 907 /* 908 * Historically we've always produced a _DYNAMIC symbol, even for 909 * static executables (in which case its value will be 0). 910 */ 911 if (sym_add_spec(MSG_ORIG(MSG_SYM_DYNAMIC), MSG_ORIG(MSG_SYM_DYNAMIC_U), 912 SDAUX_ID_DYN, FLG_SY_DYNSORT, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF), 913 ofl) == S_ERROR) 914 return (S_ERROR); 915 916 if (OFL_ALLOW_DYNSYM(ofl)) 917 if (sym_add_spec(MSG_ORIG(MSG_SYM_PLKTBL), 918 MSG_ORIG(MSG_SYM_PLKTBL_U), SDAUX_ID_PLT, 919 FLG_SY_DYNSORT, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF), 920 ofl) == S_ERROR) 921 return (S_ERROR); 922 923 /* 924 * A GOT reference will be accompanied by the associated GOT symbol. 925 * Make sure it gets assigned the appropriate special attributes. 926 */ 927 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), 928 SYM_NOHASH, 0, ofl)) != 0) && (sdp->sd_ref != REF_DYN_SEEN)) { 929 if (sym_add_spec(MSG_ORIG(MSG_SYM_GOFTBL), 930 MSG_ORIG(MSG_SYM_GOFTBL_U), SDAUX_ID_GOT, FLG_SY_DYNSORT, 931 (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF), ofl) == S_ERROR) 932 return (S_ERROR); 933 } 934 935 return (1); 936 } 937 938 /* 939 * This routine checks to see if a symbols visibility needs to be reduced to 940 * either SYMBOLIC or LOCAL. This routine can be called from either 941 * reloc_init() or sym_validate(). 942 */ 943 void 944 ld_sym_adjust_vis(Sym_desc *sdp, Ofl_desc *ofl) 945 { 946 ofl_flag_t oflags = ofl->ofl_flags; 947 Sym *sym = sdp->sd_sym; 948 949 if ((sdp->sd_ref == REF_REL_NEED) && 950 (sdp->sd_sym->st_shndx != SHN_UNDEF)) { 951 /* 952 * If auto-reduction/elimination is enabled, reduce any 953 * non-versioned global symbols. This routine is called either 954 * from any initial relocation processing that references this 955 * symbol, or from the symbol validation processing. 956 * 957 * A symbol is a candidate for auto-reduction/elimination if: 958 * 959 * . the symbol wasn't explicitly defined within a mapfile 960 * (in which case all the necessary state has been applied 961 * to the symbol), or 962 * . the symbol isn't one of the family of reserved 963 * special symbols (ie. _end, _etext, etc.), or 964 * . the symbol isn't a SINGLETON, or 965 * . the symbol wasn't explicitly defined within a version 966 * definition associated with an input relocatable object. 967 * 968 * Indicate that the symbol has been reduced as it may be 969 * necessary to print these symbols later. 970 */ 971 if ((oflags & (FLG_OF_AUTOLCL | FLG_OF_AUTOELM)) && 972 ((sdp->sd_flags1 & MSK_SY1_NOAUTO) == 0)) { 973 if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) { 974 sdp->sd_flags |= FLG_SY_REDUCED; 975 sdp->sd_flags1 |= FLG_SY1_HIDDEN; 976 } 977 978 if (oflags & (FLG_OF_REDLSYM | FLG_OF_AUTOELM)) { 979 sdp->sd_flags1 |= FLG_SY1_ELIM; 980 sym->st_other = STV_ELIMINATE | 981 (sym->st_other & ~MSK_SYM_VISIBILITY); 982 } else if (ELF_ST_VISIBILITY(sym->st_other) != 983 STV_INTERNAL) 984 sym->st_other = STV_HIDDEN | 985 (sym->st_other & ~MSK_SYM_VISIBILITY); 986 } 987 988 /* 989 * If -Bsymbolic is in effect, and the symbol hasn't explicitly 990 * been defined nodirect (via a mapfile), then bind the global 991 * symbol symbolically and assign the STV_PROTECTED visibility 992 * attribute. 993 */ 994 if ((oflags & FLG_OF_SYMBOLIC) && 995 ((sdp->sd_flags1 & (FLG_SY1_HIDDEN | FLG_SY1_NDIR)) == 0)) { 996 sdp->sd_flags1 |= FLG_SY1_PROTECT; 997 if (ELF_ST_VISIBILITY(sym->st_other) == STV_DEFAULT) 998 sym->st_other = STV_PROTECTED | 999 (sym->st_other & ~MSK_SYM_VISIBILITY); 1000 } 1001 } 1002 1003 /* 1004 * Indicate that this symbol has had it's visibility checked so that 1005 * we don't need to do this investigation again. 1006 */ 1007 sdp->sd_flags |= FLG_SY_VISIBLE; 1008 } 1009 1010 /* 1011 * Make sure a symbol definition is local to the object being built. 1012 */ 1013 static int 1014 ensure_sym_local(Ofl_desc *ofl, Sym_desc *sdp, const char *str) 1015 { 1016 if (sdp->sd_sym->st_shndx == SHN_UNDEF) { 1017 if (str) { 1018 eprintf(ofl->ofl_lml, ERR_FATAL, 1019 MSG_INTL(MSG_SYM_UNDEF), str, 1020 demangle((char *)sdp->sd_name)); 1021 } 1022 return (1); 1023 } 1024 if (sdp->sd_ref != REF_REL_NEED) { 1025 if (str) { 1026 eprintf(ofl->ofl_lml, ERR_FATAL, 1027 MSG_INTL(MSG_SYM_EXTERN), str, 1028 demangle((char *)sdp->sd_name), 1029 sdp->sd_file->ifl_name); 1030 } 1031 return (1); 1032 } 1033 1034 sdp->sd_flags |= FLG_SY_UPREQD; 1035 if (sdp->sd_isc) { 1036 sdp->sd_isc->is_flags |= FLG_IS_SECTREF; 1037 sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF; 1038 } 1039 return (0); 1040 } 1041 1042 /* 1043 * Make sure all the symbol definitions required for initarray, finiarray, or 1044 * preinitarray's are local to the object being built. 1045 */ 1046 static int 1047 ensure_array_local(Ofl_desc *ofl, List *list, const char *str) 1048 { 1049 Listnode *lnp; 1050 Sym_desc *sdp; 1051 int ret = 0; 1052 1053 for (LIST_TRAVERSE(list, lnp, sdp)) 1054 ret += ensure_sym_local(ofl, sdp, str); 1055 1056 return (ret); 1057 } 1058 1059 /* 1060 * After all symbol table input processing has been finished, and all relocation 1061 * counting has been carried out (ie. no more symbols will be read, generated, 1062 * or modified), validate and count the relevant entries: 1063 * 1064 * - check and print any undefined symbols remaining. Note that 1065 * if a symbol has been defined by virtue of the inclusion of 1066 * an implicit shared library, it is still classed as undefined. 1067 * 1068 * - count the number of global needed symbols together with the 1069 * size of their associated name strings (if scoping has been 1070 * indicated these symbols may be reduced to locals). 1071 * 1072 * - establish the size and alignment requirements for the global 1073 * .bss section (the alignment of this section is based on the 1074 * first symbol that it will contain). 1075 */ 1076 uintptr_t 1077 ld_sym_validate(Ofl_desc *ofl) 1078 { 1079 Sym_avlnode *sav; 1080 Sym_desc *sdp; 1081 Sym *sym; 1082 ofl_flag_t oflags = ofl->ofl_flags; 1083 ofl_flag_t undef = 0, needed = 0, verdesc = 0; 1084 Xword bssalign = 0, tlsalign = 0; 1085 Xword bsssize = 0, tlssize = 0; 1086 #if defined(_ELF64) 1087 Xword lbssalign = 0, lbsssize = 0; 1088 #endif 1089 int ret; 1090 int allow_ldynsym; 1091 uchar_t type; 1092 1093 /* 1094 * If a symbol is undefined and this link-edit calls for no undefined 1095 * symbols to remain (this is the default case when generating an 1096 * executable but can be enforced for any object using -z defs), the 1097 * symbol is classified as undefined and a fatal error condition will 1098 * be indicated. 1099 * 1100 * If the symbol is undefined and we're creating a shared object with 1101 * the -Bsymbolic flag, then the symbol is also classified as undefined 1102 * and a warning condition will be indicated. 1103 */ 1104 if ((oflags & (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC)) == 1105 (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC)) 1106 undef = FLG_OF_WARN; 1107 if (oflags & FLG_OF_NOUNDEF) 1108 undef = FLG_OF_FATAL; 1109 1110 /* 1111 * If the symbol is referenced from an implicitly included shared object 1112 * (ie. it's not on the NEEDED list) then the symbol is also classified 1113 * as undefined and a fatal error condition will be indicated. 1114 */ 1115 if ((oflags & FLG_OF_NOUNDEF) || !(oflags & FLG_OF_SHAROBJ)) 1116 needed = FLG_OF_FATAL; 1117 1118 /* 1119 * If the output image is being versioned all symbol definitions must be 1120 * associated with a version. Any symbol that isn't is classified as 1121 * undefined and a fatal error condition will be indicated. 1122 */ 1123 if ((oflags & FLG_OF_VERDEF) && (ofl->ofl_vercnt > VER_NDX_GLOBAL)) 1124 verdesc = FLG_OF_FATAL; 1125 1126 allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl); 1127 1128 if (allow_ldynsym) { 1129 /* 1130 * Normally, we disallow symbols with 0 size from appearing 1131 * in a dyn[sym|tls]sort section. However, there are some 1132 * symbols that serve special purposes that we want to exempt 1133 * from this rule. Look them up, and set their 1134 * FLG_SY_DYNSORT flag. 1135 */ 1136 static const char *special[] = { 1137 MSG_ORIG(MSG_SYM_INIT_U), /* _init */ 1138 MSG_ORIG(MSG_SYM_FINI_U), /* _fini */ 1139 MSG_ORIG(MSG_SYM_START), /* _start */ 1140 NULL 1141 }; 1142 int i; 1143 1144 for (i = 0; special[i] != NULL; i++) { 1145 if (((sdp = ld_sym_find(special[i], 1146 SYM_NOHASH, 0, ofl)) != NULL) && 1147 (sdp->sd_sym->st_size == 0)) { 1148 if (ld_sym_copy(sdp) == S_ERROR) 1149 return (S_ERROR); 1150 sdp->sd_flags |= FLG_SY_DYNSORT; 1151 } 1152 } 1153 } 1154 1155 /* 1156 * Collect and validate the globals from the internal symbol table. 1157 */ 1158 for (sav = avl_first(&ofl->ofl_symavl); sav; 1159 sav = AVL_NEXT(&ofl->ofl_symavl, sav)) { 1160 Is_desc *isp; 1161 int undeferr = 0; 1162 uchar_t vis; 1163 1164 sdp = sav->sav_symdesc; 1165 1166 /* 1167 * If undefined symbols are allowed ignore any symbols that are 1168 * not needed. 1169 */ 1170 if (!(oflags & FLG_OF_NOUNDEF) && 1171 (sdp->sd_ref == REF_DYN_SEEN)) 1172 continue; 1173 1174 /* 1175 * If the symbol originates from an external or parent mapfile 1176 * reference and hasn't been matched to a reference from a 1177 * relocatable object, ignore it. 1178 */ 1179 if ((sdp->sd_flags & (FLG_SY_EXTERN | FLG_SY_PARENT)) && 1180 ((sdp->sd_flags & FLG_SY_MAPUSED) == 0)) { 1181 sdp->sd_flags |= FLG_SY_INVALID; 1182 continue; 1183 } 1184 1185 sym = sdp->sd_sym; 1186 type = ELF_ST_TYPE(sym->st_info); 1187 1188 /* 1189 * Sanity check TLS. 1190 */ 1191 if ((type == STT_TLS) && (sym->st_size != 0) && 1192 (sym->st_shndx != SHN_UNDEF) && 1193 (sym->st_shndx != SHN_COMMON)) { 1194 Is_desc *isp = sdp->sd_isc; 1195 Ifl_desc *ifl = sdp->sd_file; 1196 1197 if ((isp == NULL) || (isp->is_shdr == NULL) || 1198 ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) { 1199 eprintf(ofl->ofl_lml, ERR_FATAL, 1200 MSG_INTL(MSG_SYM_TLS), 1201 demangle(sdp->sd_name), ifl->ifl_name); 1202 ofl->ofl_flags |= FLG_OF_FATAL; 1203 continue; 1204 } 1205 } 1206 1207 if ((sdp->sd_flags & FLG_SY_VISIBLE) == 0) 1208 ld_sym_adjust_vis(sdp, ofl); 1209 1210 if ((sdp->sd_flags & FLG_SY_REDUCED) && 1211 (oflags & FLG_OF_PROCRED)) { 1212 DBG_CALL(Dbg_syms_reduce(ofl, DBG_SYM_REDUCE_GLOBAL, 1213 sdp, 0, 0)); 1214 } 1215 1216 /* 1217 * Record any STV_SINGLETON existence. 1218 */ 1219 if ((vis = ELF_ST_VISIBILITY(sym->st_other)) == STV_SINGLETON) 1220 ofl->ofl_dtflags_1 |= DF_1_SINGLETON; 1221 1222 /* 1223 * If building a shared object or executable, and this is a 1224 * non-weak UNDEF symbol with reduced visibility (STV_*), then 1225 * give a fatal error. 1226 */ 1227 if (((oflags & FLG_OF_RELOBJ) == 0) && 1228 (sym->st_shndx == SHN_UNDEF) && 1229 (ELF_ST_BIND(sym->st_info) != STB_WEAK)) { 1230 if (vis && (vis != STV_SINGLETON)) { 1231 sym_undef_entry(ofl, sdp, BNDLOCAL); 1232 ofl->ofl_flags |= FLG_OF_FATAL; 1233 continue; 1234 } 1235 } 1236 1237 /* 1238 * If this symbol is defined in a non-allocatable section, 1239 * reduce it to local symbol. 1240 */ 1241 if (((isp = sdp->sd_isc) != 0) && isp->is_shdr && 1242 ((isp->is_shdr->sh_flags & SHF_ALLOC) == 0)) { 1243 sdp->sd_flags |= FLG_SY_REDUCED; 1244 sdp->sd_flags1 |= FLG_SY1_HIDDEN; 1245 } 1246 1247 /* 1248 * If this symbol originated as a SHN_SUNW_IGNORE, it will have 1249 * been processed as an SHN_UNDEF. Return the symbol to its 1250 * original index for validation, and propagation to the output 1251 * file. 1252 */ 1253 if (sdp->sd_flags1 & FLG_SY1_IGNORE) 1254 sdp->sd_shndx = SHN_SUNW_IGNORE; 1255 1256 if (undef) { 1257 /* 1258 * If a non-weak reference remains undefined, or if a 1259 * mapfile reference is not bound to the relocatable 1260 * objects that make up the object being built, we have 1261 * a fatal error. 1262 * 1263 * The exceptions are symbols which are defined to be 1264 * found in the parent (FLG_SY_PARENT), which is really 1265 * only meaningful for direct binding, or are defined 1266 * external (FLG_SY_EXTERN) so as to suppress -zdefs 1267 * errors. 1268 * 1269 * Register symbols are always allowed to be UNDEF. 1270 * 1271 * Note that we don't include references created via -u 1272 * in the same shared object binding test. This is for 1273 * backward compatibility, in that a number of archive 1274 * makefile rules used -u to cause archive extraction. 1275 * These same rules have been cut and pasted to apply 1276 * to shared objects, and thus although the -u reference 1277 * is redundant, flagging it as fatal could cause some 1278 * build to fail. Also we have documented the use of 1279 * -u as a mechanism to cause binding to weak version 1280 * definitions, thus giving users an error condition 1281 * would be incorrect. 1282 */ 1283 if (!(sdp->sd_flags & FLG_SY_REGSYM) && 1284 ((sym->st_shndx == SHN_UNDEF) && 1285 ((ELF_ST_BIND(sym->st_info) != STB_WEAK) && 1286 ((sdp->sd_flags & 1287 (FLG_SY_PARENT | FLG_SY_EXTERN)) == 0)) || 1288 (((sdp->sd_flags & 1289 (FLG_SY_MAPREF | FLG_SY_MAPUSED)) == 1290 FLG_SY_MAPREF) && 1291 ((sdp->sd_flags1 & (FLG_SY1_HIDDEN | 1292 FLG_SY1_PROTECT)) == 0)))) { 1293 sym_undef_entry(ofl, sdp, UNDEF); 1294 ofl->ofl_flags |= undef; 1295 undeferr = 1; 1296 } 1297 1298 } else { 1299 /* 1300 * For building things like shared objects (or anything 1301 * -znodefs), undefined symbols are allowed. 1302 * 1303 * If a mapfile reference remains undefined the user 1304 * would probably like a warning at least (they've 1305 * usually mis-spelt the reference). Refer to the above 1306 * comments for discussion on -u references, which 1307 * are not tested for in the same manner. 1308 */ 1309 if ((sdp->sd_flags & 1310 (FLG_SY_MAPREF | FLG_SY_MAPUSED)) == 1311 FLG_SY_MAPREF) { 1312 sym_undef_entry(ofl, sdp, UNDEF); 1313 ofl->ofl_flags |= FLG_OF_WARN; 1314 undeferr = 1; 1315 } 1316 } 1317 1318 /* 1319 * If this symbol comes from a dependency mark the dependency 1320 * as required (-z ignore can result in unused dependencies 1321 * being dropped). If we need to record dependency versioning 1322 * information indicate what version of the needed shared object 1323 * this symbol is part of. Flag the symbol as undefined if it 1324 * has not been made available to us. 1325 */ 1326 if ((sdp->sd_ref == REF_DYN_NEED) && 1327 (!(sdp->sd_flags & FLG_SY_REFRSD))) { 1328 sdp->sd_file->ifl_flags |= FLG_IF_DEPREQD; 1329 1330 /* 1331 * Capture that we've bound to a symbol that doesn't 1332 * allow being directly bound to. 1333 */ 1334 if (sdp->sd_flags1 & FLG_SY1_NDIR) 1335 ofl->ofl_flags1 |= FLG_OF1_NDIRECT; 1336 1337 if (sdp->sd_file->ifl_vercnt) { 1338 int vndx; 1339 Ver_index *vip; 1340 1341 vndx = sdp->sd_aux->sa_dverndx; 1342 vip = &sdp->sd_file->ifl_verndx[vndx]; 1343 if (vip->vi_flags & FLG_VER_AVAIL) { 1344 vip->vi_flags |= FLG_VER_REFER; 1345 } else { 1346 sym_undef_entry(ofl, sdp, NOTAVAIL); 1347 ofl->ofl_flags |= FLG_OF_FATAL; 1348 continue; 1349 } 1350 } 1351 } 1352 1353 /* 1354 * Test that we do not bind to symbol supplied from an implicit 1355 * shared object. If a binding is from a weak reference it can 1356 * be ignored. 1357 */ 1358 if (needed && !undeferr && (sdp->sd_flags & FLG_SY_GLOBREF) && 1359 (sdp->sd_ref == REF_DYN_NEED) && 1360 (sdp->sd_flags & FLG_SY_NOTAVAIL)) { 1361 sym_undef_entry(ofl, sdp, IMPLICIT); 1362 ofl->ofl_flags |= needed; 1363 continue; 1364 } 1365 1366 /* 1367 * Test that a symbol isn't going to be reduced to local scope 1368 * which actually wants to bind to a shared object - if so it's 1369 * a fatal error. 1370 */ 1371 if ((sdp->sd_ref == REF_DYN_NEED) && 1372 (sdp->sd_flags1 & (FLG_SY1_HIDDEN | FLG_SY1_PROTECT))) { 1373 sym_undef_entry(ofl, sdp, BNDLOCAL); 1374 ofl->ofl_flags |= FLG_OF_FATAL; 1375 continue; 1376 } 1377 1378 /* 1379 * If the output image is to be versioned then all symbol 1380 * definitions must be associated with a version. 1381 */ 1382 if (verdesc && (sdp->sd_ref == REF_REL_NEED) && 1383 (sym->st_shndx != SHN_UNDEF) && 1384 (!(sdp->sd_flags1 & FLG_SY1_HIDDEN)) && 1385 (sdp->sd_aux->sa_overndx == 0)) { 1386 sym_undef_entry(ofl, sdp, NOVERSION); 1387 ofl->ofl_flags |= verdesc; 1388 continue; 1389 } 1390 1391 /* 1392 * If we don't need the symbol there's no need to process it 1393 * any further. 1394 */ 1395 if (sdp->sd_ref == REF_DYN_SEEN) 1396 continue; 1397 1398 /* 1399 * Calculate the size and alignment requirements for the global 1400 * .bss and .tls sections. If we're building a relocatable 1401 * object only account for scoped COMMON symbols (these will 1402 * be converted to .bss references). 1403 * 1404 * When -z nopartial is in effect, partially initialized 1405 * symbols are directed to the special .data section 1406 * created for that purpose (ofl->ofl_isparexpn). 1407 * Otherwise, partially initialized symbols go to .bss. 1408 * 1409 * Also refer to make_mvsections() in sunwmove.c 1410 */ 1411 if ((sym->st_shndx == SHN_COMMON) && 1412 (((oflags & FLG_OF_RELOBJ) == 0) || 1413 ((sdp->sd_flags1 & FLG_SY1_HIDDEN) && 1414 (oflags & FLG_OF_PROCRED)))) { 1415 if ((sdp->sd_psyminfo == 0) || 1416 ((sdp->sd_flags & FLG_SY_PAREXPN) == 0)) { 1417 Xword * size, * align; 1418 1419 if (type != STT_TLS) { 1420 size = &bsssize; 1421 align = &bssalign; 1422 } else { 1423 size = &tlssize; 1424 align = &tlsalign; 1425 } 1426 *size = (Xword)S_ROUND(*size, sym->st_value) + 1427 sym->st_size; 1428 if (sym->st_value > *align) 1429 *align = sym->st_value; 1430 } 1431 } 1432 1433 #if defined(_ELF64) 1434 /* 1435 * Calculate the size and alignment requirement for the global 1436 * .lbss. TLS or partially initialized symbols do not need to be 1437 * considered yet. 1438 */ 1439 if ((ld_targ.t_m.m_mach == EM_AMD64) && 1440 (sym->st_shndx == SHN_X86_64_LCOMMON)) { 1441 lbsssize = (Xword)S_ROUND(lbsssize, sym->st_value) + 1442 sym->st_size; 1443 if (sym->st_value > lbssalign) 1444 lbssalign = sym->st_value; 1445 } 1446 #endif 1447 1448 /* 1449 * If a symbol was referenced via the command line 1450 * (ld -u <>, ...), then this counts as a reference against the 1451 * symbol. Mark any section that symbol is defined in. 1452 */ 1453 if (((isp = sdp->sd_isc) != 0) && 1454 (sdp->sd_flags & FLG_SY_CMDREF)) { 1455 isp->is_flags |= FLG_IS_SECTREF; 1456 isp->is_file->ifl_flags |= FLG_IF_FILEREF; 1457 } 1458 1459 /* 1460 * Update the symbol count and the associated name string size. 1461 */ 1462 if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) && 1463 (oflags & FLG_OF_PROCRED)) { 1464 /* 1465 * If any reductions are being processed, keep a count 1466 * of eliminated symbols, and if the symbol is being 1467 * reduced to local, count it's size for the .symtab. 1468 */ 1469 if (sdp->sd_flags1 & FLG_SY1_ELIM) { 1470 ofl->ofl_elimcnt++; 1471 } else { 1472 ofl->ofl_scopecnt++; 1473 if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) || 1474 sym->st_name) && (st_insert(ofl->ofl_strtab, 1475 sdp->sd_name) == -1)) 1476 return (S_ERROR); 1477 if (allow_ldynsym && sym->st_name && 1478 ldynsym_symtype[type]) { 1479 ofl->ofl_dynscopecnt++; 1480 if (st_insert(ofl->ofl_dynstrtab, 1481 sdp->sd_name) == -1) 1482 return (S_ERROR); 1483 /* Include it in sort section? */ 1484 DYNSORT_COUNT(sdp, sym, type, ++); 1485 } 1486 } 1487 } else { 1488 ofl->ofl_globcnt++; 1489 1490 /* 1491 * Check to see if this global variable should 1492 * go into a sort section. Sort sections require 1493 * a .SUNW_ldynsym section, so, don't check 1494 * unless a .SUNW_ldynsym is allowed. 1495 */ 1496 if (allow_ldynsym) { 1497 DYNSORT_COUNT(sdp, sym, type, ++); 1498 } 1499 1500 /* 1501 * If global direct bindings are in effect, or this 1502 * symbol has bound to a dependency which was specified 1503 * as requiring direct bindings, and it hasn't 1504 * explicitly been defined as a non-direct binding 1505 * symbol, mark it. 1506 */ 1507 if (((ofl->ofl_dtflags_1 & DF_1_DIRECT) || (isp && 1508 (isp->is_file->ifl_flags & FLG_IF_DIRECT))) && 1509 ((sdp->sd_flags1 & FLG_SY1_NDIR) == 0)) 1510 sdp->sd_flags1 |= FLG_SY1_DIR; 1511 1512 /* 1513 * Insert the symbol name. 1514 */ 1515 if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) || 1516 sym->st_name) { 1517 if (st_insert(ofl->ofl_strtab, 1518 sdp->sd_name) == -1) 1519 return (S_ERROR); 1520 1521 if (!(ofl->ofl_flags & FLG_OF_RELOBJ) && 1522 (st_insert(ofl->ofl_dynstrtab, 1523 sdp->sd_name) == -1)) 1524 return (S_ERROR); 1525 } 1526 1527 /* 1528 * If this section offers a global symbol - record that 1529 * fact. 1530 */ 1531 if (isp) { 1532 isp->is_flags |= FLG_IS_SECTREF; 1533 isp->is_file->ifl_flags |= FLG_IF_FILEREF; 1534 } 1535 } 1536 } 1537 1538 /* 1539 * If we've encountered a fatal error during symbol validation then 1540 * return now. 1541 */ 1542 if (ofl->ofl_flags & FLG_OF_FATAL) 1543 return (1); 1544 1545 /* 1546 * Now that symbol resolution is completed, scan any register symbols. 1547 * From now on, we're only interested in those that contribute to the 1548 * output file. 1549 */ 1550 if (ofl->ofl_regsyms) { 1551 int ndx; 1552 1553 for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) { 1554 if ((sdp = ofl->ofl_regsyms[ndx]) == NULL) 1555 continue; 1556 if (sdp->sd_ref != REF_REL_NEED) { 1557 ofl->ofl_regsyms[ndx] = NULL; 1558 continue; 1559 } 1560 1561 ofl->ofl_regsymcnt++; 1562 if (sdp->sd_sym->st_name == 0) 1563 sdp->sd_name = MSG_ORIG(MSG_STR_EMPTY); 1564 1565 if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) || 1566 (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL)) 1567 ofl->ofl_lregsymcnt++; 1568 } 1569 } 1570 1571 /* 1572 * Generate the .bss section now that we know its size and alignment. 1573 */ 1574 if (bsssize || !(oflags & FLG_OF_RELOBJ)) { 1575 if (ld_make_bss(ofl, bsssize, bssalign, MAKE_BSS) == S_ERROR) 1576 return (S_ERROR); 1577 } 1578 if (tlssize) { 1579 if (ld_make_bss(ofl, tlssize, tlsalign, MAKE_TLS) == S_ERROR) 1580 return (S_ERROR); 1581 } 1582 #if defined(_ELF64) 1583 if ((ld_targ.t_m.m_mach == EM_AMD64) && 1584 lbsssize && !(oflags & FLG_OF_RELOBJ)) { 1585 if (ld_make_bss(ofl, lbsssize, lbssalign, MAKE_LBSS) == S_ERROR) 1586 return (S_ERROR); 1587 } 1588 #endif 1589 1590 /* 1591 * Determine what entry point symbol we need, and if found save its 1592 * symbol descriptor so that we can update the ELF header entry with the 1593 * symbols value later (see update_oehdr). Make sure the symbol is 1594 * tagged to ensure its update in case -s is in effect. Use any -e 1595 * option first, or the default entry points `_start' and `main'. 1596 */ 1597 ret = 0; 1598 if (ofl->ofl_entry) { 1599 if ((sdp = 1600 ld_sym_find(ofl->ofl_entry, SYM_NOHASH, 0, ofl)) == NULL) { 1601 eprintf(ofl->ofl_lml, ERR_FATAL, 1602 MSG_INTL(MSG_ARG_NOENTRY), ofl->ofl_entry); 1603 ret++; 1604 } else if (ensure_sym_local(ofl, sdp, 1605 MSG_INTL(MSG_SYM_ENTRY)) != 0) { 1606 ret++; 1607 } else { 1608 ofl->ofl_entry = (void *)sdp; 1609 } 1610 } else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_START), 1611 SYM_NOHASH, 0, ofl)) != NULL) && (ensure_sym_local(ofl, 1612 sdp, 0) == 0)) { 1613 ofl->ofl_entry = (void *)sdp; 1614 1615 } else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_MAIN), 1616 SYM_NOHASH, 0, ofl)) != NULL) && (ensure_sym_local(ofl, 1617 sdp, 0) == 0)) { 1618 ofl->ofl_entry = (void *)sdp; 1619 } 1620 1621 /* 1622 * If ld -zdtrace=<sym> was given, then validate that the symbol is 1623 * defined within the current object being built. 1624 */ 1625 if ((sdp = ofl->ofl_dtracesym) != 0) 1626 ret += ensure_sym_local(ofl, sdp, MSG_ORIG(MSG_STR_DTRACE)); 1627 1628 /* 1629 * If any initarray, finiarray or preinitarray functions have been 1630 * requested, make sure they are defined within the current object 1631 * being built. 1632 */ 1633 if (ofl->ofl_initarray.head) { 1634 ret += ensure_array_local(ofl, &ofl->ofl_initarray, 1635 MSG_ORIG(MSG_SYM_INITARRAY)); 1636 } 1637 if (ofl->ofl_finiarray.head) { 1638 ret += ensure_array_local(ofl, &ofl->ofl_finiarray, 1639 MSG_ORIG(MSG_SYM_FINIARRAY)); 1640 } 1641 if (ofl->ofl_preiarray.head) { 1642 ret += ensure_array_local(ofl, &ofl->ofl_preiarray, 1643 MSG_ORIG(MSG_SYM_PREINITARRAY)); 1644 } 1645 1646 if (ret) 1647 return (S_ERROR); 1648 1649 /* 1650 * If we're required to record any needed dependencies versioning 1651 * information calculate it now that all symbols have been validated. 1652 */ 1653 if ((oflags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED) 1654 return (ld_vers_check_need(ofl)); 1655 else 1656 return (1); 1657 } 1658 1659 /* 1660 * qsort(3c) comparison function. As an optimization for associating weak 1661 * symbols to their strong counterparts sort global symbols according to their 1662 * section index, address and binding. 1663 */ 1664 static int 1665 compare(const void *sdpp1, const void *sdpp2) 1666 { 1667 Sym_desc *sdp1 = *((Sym_desc **)sdpp1); 1668 Sym_desc *sdp2 = *((Sym_desc **)sdpp2); 1669 Sym *sym1, *sym2; 1670 uchar_t bind1, bind2; 1671 1672 /* 1673 * Symbol descriptors may be zero, move these to the front of the 1674 * sorted array. 1675 */ 1676 if (sdp1 == NULL) 1677 return (-1); 1678 if (sdp2 == NULL) 1679 return (1); 1680 1681 sym1 = sdp1->sd_sym; 1682 sym2 = sdp2->sd_sym; 1683 1684 /* 1685 * Compare the symbols section index. This is important when sorting 1686 * the symbol tables of relocatable objects. In this case, a symbols 1687 * value is the offset within the associated section, and thus many 1688 * symbols can have the same value, but are effectively different 1689 * addresses. 1690 */ 1691 if (sym1->st_shndx > sym2->st_shndx) 1692 return (1); 1693 if (sym1->st_shndx < sym2->st_shndx) 1694 return (-1); 1695 1696 /* 1697 * Compare the symbols value (address). 1698 */ 1699 if (sym1->st_value > sym2->st_value) 1700 return (1); 1701 if (sym1->st_value < sym2->st_value) 1702 return (-1); 1703 1704 bind1 = ELF_ST_BIND(sym1->st_info); 1705 bind2 = ELF_ST_BIND(sym2->st_info); 1706 1707 /* 1708 * If two symbols have the same address place the weak symbol before 1709 * any strong counterpart. 1710 */ 1711 if (bind1 > bind2) 1712 return (-1); 1713 if (bind1 < bind2) 1714 return (1); 1715 1716 return (0); 1717 } 1718 1719 /* 1720 * Issue a MSG_SYM_BADADDR error from ld_sym_process(). This error 1721 * is issued when a symbol address/size is not contained by the 1722 * target section. 1723 * 1724 * Such objects are at least partially corrupt, and the user would 1725 * be well advised to be skeptical of them, and to ask their compiler 1726 * supplier to fix the problem. However, a distinction needs to be 1727 * made between symbols that reference readonly text, and those that 1728 * access writable data. Other than throwing off profiling results, 1729 * the readonly section case is less serious. We have encountered 1730 * such objects in the field. In order to allow existing objects 1731 * to continue working, we issue a warning rather than a fatal error 1732 * if the symbol is against readonly text. Other cases are fatal. 1733 */ 1734 static void 1735 issue_badaddr_msg(Ifl_desc *ifl, Ofl_desc *ofl, Sym_desc *sdp, 1736 Sym *sym, Word shndx) 1737 { 1738 ofl_flag_t flag; 1739 Error err; 1740 const char *msg; 1741 1742 if ((sdp->sd_isc->is_shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == 1743 SHF_ALLOC) { 1744 msg = MSG_INTL(MSG_SYM_BADADDR_ROTXT); 1745 flag = FLG_OF_WARN; 1746 err = ERR_WARNING; 1747 } else { 1748 msg = MSG_INTL(MSG_SYM_BADADDR); 1749 flag = FLG_OF_FATAL; 1750 err = ERR_FATAL; 1751 } 1752 1753 eprintf(ofl->ofl_lml, err, msg, demangle(sdp->sd_name), 1754 ifl->ifl_name, shndx, sdp->sd_isc->is_name, 1755 EC_XWORD(sdp->sd_isc->is_shdr->sh_size), 1756 EC_XWORD(sym->st_value), EC_XWORD(sym->st_size)); 1757 ofl->ofl_flags |= flag; 1758 } 1759 1760 1761 /* 1762 * Process the symbol table for the specified input file. At this point all 1763 * input sections from this input file have been assigned an input section 1764 * descriptor which is saved in the `ifl_isdesc' array. 1765 * 1766 * - local symbols are saved (as is) if the input file is a 1767 * relocatable object 1768 * 1769 * - global symbols are added to the linkers internal symbol 1770 * table if they are not already present, otherwise a symbol 1771 * resolution function is called upon to resolve the conflict. 1772 */ 1773 uintptr_t 1774 ld_sym_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl) 1775 { 1776 /* 1777 * This macro tests the given symbol to see if it is out of 1778 * range relative to the section it references. 1779 * 1780 * entry: 1781 * - ifl is a relative object (ET_REL) 1782 * _sdp - Symbol descriptor 1783 * _sym - Symbol 1784 * _type - Symbol type 1785 * 1786 * The following are tested: 1787 * - Symbol length is non-zero 1788 * - Symbol type is a type that references code or data 1789 * - Referenced section is not 0 (indicates an UNDEF symbol) 1790 * and is not in the range of special values above SHN_LORESERVE 1791 * (excluding SHN_XINDEX, which is OK). 1792 * - We have a valid section header for the target section 1793 * 1794 * If the above are all true, and the symbol position is not 1795 * contained by the target section, this macro evaluates to 1796 * True (1). Otherwise, False(0). 1797 */ 1798 #define SYM_LOC_BADADDR(_sdp, _sym, _type) \ 1799 (_sym->st_size && dynsymsort_symtype[_type] && \ 1800 (_sym->st_shndx != SHN_UNDEF) && \ 1801 ((_sym->st_shndx < SHN_LORESERVE) || \ 1802 (_sym->st_shndx == SHN_XINDEX)) && \ 1803 _sdp->sd_isc && _sdp->sd_isc->is_shdr && \ 1804 ((_sym->st_value + _sym->st_size) > _sdp->sd_isc->is_shdr->sh_size)) 1805 1806 Conv_inv_buf_t inv_buf; 1807 Sym *sym = (Sym *)isc->is_indata->d_buf; 1808 Word *symshndx = NULL; 1809 Shdr *shdr = isc->is_shdr; 1810 Sym_desc *sdp; 1811 size_t strsize; 1812 char *strs; 1813 uchar_t type, bind; 1814 Word ndx, hash, local, total; 1815 Half etype = ifl->ifl_ehdr->e_type; 1816 int etype_rel; 1817 const char *symsecname, *strsecname; 1818 avl_index_t where; 1819 int test_gnu_hidden_bit, weak; 1820 1821 /* 1822 * Its possible that a file may contain more that one symbol table, 1823 * ie. .dynsym and .symtab in a shared library. Only process the first 1824 * table (here, we assume .dynsym comes before .symtab). 1825 */ 1826 if (ifl->ifl_symscnt) 1827 return (1); 1828 1829 if (isc->is_symshndx) 1830 symshndx = isc->is_symshndx->is_indata->d_buf; 1831 1832 DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl)); 1833 1834 if (isc->is_name) 1835 symsecname = isc->is_name; 1836 else 1837 symsecname = MSG_ORIG(MSG_STR_EMPTY); 1838 1839 /* 1840 * From the symbol tables section header information determine which 1841 * strtab table is needed to locate the actual symbol names. 1842 */ 1843 if (ifl->ifl_flags & FLG_IF_HSTRTAB) { 1844 ndx = shdr->sh_link; 1845 if ((ndx == 0) || (ndx >= ifl->ifl_shnum)) { 1846 eprintf(ofl->ofl_lml, ERR_FATAL, 1847 MSG_INTL(MSG_FIL_INVSHLINK), 1848 ifl->ifl_name, symsecname, EC_XWORD(ndx)); 1849 return (S_ERROR); 1850 } 1851 strsize = ifl->ifl_isdesc[ndx]->is_shdr->sh_size; 1852 strs = ifl->ifl_isdesc[ndx]->is_indata->d_buf; 1853 if (ifl->ifl_isdesc[ndx]->is_name) 1854 strsecname = ifl->ifl_isdesc[ndx]->is_name; 1855 else 1856 strsecname = MSG_ORIG(MSG_STR_EMPTY); 1857 } else { 1858 /* 1859 * There is no string table section in this input file 1860 * although there are symbols in this symbol table section. 1861 * This means that these symbols do not have names. 1862 * Currently, only scratch register symbols are allowed 1863 * not to have names. 1864 */ 1865 strsize = 0; 1866 strs = (char *)MSG_ORIG(MSG_STR_EMPTY); 1867 strsecname = MSG_ORIG(MSG_STR_EMPTY); 1868 } 1869 1870 /* 1871 * Determine the number of local symbols together with the total 1872 * number we have to process. 1873 */ 1874 total = (Word)(shdr->sh_size / shdr->sh_entsize); 1875 local = shdr->sh_info; 1876 1877 /* 1878 * Allocate a symbol table index array and a local symbol array 1879 * (global symbols are processed and added to the ofl->ofl_symbkt[] 1880 * array). If we are dealing with a relocatable object, allocate the 1881 * local symbol descriptors. If this isn't a relocatable object we 1882 * still have to process any shared object locals to determine if any 1883 * register symbols exist. Although these aren't added to the output 1884 * image, they are used as part of symbol resolution. 1885 */ 1886 if ((ifl->ifl_oldndx = libld_malloc((size_t)(total * 1887 sizeof (Sym_desc *)))) == NULL) 1888 return (S_ERROR); 1889 etype_rel = (etype == ET_REL); 1890 if (etype_rel && local) { 1891 if ((ifl->ifl_locs = 1892 libld_calloc(sizeof (Sym_desc), local)) == NULL) 1893 return (S_ERROR); 1894 /* LINTED */ 1895 ifl->ifl_locscnt = (Word)local; 1896 } 1897 ifl->ifl_symscnt = total; 1898 1899 /* 1900 * If there are local symbols to save add them to the symbol table 1901 * index array. 1902 */ 1903 if (local) { 1904 int allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl); 1905 Sym_desc *last_file_sdp = NULL; 1906 int last_file_ndx = 0; 1907 1908 for (sym++, ndx = 1; ndx < local; sym++, ndx++) { 1909 Word shndx, sdflags = FLG_SY_CLEAN; 1910 const char *name; 1911 Sym_desc *rsdp; 1912 int shndx_bad = 0; 1913 1914 /* 1915 * Determine and validate the associated section index. 1916 */ 1917 if (symshndx && (sym->st_shndx == SHN_XINDEX)) { 1918 shndx = symshndx[ndx]; 1919 } else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) { 1920 sdflags |= FLG_SY_SPECSEC; 1921 } else if (shndx > ifl->ifl_ehdr->e_shnum) { 1922 /* We need the name before we can issue error */ 1923 shndx_bad = 1; 1924 } 1925 1926 /* 1927 * Check if st_name has a valid value or not. 1928 */ 1929 if ((name = string(ofl, ifl, sym, strs, strsize, ndx, 1930 shndx, symsecname, strsecname, &sdflags)) == NULL) { 1931 ofl->ofl_flags |= FLG_OF_FATAL; 1932 continue; 1933 } 1934 1935 /* 1936 * Now that we have the name, if the section index 1937 * was bad, report it. 1938 */ 1939 if (shndx_bad) { 1940 eprintf(ofl->ofl_lml, ERR_WARNING, 1941 MSG_INTL(MSG_SYM_INVSHNDX), 1942 demangle_symname(name, isc->is_name, ndx), 1943 ifl->ifl_name, 1944 conv_sym_shndx(sym->st_shndx, &inv_buf)); 1945 continue; 1946 } 1947 1948 /* 1949 * If this local symbol table originates from a shared 1950 * object, then we're only interested in recording 1951 * register symbols. As local symbol descriptors aren't 1952 * allocated for shared objects, one will be allocated 1953 * to associated with the register symbol. This symbol 1954 * won't become part of the output image, but we must 1955 * process it to test for register conflicts. 1956 */ 1957 rsdp = sdp = 0; 1958 if (sdflags & FLG_SY_REGSYM) { 1959 /* 1960 * The presence of FLG_SY_REGSYM means that 1961 * the pointers in ld_targ.t_ms are non-NULL. 1962 */ 1963 rsdp = (*ld_targ.t_ms.ms_reg_find)(sym, ofl); 1964 if (rsdp != 0) { 1965 /* 1966 * The fact that another register def- 1967 * inition has been found is fatal. 1968 * Call the verification routine to get 1969 * the error message and move on. 1970 */ 1971 (void) (*ld_targ.t_ms.ms_reg_check) 1972 (rsdp, sym, name, ifl, ofl); 1973 continue; 1974 } 1975 1976 if (etype == ET_DYN) { 1977 if ((sdp = libld_calloc( 1978 sizeof (Sym_desc), 1)) == NULL) 1979 return (S_ERROR); 1980 sdp->sd_ref = REF_DYN_SEEN; 1981 1982 /* Will not appear in output object */ 1983 ofl->ofl_locscnt--; 1984 } 1985 } else if (etype == ET_DYN) 1986 continue; 1987 1988 /* 1989 * Fill in the remaining symbol descriptor information. 1990 */ 1991 if (sdp == NULL) { 1992 sdp = &(ifl->ifl_locs[ndx]); 1993 sdp->sd_ref = REF_REL_NEED; 1994 } 1995 if (rsdp == NULL) { 1996 sdp->sd_name = name; 1997 sdp->sd_sym = sym; 1998 sdp->sd_shndx = shndx; 1999 sdp->sd_flags = sdflags; 2000 sdp->sd_file = ifl; 2001 ifl->ifl_oldndx[ndx] = sdp; 2002 } 2003 2004 DBG_CALL(Dbg_syms_entry(ofl->ofl_lml, ndx, sdp)); 2005 2006 /* 2007 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF 2008 * so as to simplify future processing. 2009 */ 2010 if (sym->st_shndx == SHN_SUNW_IGNORE) { 2011 sdp->sd_shndx = shndx = SHN_UNDEF; 2012 sdp->sd_flags1 |= 2013 (FLG_SY1_IGNORE | FLG_SY1_ELIM); 2014 } 2015 2016 /* 2017 * Process any register symbols. 2018 */ 2019 if (sdp->sd_flags & FLG_SY_REGSYM) { 2020 /* 2021 * Add a diagnostic to indicate we've caught a 2022 * register symbol, as this can be useful if a 2023 * register conflict is later discovered. 2024 */ 2025 DBG_CALL(Dbg_syms_entered(ofl, sym, sdp)); 2026 2027 /* 2028 * If this register symbol hasn't already been 2029 * recorded, enter it now. 2030 * 2031 * The presence of FLG_SY_REGSYM means that 2032 * the pointers in ld_targ.t_ms are non-NULL. 2033 */ 2034 if ((rsdp == NULL) && 2035 ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) == 2036 0)) 2037 return (S_ERROR); 2038 } 2039 2040 /* 2041 * Assign an input section. 2042 */ 2043 if ((sym->st_shndx != SHN_UNDEF) && 2044 ((sdp->sd_flags & FLG_SY_SPECSEC) == 0)) 2045 sdp->sd_isc = ifl->ifl_isdesc[shndx]; 2046 2047 /* 2048 * If this symbol falls within the range of a section 2049 * being discarded, then discard the symbol itself. 2050 * There is no reason to keep this local symbol. 2051 */ 2052 if (sdp->sd_isc && 2053 (sdp->sd_isc->is_flags & FLG_IS_DISCARD)) { 2054 sdp->sd_flags |= FLG_SY_ISDISC; 2055 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp)); 2056 continue; 2057 } 2058 2059 /* 2060 * Skip any section symbols as new versions of these 2061 * will be created. 2062 */ 2063 if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION) { 2064 if (sym->st_shndx == SHN_UNDEF) { 2065 eprintf(ofl->ofl_lml, ERR_WARNING, 2066 MSG_INTL(MSG_SYM_INVSHNDX), 2067 demangle_symname(name, isc->is_name, 2068 ndx), ifl->ifl_name, 2069 conv_sym_shndx(sym->st_shndx, 2070 &inv_buf)); 2071 } 2072 continue; 2073 } 2074 2075 /* 2076 * For a relocatable object, if this symbol is defined 2077 * and has non-zero length and references an address 2078 * within an associated section, then check its extents 2079 * to make sure the section boundaries encompass it. 2080 * If they don't, the ELF file is corrupt. 2081 */ 2082 if (etype_rel) { 2083 if (SYM_LOC_BADADDR(sdp, sym, type)) { 2084 issue_badaddr_msg(ifl, ofl, sdp, 2085 sym, shndx); 2086 if (ofl->ofl_flags & FLG_OF_FATAL) 2087 continue; 2088 } 2089 2090 /* 2091 * We have observed relocatable objects 2092 * containing identical adjacent STT_FILE 2093 * symbols. Discard any other than the first, 2094 * as they are all equivalent and the extras 2095 * do not add information. 2096 * 2097 * For the purpose of this test, we assume 2098 * that only the symbol type and the string 2099 * table offset (st_name) matter. 2100 */ 2101 if (type == STT_FILE) { 2102 int toss = (last_file_sdp != NULL) && 2103 ((ndx - 1) == last_file_ndx) && 2104 (sym->st_name == 2105 last_file_sdp->sd_sym->st_name); 2106 2107 last_file_sdp = sdp; 2108 last_file_ndx = ndx; 2109 if (toss) { 2110 sdp->sd_flags |= FLG_SY_INVALID; 2111 DBG_CALL(Dbg_syms_dup_discarded( 2112 ofl->ofl_lml, ndx, sdp)); 2113 continue; 2114 } 2115 } 2116 } 2117 2118 2119 /* 2120 * Sanity check for TLS 2121 */ 2122 if ((sym->st_size != 0) && ((type == STT_TLS) && 2123 (sym->st_shndx != SHN_COMMON))) { 2124 Is_desc *isp = sdp->sd_isc; 2125 2126 if ((isp == NULL) || (isp->is_shdr == NULL) || 2127 ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) { 2128 eprintf(ofl->ofl_lml, ERR_FATAL, 2129 MSG_INTL(MSG_SYM_TLS), 2130 demangle(sdp->sd_name), 2131 ifl->ifl_name); 2132 ofl->ofl_flags |= FLG_OF_FATAL; 2133 continue; 2134 } 2135 } 2136 2137 /* 2138 * Carry our some basic sanity checks (these are just 2139 * some of the erroneous symbol entries we've come 2140 * across, there's probably a lot more). The symbol 2141 * will not be carried forward to the output file, which 2142 * won't be a problem unless a relocation is required 2143 * against it. 2144 */ 2145 if (((sdp->sd_flags & FLG_SY_SPECSEC) && 2146 ((sym->st_shndx == SHN_COMMON)) || 2147 ((type == STT_FILE) && 2148 (sym->st_shndx != SHN_ABS))) || 2149 (sdp->sd_isc && (sdp->sd_isc->is_osdesc == NULL))) { 2150 eprintf(ofl->ofl_lml, ERR_WARNING, 2151 MSG_INTL(MSG_SYM_INVSHNDX), 2152 demangle_symname(name, isc->is_name, ndx), 2153 ifl->ifl_name, 2154 conv_sym_shndx(sym->st_shndx, &inv_buf)); 2155 sdp->sd_isc = NULL; 2156 sdp->sd_flags |= FLG_SY_INVALID; 2157 continue; 2158 } 2159 2160 /* 2161 * As these local symbols will become part of the output 2162 * image, record their number and name string size. 2163 * Globals are counted after all input file processing 2164 * (and hence symbol resolution) is complete during 2165 * sym_validate(). 2166 */ 2167 if (!(ofl->ofl_flags & FLG_OF_REDLSYM)) { 2168 ofl->ofl_locscnt++; 2169 2170 if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) || 2171 sym->st_name) && (st_insert(ofl->ofl_strtab, 2172 sdp->sd_name) == -1)) 2173 return (S_ERROR); 2174 2175 if (allow_ldynsym && sym->st_name && 2176 ldynsym_symtype[type]) { 2177 ofl->ofl_dynlocscnt++; 2178 if (st_insert(ofl->ofl_dynstrtab, 2179 sdp->sd_name) == -1) 2180 return (S_ERROR); 2181 /* Include it in sort section? */ 2182 DYNSORT_COUNT(sdp, sym, type, ++); 2183 } 2184 } 2185 } 2186 } 2187 2188 /* 2189 * The GNU ld interprets the top bit of the 16-bit Versym value 2190 * (0x8000) as the "hidden" bit. If this bit is set, the linker 2191 * is supposed to act as if that symbol does not exist. The Solaris 2192 * linker does not support this mechanism, or the model of interface 2193 * evolution that it allows, but we honor it in GNU ld produced 2194 * objects in order to interoperate with them. 2195 * 2196 * Determine if we should honor the GNU hidden bit for this file. 2197 */ 2198 test_gnu_hidden_bit = ((ifl->ifl_flags & FLG_IF_GNUVER) != 0) && 2199 (ifl->ifl_versym != NULL); 2200 2201 /* 2202 * Now scan the global symbols entering them in the internal symbol 2203 * table or resolving them as necessary. 2204 */ 2205 sym = (Sym *)isc->is_indata->d_buf; 2206 sym += local; 2207 weak = 0; 2208 /* LINTED */ 2209 for (ndx = (int)local; ndx < total; sym++, ndx++) { 2210 const char *name; 2211 Word shndx, sdflags = 0; 2212 int shndx_bad = 0; 2213 2214 /* 2215 * Determine and validate the associated section index. 2216 */ 2217 if (symshndx && (sym->st_shndx == SHN_XINDEX)) { 2218 shndx = symshndx[ndx]; 2219 } else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) { 2220 sdflags |= FLG_SY_SPECSEC; 2221 } else if (shndx > ifl->ifl_ehdr->e_shnum) { 2222 /* We need the name before we can issue error */ 2223 shndx_bad = 1; 2224 } 2225 2226 /* 2227 * Check if st_name has a valid value or not. 2228 */ 2229 if ((name = string(ofl, ifl, sym, strs, strsize, ndx, shndx, 2230 symsecname, strsecname, &sdflags)) == NULL) { 2231 ofl->ofl_flags |= FLG_OF_FATAL; 2232 continue; 2233 } 2234 2235 /* 2236 * Now that we have the name, if the section index 2237 * was bad, report it. 2238 */ 2239 if (shndx_bad) { 2240 eprintf(ofl->ofl_lml, ERR_WARNING, 2241 MSG_INTL(MSG_SYM_INVSHNDX), 2242 demangle_symname(name, isc->is_name, ndx), 2243 ifl->ifl_name, 2244 conv_sym_shndx(sym->st_shndx, &inv_buf)); 2245 continue; 2246 } 2247 2248 2249 /* 2250 * Test for the GNU hidden bit, and ignore symbols that 2251 * have it set. 2252 */ 2253 if (test_gnu_hidden_bit && 2254 ((ifl->ifl_versym[ndx] & 0x8000) != 0)) 2255 continue; 2256 2257 /* 2258 * The linker itself will generate symbols for _end, _etext, 2259 * _edata, _DYNAMIC and _PROCEDURE_LINKAGE_TABLE_, so don't 2260 * bother entering these symbols from shared objects. This 2261 * results in some wasted resolution processing, which is hard 2262 * to feel, but if nothing else, pollutes diagnostic relocation 2263 * output. 2264 */ 2265 if (name[0] && (etype == ET_DYN) && (sym->st_size == 0) && 2266 (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) && 2267 (name[0] == '_') && ((name[1] == 'e') || 2268 (name[1] == 'D') || (name[1] == 'P')) && 2269 ((strcmp(name, MSG_ORIG(MSG_SYM_ETEXT_U)) == 0) || 2270 (strcmp(name, MSG_ORIG(MSG_SYM_EDATA_U)) == 0) || 2271 (strcmp(name, MSG_ORIG(MSG_SYM_END_U)) == 0) || 2272 (strcmp(name, MSG_ORIG(MSG_SYM_DYNAMIC_U)) == 0) || 2273 (strcmp(name, MSG_ORIG(MSG_SYM_PLKTBL_U)) == 0))) { 2274 ifl->ifl_oldndx[ndx] = 0; 2275 continue; 2276 } 2277 2278 /* 2279 * Determine and validate the symbols binding. 2280 */ 2281 bind = ELF_ST_BIND(sym->st_info); 2282 if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) { 2283 eprintf(ofl->ofl_lml, ERR_WARNING, 2284 MSG_INTL(MSG_SYM_NONGLOB), 2285 demangle_symname(name, isc->is_name, ndx), 2286 ifl->ifl_name, 2287 conv_sym_info_bind(bind, 0, &inv_buf)); 2288 continue; 2289 } 2290 if (bind == STB_WEAK) 2291 weak++; 2292 2293 /* 2294 * If this symbol falls within the range of a section being 2295 * discarded, then discard the symbol itself. 2296 */ 2297 if (((sdflags & FLG_SY_SPECSEC) == 0) && 2298 (sym->st_shndx != SHN_UNDEF)) { 2299 Is_desc *isp; 2300 2301 if (shndx >= ifl->ifl_shnum) { 2302 /* 2303 * Carry our some basic sanity checks 2304 * The symbol will not be carried forward to 2305 * the output file, which won't be a problem 2306 * unless a relocation is required against it. 2307 */ 2308 eprintf(ofl->ofl_lml, ERR_WARNING, 2309 MSG_INTL(MSG_SYM_INVSHNDX), 2310 demangle_symname(name, isc->is_name, ndx), 2311 ifl->ifl_name, 2312 conv_sym_shndx(sym->st_shndx, &inv_buf)); 2313 continue; 2314 } 2315 2316 isp = ifl->ifl_isdesc[shndx]; 2317 if (isp && (isp->is_flags & FLG_IS_DISCARD)) { 2318 if ((sdp = 2319 libld_calloc(sizeof (Sym_desc), 1)) == NULL) 2320 return (S_ERROR); 2321 2322 /* 2323 * Create a dummy symbol entry so that if we 2324 * find any references to this discarded symbol 2325 * we can compensate. 2326 */ 2327 sdp->sd_name = name; 2328 sdp->sd_sym = sym; 2329 sdp->sd_file = ifl; 2330 sdp->sd_isc = isp; 2331 sdp->sd_flags = FLG_SY_ISDISC; 2332 ifl->ifl_oldndx[ndx] = sdp; 2333 2334 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp)); 2335 continue; 2336 } 2337 } 2338 2339 /* 2340 * If the symbol does not already exist in the internal symbol 2341 * table add it, otherwise resolve the conflict. If the symbol 2342 * from this file is kept, retain its symbol table index for 2343 * possible use in associating a global alias. 2344 */ 2345 /* LINTED */ 2346 hash = (Word)elf_hash((const char *)name); 2347 if ((sdp = ld_sym_find(name, hash, &where, ofl)) == NULL) { 2348 DBG_CALL(Dbg_syms_global(ofl->ofl_lml, ndx, name)); 2349 if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, ndx, 2350 shndx, sdflags, 0, &where)) == (Sym_desc *)S_ERROR) 2351 return (S_ERROR); 2352 2353 } else if (ld_sym_resolve(sdp, sym, ifl, ofl, ndx, shndx, 2354 sdflags) == S_ERROR) 2355 return (S_ERROR); 2356 2357 /* 2358 * After we've compared a defined symbol in one shared 2359 * object, flag the symbol so we don't compare it again. 2360 */ 2361 if ((etype == ET_DYN) && (sym->st_shndx != SHN_UNDEF) && 2362 ((sdp->sd_flags & FLG_SY_SOFOUND) == 0)) 2363 sdp->sd_flags |= FLG_SY_SOFOUND; 2364 2365 /* 2366 * If the symbol is accepted from this file retain the symbol 2367 * index for possible use in aliasing. 2368 */ 2369 if (sdp->sd_file == ifl) 2370 sdp->sd_symndx = ndx; 2371 2372 ifl->ifl_oldndx[ndx] = sdp; 2373 2374 /* 2375 * If we've accepted a register symbol, continue to validate 2376 * it. 2377 */ 2378 if (sdp->sd_flags & FLG_SY_REGSYM) { 2379 Sym_desc *rsdp; 2380 2381 /* 2382 * The presence of FLG_SY_REGSYM means that 2383 * the pointers in ld_targ.t_ms are non-NULL. 2384 */ 2385 rsdp = (*ld_targ.t_ms.ms_reg_find)(sdp->sd_sym, ofl); 2386 if (rsdp == NULL) { 2387 if ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) == 0) 2388 return (S_ERROR); 2389 } else if (rsdp != sdp) { 2390 (void) (*ld_targ.t_ms.ms_reg_check)(rsdp, 2391 sdp->sd_sym, sdp->sd_name, ifl, ofl); 2392 } 2393 } 2394 2395 /* 2396 * For a relocatable object, if this symbol is defined 2397 * and has non-zero length and references an address 2398 * within an associated section, then check its extents 2399 * to make sure the section boundaries encompass it. 2400 * If they don't, the ELF file is corrupt. Note that this 2401 * global symbol may have come from another file to satisfy 2402 * an UNDEF symbol of the same name from this one. In that 2403 * case, we don't check it, because it was already checked 2404 * as part of its own file. 2405 */ 2406 if (etype_rel && (sdp->sd_file == ifl)) { 2407 Sym *tsym = sdp->sd_sym; 2408 2409 if (SYM_LOC_BADADDR(sdp, tsym, 2410 ELF_ST_TYPE(tsym->st_info))) { 2411 issue_badaddr_msg(ifl, ofl, sdp, 2412 tsym, tsym->st_shndx); 2413 continue; 2414 } 2415 } 2416 } 2417 2418 /* 2419 * Associate weak (alias) symbols to their non-weak counterparts by 2420 * scaning the global symbols one more time. 2421 * 2422 * This association is needed when processing the symbols from a shared 2423 * object dependency when a a weak definition satisfies a reference: 2424 * 2425 * - When building a dynamic executable, if a referenced symbol is a 2426 * data item, the symbol data is copied to the executables address 2427 * space. In this copy-relocation case, we must also reassociate 2428 * the alias symbol with its new location in the executable. 2429 * 2430 * - If the referenced symbol is a function then we may need to 2431 * promote the symbols binding from undefined weak to undefined, 2432 * otherwise the run-time linker will not generate the correct 2433 * relocation error should the symbol not be found. 2434 * 2435 * Weak alias association is also required when a local dynsym table 2436 * is being created. This table should only contain one instance of a 2437 * symbol that is associated to a given address. 2438 * 2439 * The true association between a weak/strong symbol pair is that both 2440 * symbol entries are identical, thus first we create a sorted symbol 2441 * list keyed off of the symbols section index and value. If the symbol 2442 * belongs to the same section and has the same value, then the chances 2443 * are that the rest of the symbols data is the same. This list is then 2444 * scanned for weak symbols, and if one is found then any strong 2445 * association will exist in the entries that follow. Thus we just have 2446 * to scan one (typically a single alias) or more (in the uncommon 2447 * instance of multiple weak to strong associations) entries to 2448 * determine if a match exists. 2449 */ 2450 if (weak && (OFL_ALLOW_LDYNSYM(ofl) || (etype == ET_DYN)) && 2451 (total > local)) { 2452 static Sym_desc **sort; 2453 static size_t osize = 0; 2454 size_t nsize = (total - local) * sizeof (Sym_desc *); 2455 2456 /* 2457 * As we might be processing many input files, and many symbols, 2458 * try and reuse a static sort buffer. Note, presently we're 2459 * playing the game of never freeing any buffers as there's a 2460 * belief this wastes time. 2461 */ 2462 if ((osize == 0) || (nsize > osize)) { 2463 if ((sort = libld_malloc(nsize)) == NULL) 2464 return (S_ERROR); 2465 osize = nsize; 2466 } 2467 (void) memcpy((void *)sort, &ifl->ifl_oldndx[local], nsize); 2468 2469 qsort(sort, (total - local), sizeof (Sym_desc *), compare); 2470 2471 for (ndx = 0; ndx < (total - local); ndx++) { 2472 Sym_desc *wsdp = sort[ndx]; 2473 Sym *wsym; 2474 int sndx; 2475 2476 /* 2477 * Ignore any empty symbol descriptor, or the case where 2478 * the symbol has been resolved to a different file. 2479 */ 2480 if ((wsdp == NULL) || (wsdp->sd_file != ifl)) 2481 continue; 2482 2483 wsym = wsdp->sd_sym; 2484 2485 if ((wsym->st_shndx == SHN_UNDEF) || 2486 (wsdp->sd_flags & FLG_SY_SPECSEC) || 2487 (ELF_ST_BIND(wsym->st_info) != STB_WEAK)) 2488 continue; 2489 2490 /* 2491 * We have a weak symbol, if it has a strong alias it 2492 * will have been sorted to one of the following sort 2493 * table entries. Note that we could have multiple weak 2494 * symbols aliased to one strong (if this occurs then 2495 * the strong symbol only maintains one alias back to 2496 * the last weak). 2497 */ 2498 for (sndx = ndx + 1; sndx < (total - local); sndx++) { 2499 Sym_desc *ssdp = sort[sndx]; 2500 Sym *ssym; 2501 int w_dynbits, s_dynbits; 2502 2503 /* 2504 * Ignore any empty symbol descriptor, or the 2505 * case where the symbol has been resolved to a 2506 * different file. 2507 */ 2508 if ((ssdp == NULL) || (ssdp->sd_file != ifl)) 2509 continue; 2510 2511 ssym = ssdp->sd_sym; 2512 2513 if (ssym->st_shndx == SHN_UNDEF) 2514 continue; 2515 2516 if ((ssym->st_shndx != wsym->st_shndx) || 2517 (ssym->st_value != wsym->st_value)) 2518 break; 2519 2520 if ((ssym->st_size != wsym->st_size) || 2521 (ssdp->sd_flags & FLG_SY_SPECSEC) || 2522 (ELF_ST_BIND(ssym->st_info) == STB_WEAK)) 2523 continue; 2524 2525 /* 2526 * If a sharable object, set link fields so 2527 * that they reference each other.` 2528 */ 2529 if (etype == ET_DYN) { 2530 ssdp->sd_aux->sa_linkndx = 2531 (Word)wsdp->sd_symndx; 2532 wsdp->sd_aux->sa_linkndx = 2533 (Word)ssdp->sd_symndx; 2534 } 2535 2536 /* 2537 * Determine which of these two symbols go into 2538 * the sort section. If a mapfile has made 2539 * explicit settings of the FLG_SY_*DYNSORT 2540 * flags for both symbols, then we do what they 2541 * say. If one has the DYNSORT flags set, we 2542 * set the NODYNSORT bit in the other. And if 2543 * neither has an explicit setting, then we 2544 * favor the weak symbol because they usually 2545 * lack the leading underscore. 2546 */ 2547 w_dynbits = wsdp->sd_flags & 2548 (FLG_SY_DYNSORT | FLG_SY_NODYNSORT); 2549 s_dynbits = ssdp->sd_flags & 2550 (FLG_SY_DYNSORT | FLG_SY_NODYNSORT); 2551 if (!(w_dynbits && s_dynbits)) { 2552 if (s_dynbits) { 2553 if (s_dynbits == FLG_SY_DYNSORT) 2554 wsdp->sd_flags |= 2555 FLG_SY_NODYNSORT; 2556 } else if (w_dynbits != 2557 FLG_SY_NODYNSORT) { 2558 ssdp->sd_flags |= 2559 FLG_SY_NODYNSORT; 2560 } 2561 } 2562 break; 2563 } 2564 } 2565 } 2566 return (1); 2567 2568 #undef SYM_LOC_BADADDR 2569 } 2570 2571 /* 2572 * Add an undefined symbol to the symbol table. The reference originates from 2573 * the location identifed by the message id (mid). These references can 2574 * originate from command line options such as -e, -u, -initarray, etc. 2575 * (identified with MSG_INTL(MSG_STR_COMMAND)), or from internally generated 2576 * TLS relocation references (identified with MSG_INTL(MSG_STR_TLSREL)). 2577 */ 2578 Sym_desc * 2579 ld_sym_add_u(const char *name, Ofl_desc *ofl, Msg mid) 2580 { 2581 Sym *sym; 2582 Ifl_desc *ifl = NULL, *_ifl; 2583 Sym_desc *sdp; 2584 Word hash; 2585 Listnode *lnp; 2586 avl_index_t where; 2587 const char *reference = MSG_INTL(mid); 2588 2589 /* 2590 * As an optimization, determine whether we've already generated this 2591 * reference. If the symbol doesn't already exist we'll create it. 2592 * Or if the symbol does exist from a different source, we'll resolve 2593 * the conflict. 2594 */ 2595 /* LINTED */ 2596 hash = (Word)elf_hash(name); 2597 if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL) { 2598 if ((sdp->sd_sym->st_shndx == SHN_UNDEF) && 2599 (sdp->sd_file->ifl_name == reference)) 2600 return (sdp); 2601 } 2602 2603 /* 2604 * Determine whether a pseudo input file descriptor exists to represent 2605 * the command line, as any global symbol needs an input file descriptor 2606 * during any symbol resolution (refer to map_ifl() which provides a 2607 * similar method for adding symbols from mapfiles). 2608 */ 2609 for (LIST_TRAVERSE(&ofl->ofl_objs, lnp, _ifl)) 2610 if (strcmp(_ifl->ifl_name, reference) == 0) { 2611 ifl = _ifl; 2612 break; 2613 } 2614 2615 /* 2616 * If no descriptor exists create one. 2617 */ 2618 if (ifl == NULL) { 2619 if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) == 2620 (Ifl_desc *)0) 2621 return ((Sym_desc *)S_ERROR); 2622 ifl->ifl_name = reference; 2623 ifl->ifl_flags = FLG_IF_NEEDED | FLG_IF_FILEREF; 2624 if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr), 2625 1)) == NULL) 2626 return ((Sym_desc *)S_ERROR); 2627 ifl->ifl_ehdr->e_type = ET_REL; 2628 2629 if (list_appendc(&ofl->ofl_objs, ifl) == 0) 2630 return ((Sym_desc *)S_ERROR); 2631 } 2632 2633 /* 2634 * Allocate a symbol structure and add it to the global symbol table. 2635 */ 2636 if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL) 2637 return ((Sym_desc *)S_ERROR); 2638 sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE); 2639 sym->st_shndx = SHN_UNDEF; 2640 2641 DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl)); 2642 if (sdp == NULL) { 2643 DBG_CALL(Dbg_syms_global(ofl->ofl_lml, 0, name)); 2644 if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, 0, SHN_UNDEF, 2645 0, 0, &where)) == (Sym_desc *)S_ERROR) 2646 return ((Sym_desc *)S_ERROR); 2647 } else if (ld_sym_resolve(sdp, sym, ifl, ofl, 0, 2648 SHN_UNDEF, 0) == S_ERROR) 2649 return ((Sym_desc *)S_ERROR); 2650 2651 sdp->sd_flags &= ~FLG_SY_CLEAN; 2652 sdp->sd_flags |= FLG_SY_CMDREF; 2653 2654 return (sdp); 2655 } 2656