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