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