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