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  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
  27  */
  28 
  29 /*
  30  * Module sections. Initialize special sections
  31  */
  32 
  33 #define ELF_TARGET_AMD64
  34 
  35 #include        <string.h>
  36 #include        <strings.h>
  37 #include        <stdio.h>
  38 #include        <link.h>
  39 #include        <debug.h>
  40 #include        "msg.h"
  41 #include        "_libld.h"
  42 
  43 inline static void
  44 remove_local(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym)
  45 {
  46         Sym     *sym = sdp->sd_sym;
  47         uchar_t type = ELF_ST_TYPE(sym->st_info);
  48         /* LINTED - only used for assert() */
  49         int     err;
  50 
  51         if ((ofl->ofl_flags & FLG_OF_REDLSYM) == 0) {
  52                 ofl->ofl_locscnt--;
  53 
  54                 err = st_delstring(ofl->ofl_strtab, sdp->sd_name);
  55                 assert(err != -1);
  56 
  57                 if (allow_ldynsym && ldynsym_symtype[type]) {
  58                         ofl->ofl_dynlocscnt--;
  59 
  60                         err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name);
  61                         assert(err != -1);
  62                         /* Remove from sort section? */
  63                         DYNSORT_COUNT(sdp, sym, type, --);
  64                 }
  65         }
  66         sdp->sd_flags |= FLG_SY_ISDISC;
  67 }
  68 
  69 inline static void
  70 remove_scoped(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym)
  71 {
  72         Sym     *sym = sdp->sd_sym;
  73         uchar_t type = ELF_ST_TYPE(sym->st_info);
  74         /* LINTED - only used for assert() */
  75         int     err;
  76 
  77         ofl->ofl_scopecnt--;
  78         ofl->ofl_elimcnt++;
  79 
  80         err = st_delstring(ofl->ofl_strtab, sdp->sd_name);
  81         assert(err != -1);
  82 
  83         if (allow_ldynsym && ldynsym_symtype[type]) {
  84                 ofl->ofl_dynscopecnt--;
  85 
  86                 err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name);
  87                 assert(err != -1);
  88                 /* Remove from sort section? */
  89                 DYNSORT_COUNT(sdp, sym, type, --);
  90         }
  91         sdp->sd_flags |= FLG_SY_ELIM;
  92 }
  93 
  94 inline static void
  95 ignore_sym(Ofl_desc *ofl, Ifl_desc *ifl, Sym_desc *sdp, int allow_ldynsym)
  96 {
  97         Os_desc *osp;
  98         Is_desc *isp = sdp->sd_isc;
  99         uchar_t bind = ELF_ST_BIND(sdp->sd_sym->st_info);
 100 
 101         if (bind == STB_LOCAL) {
 102                 uchar_t type = ELF_ST_TYPE(sdp->sd_sym->st_info);
 103 
 104                 /*
 105                  * Skip section symbols, these were never collected in the
 106                  * first place.
 107                  */
 108                 if (type == STT_SECTION)
 109                         return;
 110 
 111                 /*
 112                  * Determine if the whole file is being removed.  Remove any
 113                  * file symbol, and any symbol that is not associated with a
 114                  * section, provided the symbol has not been identified as
 115                  * (update) required.
 116                  */
 117                 if (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) &&
 118                     ((type == STT_FILE) || ((isp == NULL) &&
 119                     ((sdp->sd_flags & FLG_SY_UPREQD) == 0)))) {
 120                         DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
 121                         if (ifl->ifl_flags & FLG_IF_IGNORE)
 122                                 remove_local(ofl, sdp, allow_ldynsym);
 123                         return;
 124                 }
 125 
 126         } else {
 127                 /*
 128                  * Global symbols can only be eliminated when the interfaces of
 129                  * an object have been defined via versioning/scoping.
 130                  */
 131                 if (!SYM_IS_HIDDEN(sdp))
 132                         return;
 133 
 134                 /*
 135                  * Remove any unreferenced symbols that are not associated with
 136                  * a section.
 137                  */
 138                 if ((isp == NULL) && ((sdp->sd_flags & FLG_SY_UPREQD) == 0)) {
 139                         DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
 140                         if (ifl->ifl_flags & FLG_IF_IGNORE)
 141                                 remove_scoped(ofl, sdp, allow_ldynsym);
 142                         return;
 143                 }
 144         }
 145 
 146         /*
 147          * Do not discard any symbols that are associated with non-allocable
 148          * segments.
 149          */
 150         if (isp && ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
 151             ((osp = isp->is_osdesc) != 0) &&
 152             (osp->os_sgdesc->sg_phdr.p_type == PT_LOAD)) {
 153                 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
 154                 if (ifl->ifl_flags & FLG_IF_IGNORE) {
 155                         if (bind == STB_LOCAL)
 156                                 remove_local(ofl, sdp, allow_ldynsym);
 157                         else
 158                                 remove_scoped(ofl, sdp, allow_ldynsym);
 159                 }
 160         }
 161 }
 162 
 163 /*
 164  * There are situations where we may count output sections (ofl_shdrcnt)
 165  * that are subsequently eliminated from the output object. Whether or
 166  * not this happens cannot be known until all input has been seen and
 167  * section elimination code has run. However, the situations where this
 168  * outcome is possible are known, and are flagged by setting FLG_OF_ADJOSCNT.
 169  *
 170  * If FLG_OF_ADJOSCNT is set, this routine makes a pass over the output
 171  * sections. If an unused output section is encountered, we decrement
 172  * ofl->ofl_shdrcnt and remove the section name from the .shstrtab string
 173  * table (ofl->ofl_shdrsttab).
 174  *
 175  * This code must be kept in sync with the similar code
 176  * found in outfile.c:ld_create_outfile().
 177  */
 178 static void
 179 adjust_os_count(Ofl_desc *ofl)
 180 {
 181         Sg_desc         *sgp;
 182         Is_desc         *isp;
 183         Os_desc         *osp;
 184         Ifl_desc        *ifl;
 185         Aliste          idx1;
 186 
 187         if ((ofl->ofl_flags & FLG_OF_ADJOSCNT) == 0)
 188                 return;
 189 
 190         /*
 191          * For each output section, look at the input sections to find at least
 192          * one input section that has not been eliminated. If none are found,
 193          * the -z ignore processing above has eliminated that output section.
 194          */
 195         for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
 196                 Aliste  idx2;
 197                 Word    ptype = sgp->sg_phdr.p_type;
 198 
 199                 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
 200                         Aliste  idx3;
 201                         int     keep = 0, os_isdescs_idx;
 202 
 203                         OS_ISDESCS_TRAVERSE(os_isdescs_idx, osp, idx3, isp) {
 204                                 ifl = isp->is_file;
 205 
 206                                 /* Input section is tagged for discard? */
 207                                 if (isp->is_flags & FLG_IS_DISCARD)
 208                                         continue;
 209 
 210                                 /*
 211                                  * If the file is discarded, it will take
 212                                  * the section with it.
 213                                  */
 214                                 if (ifl &&
 215                                     (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) ||
 216                                     ((ptype == PT_LOAD) &&
 217                                     ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
 218                                     (isp->is_shdr->sh_size > 0))) &&
 219                                     (ifl->ifl_flags & FLG_IF_IGNORE))
 220                                         continue;
 221 
 222                                 /*
 223                                  * We have found a kept input section,
 224                                  * so the output section will be created.
 225                                  */
 226                                 keep = 1;
 227                                 break;
 228                         }
 229                         /*
 230                          * If no section of this name was kept, decrement
 231                          * the count and remove the name from .shstrtab.
 232                          */
 233                         if (keep == 0) {
 234                                 /* LINTED - only used for assert() */
 235                                 int err;
 236 
 237                                 ofl->ofl_shdrcnt--;
 238                                 err = st_delstring(ofl->ofl_shdrsttab,
 239                                     osp->os_name);
 240                                 assert(err != -1);
 241                         }
 242                 }
 243         }
 244 }
 245 
 246 /*
 247  * If -zignore has been in effect, scan all input files to determine if the
 248  * file, or sections from the file, have been referenced.  If not, the file or
 249  * some of the files sections can be discarded. If sections are to be
 250  * discarded, rescan the output relocations and the symbol table and remove
 251  * the relocations and symbol entries that are no longer required.
 252  *
 253  * Note:  It's possible that a section which is being discarded has contributed
 254  *        to the GOT table or the PLT table.  However, we can't at this point
 255  *        eliminate the corresponding entries.  This is because there could well
 256  *        be other sections referencing those same entries, but we don't have
 257  *        the infrastructure to determine this.  So, keep the PLT and GOT
 258  *        entries in the table in case someone wants them.
 259  * Note:  The section to be affected needs to be allocatable.
 260  *        So even if -zignore is in effect, if the section is not allocatable,
 261  *        we do not eliminate it.
 262  */
 263 static uintptr_t
 264 ignore_section_processing(Ofl_desc *ofl)
 265 {
 266         Sg_desc         *sgp;
 267         Is_desc         *isp;
 268         Os_desc         *osp;
 269         Ifl_desc        *ifl;
 270         Rel_cachebuf    *rcbp;
 271         Rel_desc        *rsp;
 272         int             allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
 273         Aliste          idx1;
 274 
 275         for (APLIST_TRAVERSE(ofl->ofl_objs, idx1, ifl)) {
 276                 uint_t  num, discard;
 277 
 278                 /*
 279                  * Diagnose (-D unused) a completely unreferenced file.
 280                  */
 281                 if ((ifl->ifl_flags & FLG_IF_FILEREF) == 0)
 282                         DBG_CALL(Dbg_unused_file(ofl->ofl_lml,
 283                             ifl->ifl_name, 0, 0));
 284                 if (((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0) ||
 285                     ((ifl->ifl_flags & FLG_IF_IGNORE) == 0))
 286                         continue;
 287 
 288                 /*
 289                  * Before scanning the whole symbol table to determine if
 290                  * symbols should be discard - quickly (relatively) scan the
 291                  * sections to determine if any are to be discarded.
 292                  */
 293                 discard = 0;
 294                 if (ifl->ifl_flags & FLG_IF_FILEREF) {
 295                         for (num = 1; num < ifl->ifl_shnum; num++) {
 296                                 if (((isp = ifl->ifl_isdesc[num]) != NULL) &&
 297                                     ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
 298                                     ((osp = isp->is_osdesc) != NULL) &&
 299                                     ((sgp = osp->os_sgdesc) != NULL) &&
 300                                     (sgp->sg_phdr.p_type == PT_LOAD)) {
 301                                         discard++;
 302                                         break;
 303                                 }
 304                         }
 305                 }
 306 
 307                 /*
 308                  * No sections are to be 'ignored'
 309                  */
 310                 if ((discard == 0) && (ifl->ifl_flags & FLG_IF_FILEREF))
 311                         continue;
 312 
 313                 /*
 314                  * We know that we have discarded sections.  Scan the symbol
 315                  * table for this file to determine if symbols need to be
 316                  * discarded that are associated with the 'ignored' sections.
 317                  */
 318                 for (num = 1; num < ifl->ifl_symscnt; num++) {
 319                         Sym_desc        *sdp;
 320 
 321                         /*
 322                          * If the symbol definition has been resolved to another
 323                          * file, or the symbol has already been discarded or
 324                          * eliminated, skip it.
 325                          */
 326                         sdp = ifl->ifl_oldndx[num];
 327                         if ((sdp->sd_file != ifl) ||
 328                             (sdp->sd_flags &
 329                             (FLG_SY_ISDISC | FLG_SY_INVALID | FLG_SY_ELIM)))
 330                                 continue;
 331 
 332                         /*
 333                          * Complete the investigation of the symbol.
 334                          */
 335                         ignore_sym(ofl, ifl, sdp, allow_ldynsym);
 336                 }
 337         }
 338 
 339         /*
 340          * If we were only here to solicit debugging diagnostics, we're done.
 341          */
 342         if ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0)
 343                 return (1);
 344 
 345         /*
 346          * Scan all output relocations searching for those against discarded or
 347          * ignored sections.  If one is found, decrement the total outrel count.
 348          */
 349         REL_CACHE_TRAVERSE(&ofl->ofl_outrels, idx1, rcbp, rsp) {
 350                 Is_desc         *isc = rsp->rel_isdesc;
 351                 uint_t          flags, entsize;
 352                 Shdr            *shdr;
 353 
 354                 if ((isc == NULL) || ((isc->is_flags & (FLG_IS_SECTREF))) ||
 355                     ((ifl = isc->is_file) == NULL) ||
 356                     ((ifl->ifl_flags & FLG_IF_IGNORE) == 0) ||
 357                     ((shdr = isc->is_shdr) == NULL) ||
 358                     ((shdr->sh_flags & SHF_ALLOC) == 0))
 359                         continue;
 360 
 361                 flags = rsp->rel_flags;
 362 
 363                 if (flags & (FLG_REL_GOT | FLG_REL_BSS |
 364                     FLG_REL_NOINFO | FLG_REL_PLT))
 365                         continue;
 366 
 367                 osp = RELAUX_GET_OSDESC(rsp);
 368 
 369                 if (rsp->rel_flags & FLG_REL_RELA)
 370                         entsize = sizeof (Rela);
 371                 else
 372                         entsize = sizeof (Rel);
 373 
 374                 assert(osp->os_szoutrels > 0);
 375                 osp->os_szoutrels -= entsize;
 376 
 377                 if (!(flags & FLG_REL_PLT))
 378                         ofl->ofl_reloccntsub++;
 379 
 380                 if (rsp->rel_rtype == ld_targ.t_m.m_r_relative)
 381                         ofl->ofl_relocrelcnt--;
 382         }
 383 
 384         /*
 385          * As a result of our work here, the number of output sections may
 386          * have decreased. Trigger a call to adjust_os_count().
 387          */
 388         ofl->ofl_flags |= FLG_OF_ADJOSCNT;
 389 
 390         return (1);
 391 }
 392 
 393 /*
 394  * Allocate Elf_Data, Shdr, and Is_desc structures for a new
 395  * section.
 396  *
 397  * entry:
 398  *      ofl - Output file descriptor
 399  *      shtype - SHT_ type code for section.
 400  *      shname - String giving the name for the new section.
 401  *      entcnt - # of items contained in the data part of the new section.
 402  *              This value is multiplied against the known element size
 403  *              for the section type to determine the size of the data
 404  *              area for the section. It is only meaningful in cases where
 405  *              the section type has a non-zero element size. In other cases,
 406  *              the caller must set the size fields in the *ret_data and
 407  *              *ret_shdr structs manually.
 408  *      ret_isec, ret_shdr, ret_data - Address of pointers to
 409  *              receive address of newly allocated structs.
 410  *
 411  * exit:
 412  *      On error, returns S_ERROR. On success, returns (1), and the
 413  *      ret_ pointers have been updated to point at the new structures,
 414  *      which have been filled in. To finish the task, the caller must
 415  *      update any fields within the supplied descriptors that differ
 416  *      from its needs, and then call ld_place_section().
 417  */
 418 static uintptr_t
 419 new_section(Ofl_desc *ofl, Word shtype, const char *shname, Xword entcnt,
 420         Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data)
 421 {
 422         typedef struct sec_info {
 423                 Word d_type;
 424                 Word align;     /* Used in both data and section header */
 425                 Word sh_flags;
 426                 Word sh_entsize;
 427         } SEC_INFO_T;
 428 
 429         const SEC_INFO_T        *sec_info;
 430 
 431         Shdr            *shdr;
 432         Elf_Data        *data;
 433         Is_desc         *isec;
 434         size_t          size;
 435 
 436         /*
 437          * For each type of section, we have a distinct set of
 438          * SEC_INFO_T values. This macro defines a static structure
 439          * containing those values and generates code to set the sec_info
 440          * pointer to refer to it. The pointer in sec_info remains valid
 441          * outside of the declaration scope because the info_s struct is static.
 442          *
 443          * We can't determine the value of M_WORD_ALIGN at compile time, so
 444          * a different variant is used for those cases.
 445          */
 446 #define SET_SEC_INFO(d_type, d_align, sh_flags, sh_entsize) \
 447         { \
 448                 static const SEC_INFO_T info_s = { d_type, d_align, sh_flags, \
 449                     sh_entsize}; \
 450                 sec_info = &info_s; \
 451         }
 452 #define SET_SEC_INFO_WORD_ALIGN(d_type, sh_flags, sh_entsize) \
 453         { \
 454                 static SEC_INFO_T info_s = { d_type, 0, sh_flags, \
 455                     sh_entsize}; \
 456                 info_s.align = ld_targ.t_m.m_word_align; \
 457                 sec_info = &info_s; \
 458         }
 459 
 460         switch (shtype) {
 461         case SHT_PROGBITS:
 462                 /*
 463                  * SHT_PROGBITS sections contain are used for many
 464                  * different sections. Alignments and flags differ.
 465                  * Some have a standard entsize, and others don't.
 466                  * We set some defaults here, but there is no expectation
 467                  * that they are correct or complete for any specific
 468                  * purpose. The caller must provide the correct values.
 469                  */
 470                 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 0)
 471                 break;
 472 
 473         case SHT_SYMTAB:
 474                 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, 0, sizeof (Sym))
 475                 break;
 476 
 477         case SHT_DYNSYM:
 478                 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, SHF_ALLOC, sizeof (Sym))
 479                 break;
 480 
 481         case SHT_SUNW_LDYNSYM:
 482                 ofl->ofl_flags |= FLG_OF_OSABI;
 483                 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, SHF_ALLOC, sizeof (Sym))
 484                 break;
 485 
 486         case SHT_STRTAB:
 487                 /*
 488                  * A string table may or may not be allocable, depending
 489                  * on context, so we leave that flag unset and leave it to
 490                  * the caller to add it if necessary.
 491                  *
 492                  * String tables do not have a standard entsize, so
 493                  * we set it to 0.
 494                  */
 495                 SET_SEC_INFO(ELF_T_BYTE, 1, SHF_STRINGS, 0)
 496                 break;
 497 
 498         case SHT_RELA:
 499                 /*
 500                  * Relocations with an addend (Everything except 32-bit X86).
 501                  * The caller is expected to set all section header flags.
 502                  */
 503                 SET_SEC_INFO_WORD_ALIGN(ELF_T_RELA, 0, sizeof (Rela))
 504                 break;
 505 
 506         case SHT_REL:
 507                 /*
 508                  * Relocations without an addend (32-bit X86 only).
 509                  * The caller is expected to set all section header flags.
 510                  */
 511                 SET_SEC_INFO_WORD_ALIGN(ELF_T_REL, 0, sizeof (Rel))
 512                 break;
 513 
 514         case SHT_HASH:
 515                 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC, sizeof (Word))
 516                 break;
 517 
 518         case SHT_SUNW_symsort:
 519         case SHT_SUNW_tlssort:
 520                 ofl->ofl_flags |= FLG_OF_OSABI;
 521                 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC, sizeof (Word))
 522                 break;
 523 
 524         case SHT_DYNAMIC:
 525                 /*
 526                  * A dynamic section may or may not be allocable, and may or
 527                  * may not be writable, depending on context, so we leave the
 528                  * flags unset and leave it to the caller to add them if
 529                  * necessary.
 530                  */
 531                 SET_SEC_INFO_WORD_ALIGN(ELF_T_DYN, 0, sizeof (Dyn))
 532                 break;
 533 
 534         case SHT_NOBITS:
 535                 /*
 536                  * SHT_NOBITS is used for BSS-type sections. The size and
 537                  * alignment depend on the specific use and must be adjusted
 538                  * by the caller.
 539                  */
 540                 SET_SEC_INFO(ELF_T_BYTE, 0, SHF_ALLOC | SHF_WRITE, 0)
 541                 break;
 542 
 543         case SHT_INIT_ARRAY:
 544         case SHT_FINI_ARRAY:
 545         case SHT_PREINIT_ARRAY:
 546                 SET_SEC_INFO(ELF_T_ADDR, sizeof (Addr), SHF_ALLOC | SHF_WRITE,
 547                     sizeof (Addr))
 548                 break;
 549 
 550         case SHT_SYMTAB_SHNDX:
 551                 /*
 552                  * Note that these sections are created to be associated
 553                  * with both symtab and dynsym symbol tables. However, they
 554                  * are non-allocable in all cases, because the runtime
 555                  * linker has no need for this information. It is purely
 556                  * informational, used by elfdump(1), debuggers, etc.
 557                  */
 558                 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, 0, sizeof (Word));
 559                 break;
 560 
 561         case SHT_SUNW_cap:
 562                 ofl->ofl_flags |= FLG_OF_OSABI;
 563                 SET_SEC_INFO_WORD_ALIGN(ELF_T_CAP, SHF_ALLOC, sizeof (Cap));
 564                 break;
 565 
 566         case SHT_SUNW_capchain:
 567                 ofl->ofl_flags |= FLG_OF_OSABI;
 568                 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC,
 569                     sizeof (Capchain));
 570                 break;
 571 
 572         case SHT_SUNW_capinfo:
 573                 ofl->ofl_flags |= FLG_OF_OSABI;
 574 #if     _ELF64
 575                 SET_SEC_INFO(ELF_T_XWORD, sizeof (Xword), SHF_ALLOC,
 576                     sizeof (Capinfo));
 577 #else
 578                 SET_SEC_INFO(ELF_T_WORD, sizeof (Word), SHF_ALLOC,
 579                     sizeof (Capinfo));
 580 #endif
 581                 break;
 582 
 583         case SHT_SUNW_move:
 584                 ofl->ofl_flags |= FLG_OF_OSABI;
 585                 SET_SEC_INFO(ELF_T_BYTE, sizeof (Lword),
 586                     SHF_ALLOC | SHF_WRITE, sizeof (Move));
 587                 break;
 588 
 589         case SHT_SUNW_syminfo:
 590                 ofl->ofl_flags |= FLG_OF_OSABI;
 591                 /*
 592                  * The sh_info field of the SHT_*_syminfo section points
 593                  * to the header index of the associated .dynamic section,
 594                  * so we also set SHF_INFO_LINK.
 595                  */
 596                 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE,
 597                     SHF_ALLOC | SHF_INFO_LINK, sizeof (Syminfo));
 598                 break;
 599 
 600         case SHT_SUNW_verneed:
 601         case SHT_SUNW_verdef:
 602                 ofl->ofl_flags |= FLG_OF_OSABI;
 603                 /*
 604                  * The info for verneed and versym happen to be the same.
 605                  * The entries in these sections are not of uniform size,
 606                  * so we set the entsize to 0.
 607                  */
 608                 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 0);
 609                 break;
 610 
 611         case SHT_SUNW_versym:
 612                 ofl->ofl_flags |= FLG_OF_OSABI;
 613                 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC,
 614                     sizeof (Versym));
 615                 break;
 616 
 617         default:
 618                 /* Should not happen: fcn called with unknown section type */
 619                 assert(0);
 620                 return (S_ERROR);
 621         }
 622 #undef  SET_SEC_INFO
 623 #undef  SET_SEC_INFO_WORD_ALIGN
 624 
 625         size = entcnt * sec_info->sh_entsize;
 626 
 627         /*
 628          * Allocate and initialize the Elf_Data structure.
 629          */
 630         if ((data = libld_calloc(sizeof (Elf_Data), 1)) == NULL)
 631                 return (S_ERROR);
 632         data->d_type = sec_info->d_type;
 633         data->d_size = size;
 634         data->d_align = sec_info->align;
 635         data->d_version = ofl->ofl_dehdr->e_version;
 636 
 637         /*
 638          * Allocate and initialize the Shdr structure.
 639          */
 640         if ((shdr = libld_calloc(sizeof (Shdr), 1)) == NULL)
 641                 return (S_ERROR);
 642         shdr->sh_type = shtype;
 643         shdr->sh_size = size;
 644         shdr->sh_flags = sec_info->sh_flags;
 645         shdr->sh_addralign = sec_info->align;
 646         shdr->sh_entsize = sec_info->sh_entsize;
 647 
 648         /*
 649          * Allocate and initialize the Is_desc structure.
 650          */
 651         if ((isec = libld_calloc(1, sizeof (Is_desc))) == NULL)
 652                 return (S_ERROR);
 653         isec->is_name = shname;
 654         isec->is_shdr = shdr;
 655         isec->is_indata = data;
 656 
 657 
 658         *ret_isec = isec;
 659         *ret_shdr = shdr;
 660         *ret_data = data;
 661         return (1);
 662 }
 663 
 664 /*
 665  * Use an existing input section as a template to create a new
 666  * input section with the same values as the original, other than
 667  * the size of the data area which is supplied by the caller.
 668  *
 669  * entry:
 670  *      ofl - Output file descriptor
 671  *      ifl - Input file section to use as a template
 672  *      size - Size of data area for new section
 673  *      ret_isec, ret_shdr, ret_data - Address of pointers to
 674  *              receive address of newly allocated structs.
 675  *
 676  * exit:
 677  *      On error, returns S_ERROR. On success, returns (1), and the
 678  *      ret_ pointers have been updated to point at the new structures,
 679  *      which have been filled in. To finish the task, the caller must
 680  *      update any fields within the supplied descriptors that differ
 681  *      from its needs, and then call ld_place_section().
 682  */
 683 static uintptr_t
 684 new_section_from_template(Ofl_desc *ofl, Is_desc *tmpl_isp, size_t size,
 685         Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data)
 686 {
 687         Shdr            *shdr;
 688         Elf_Data        *data;
 689         Is_desc         *isec;
 690 
 691         /*
 692          * Allocate and initialize the Elf_Data structure.
 693          */
 694         if ((data = libld_calloc(sizeof (Elf_Data), 1)) == NULL)
 695                 return (S_ERROR);
 696         data->d_type = tmpl_isp->is_indata->d_type;
 697         data->d_size = size;
 698         data->d_align = tmpl_isp->is_shdr->sh_addralign;
 699         data->d_version = ofl->ofl_dehdr->e_version;
 700 
 701         /*
 702          * Allocate and initialize the Shdr structure.
 703          */
 704         if ((shdr = libld_malloc(sizeof (Shdr))) == NULL)
 705                 return (S_ERROR);
 706         *shdr = *tmpl_isp->is_shdr;
 707         shdr->sh_addr = 0;
 708         shdr->sh_offset = 0;
 709         shdr->sh_size = size;
 710 
 711         /*
 712          * Allocate and initialize the Is_desc structure.
 713          */
 714         if ((isec = libld_calloc(1, sizeof (Is_desc))) == NULL)
 715                 return (S_ERROR);
 716         isec->is_name = tmpl_isp->is_name;
 717         isec->is_shdr = shdr;
 718         isec->is_indata = data;
 719 
 720 
 721         *ret_isec = isec;
 722         *ret_shdr = shdr;
 723         *ret_data = data;
 724         return (1);
 725 }
 726 
 727 /*
 728  * Build a .bss section for allocation of tentative definitions.  Any `static'
 729  * .bss definitions would have been associated to their own .bss sections and
 730  * thus collected from the input files.  `global' .bss definitions are tagged
 731  * as COMMON and do not cause any associated .bss section elements to be
 732  * generated.  Here we add up all these COMMON symbols and generate the .bss
 733  * section required to represent them.
 734  */
 735 uintptr_t
 736 ld_make_bss(Ofl_desc *ofl, Xword size, Xword align, uint_t ident)
 737 {
 738         Shdr            *shdr;
 739         Elf_Data        *data;
 740         Is_desc         *isec;
 741         Os_desc         *osp;
 742         Xword           rsize = (Xword)ofl->ofl_relocbsssz;
 743 
 744         /*
 745          * Allocate header structs. We will set the name ourselves below,
 746          * and there is no entcnt for a BSS. So, the shname and entcnt
 747          * arguments are 0.
 748          */
 749         if (new_section(ofl, SHT_NOBITS, NULL, 0,
 750             &isec, &shdr, &data) == S_ERROR)
 751                 return (S_ERROR);
 752 
 753         data->d_size = (size_t)size;
 754         data->d_align = (size_t)align;
 755 
 756         shdr->sh_size = size;
 757         shdr->sh_addralign = align;
 758 
 759         if (ident == ld_targ.t_id.id_tlsbss) {
 760                 isec->is_name = MSG_ORIG(MSG_SCN_TBSS);
 761                 ofl->ofl_istlsbss = isec;
 762                 shdr->sh_flags |= SHF_TLS;
 763 
 764         } else if (ident == ld_targ.t_id.id_bss) {
 765                 isec->is_name = MSG_ORIG(MSG_SCN_BSS);
 766                 ofl->ofl_isbss = isec;
 767 
 768 #if     defined(_ELF64)
 769         } else if ((ld_targ.t_m.m_mach == EM_AMD64) &&
 770             (ident == ld_targ.t_id.id_lbss)) {
 771                 isec->is_name = MSG_ORIG(MSG_SCN_LBSS);
 772                 ofl->ofl_islbss = isec;
 773                 shdr->sh_flags |= SHF_AMD64_LARGE;
 774 #endif
 775         }
 776 
 777         /*
 778          * Retain this .*bss input section as this will be where global symbol
 779          * references are added.
 780          */
 781         if ((osp = ld_place_section(ofl, isec, NULL, ident, NULL)) ==
 782             (Os_desc *)S_ERROR)
 783                 return (S_ERROR);
 784 
 785         /*
 786          * If relocations exist against a .*bss section, a section symbol must
 787          * be created for the section in the .dynsym symbol table.
 788          */
 789         if (!(osp->os_flags & FLG_OS_OUTREL)) {
 790                 ofl_flag_t      flagtotest;
 791 
 792                 if (ident == ld_targ.t_id.id_tlsbss)
 793                         flagtotest = FLG_OF1_TLSOREL;
 794                 else
 795                         flagtotest = FLG_OF1_BSSOREL;
 796 
 797                 if (ofl->ofl_flags1 & flagtotest) {
 798                         ofl->ofl_dynshdrcnt++;
 799                         osp->os_flags |= FLG_OS_OUTREL;
 800                 }
 801         }
 802 
 803         osp->os_szoutrels = rsize;
 804         return (1);
 805 }
 806 
 807 /*
 808  * Build a SHT_{INIT|FINI|PREINIT}ARRAY section (specified via
 809  * ld -z *array=name).
 810  */
 811 static uintptr_t
 812 make_array(Ofl_desc *ofl, Word shtype, const char *sectname, APlist *alp)
 813 {
 814         uint_t          entcount;
 815         Aliste          idx;
 816         Elf_Data        *data;
 817         Is_desc         *isec;
 818         Shdr            *shdr;
 819         Sym_desc        *sdp;
 820         Rel_desc        reld;
 821         Rela            reloc;
 822         Os_desc         *osp;
 823         uintptr_t       ret = 1;
 824 
 825         if (alp == NULL)
 826                 return (1);
 827 
 828         entcount = 0;
 829         for (APLIST_TRAVERSE(alp, idx, sdp))
 830                 entcount++;
 831 
 832         if (new_section(ofl, shtype, sectname, entcount, &isec, &shdr, &data) ==
 833             S_ERROR)
 834                 return (S_ERROR);
 835 
 836         if ((data->d_buf = libld_calloc(sizeof (Addr), entcount)) == NULL)
 837                 return (S_ERROR);
 838 
 839         if (ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_array, NULL) ==
 840             (Os_desc *)S_ERROR)
 841                 return (S_ERROR);
 842 
 843         osp = isec->is_osdesc;
 844 
 845         if ((ofl->ofl_osinitarray == NULL) && (shtype == SHT_INIT_ARRAY))
 846                 ofl->ofl_osinitarray = osp;
 847         if ((ofl->ofl_ospreinitarray == NULL) && (shtype == SHT_PREINIT_ARRAY))
 848                 ofl->ofl_ospreinitarray = osp;
 849         else if ((ofl->ofl_osfiniarray == NULL) && (shtype == SHT_FINI_ARRAY))
 850                 ofl->ofl_osfiniarray = osp;
 851 
 852         /*
 853          * Create relocations against this section to initialize it to the
 854          * function addresses.
 855          */
 856         reld.rel_isdesc = isec;
 857         reld.rel_aux = NULL;
 858         reld.rel_flags = FLG_REL_LOAD;
 859 
 860         /*
 861          * Fabricate the relocation information (as if a relocation record had
 862          * been input - see init_rel()).
 863          */
 864         reld.rel_rtype = ld_targ.t_m.m_r_arrayaddr;
 865         reld.rel_roffset = 0;
 866         reld.rel_raddend = 0;
 867 
 868         /*
 869          * Create a minimal relocation record to satisfy process_sym_reloc()
 870          * debugging requirements.
 871          */
 872         reloc.r_offset = 0;
 873         reloc.r_info = ELF_R_INFO(0, ld_targ.t_m.m_r_arrayaddr);
 874         reloc.r_addend = 0;
 875 
 876         DBG_CALL(Dbg_reloc_generate(ofl->ofl_lml, osp,
 877             ld_targ.t_m.m_rel_sht_type));
 878         for (APLIST_TRAVERSE(alp, idx, sdp)) {
 879                 reld.rel_sym = sdp;
 880 
 881                 if (ld_process_sym_reloc(ofl, &reld, (Rel *)&reloc, isec,
 882                     MSG_INTL(MSG_STR_COMMAND), 0) == S_ERROR) {
 883                         ret = S_ERROR;
 884                         continue;
 885                 }
 886 
 887                 reld.rel_roffset += (Xword)sizeof (Addr);
 888                 reloc.r_offset = reld.rel_roffset;
 889         }
 890 
 891         return (ret);
 892 }
 893 
 894 /*
 895  * Build a comment section (-Qy option).
 896  */
 897 static uintptr_t
 898 make_comment(Ofl_desc *ofl)
 899 {
 900         Shdr            *shdr;
 901         Elf_Data        *data;
 902         Is_desc         *isec;
 903 
 904         if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_COMMENT), 0,
 905             &isec, &shdr, &data) == S_ERROR)
 906                 return (S_ERROR);
 907 
 908         data->d_buf = (void *)ofl->ofl_sgsid;
 909         data->d_size = strlen(ofl->ofl_sgsid) + 1;
 910         data->d_align = 1;
 911 
 912         shdr->sh_size = (Xword)data->d_size;
 913         shdr->sh_flags = 0;
 914         shdr->sh_addralign = 1;
 915 
 916         return ((uintptr_t)ld_place_section(ofl, isec, NULL,
 917             ld_targ.t_id.id_note, NULL));
 918 }
 919 
 920 /*
 921  * Make the dynamic section.  Calculate the size of any strings referenced
 922  * within this structure, they will be added to the global string table
 923  * (.dynstr).  This routine should be called before make_dynstr().
 924  *
 925  * This routine must be maintained in parallel with update_odynamic()
 926  * in update.c
 927  */
 928 static uintptr_t
 929 make_dynamic(Ofl_desc *ofl)
 930 {
 931         Shdr            *shdr;
 932         Os_desc         *osp;
 933         Elf_Data        *data;
 934         Is_desc         *isec;
 935         size_t          cnt = 0;
 936         Aliste          idx;
 937         Ifl_desc        *ifl;
 938         Sym_desc        *sdp;
 939         size_t          size;
 940         Str_tbl         *strtbl;
 941         ofl_flag_t      flags = ofl->ofl_flags;
 942         int             not_relobj = !(flags & FLG_OF_RELOBJ);
 943         int             unused = 0;
 944 
 945         /*
 946          * Select the required string table.
 947          */
 948         if (OFL_IS_STATIC_OBJ(ofl))
 949                 strtbl = ofl->ofl_strtab;
 950         else
 951                 strtbl = ofl->ofl_dynstrtab;
 952 
 953         /*
 954          * Only a limited subset of DT_ entries apply to relocatable
 955          * objects. See the comment at the head of update_odynamic() in
 956          * update.c for details.
 957          */
 958         if (new_section(ofl, SHT_DYNAMIC, MSG_ORIG(MSG_SCN_DYNAMIC), 0,
 959             &isec, &shdr, &data) == S_ERROR)
 960                 return (S_ERROR);
 961 
 962         /*
 963          * new_section() does not set SHF_ALLOC.  If we're building anything
 964          * besides a relocatable object, then the .dynamic section should
 965          * reside in allocatable memory.
 966          */
 967         if (not_relobj)
 968                 shdr->sh_flags |= SHF_ALLOC;
 969 
 970         /*
 971          * new_section() does not set SHF_WRITE.  If we're building an object
 972          * that specifies an interpretor, then a DT_DEBUG entry is created,
 973          * which is initialized to the applications link-map list at runtime.
 974          */
 975         if (ofl->ofl_osinterp)
 976                 shdr->sh_flags |= SHF_WRITE;
 977 
 978         osp = ofl->ofl_osdynamic =
 979             ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynamic, NULL);
 980 
 981         /*
 982          * Reserve entries for any needed dependencies.
 983          */
 984         for (APLIST_TRAVERSE(ofl->ofl_sos, idx, ifl)) {
 985                 if (!(ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR)))
 986                         continue;
 987 
 988                 /*
 989                  * If this dependency didn't satisfy any symbol references,
 990                  * generate a debugging diagnostic (ld(1) -Dunused can be used
 991                  * to display these).  If this is a standard needed dependency,
 992                  * and -z ignore is in effect, drop the dependency.  Explicitly
 993                  * defined dependencies (i.e., -N dep) don't get dropped, and
 994                  * are flagged as being required to simplify update_odynamic()
 995                  * processing.
 996                  */
 997                 if ((ifl->ifl_flags & FLG_IF_NEEDSTR) ||
 998                     ((ifl->ifl_flags & FLG_IF_DEPREQD) == 0)) {
 999                         if (unused++ == 0)
1000                                 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
1001                         DBG_CALL(Dbg_unused_file(ofl->ofl_lml, ifl->ifl_soname,
1002                             (ifl->ifl_flags & FLG_IF_NEEDSTR), 0));
1003 
1004                         /*
1005                          * Guidance: Remove unused dependency.
1006                          *
1007                          * If -z ignore is in effect, this warning is not
1008                          * needed because we will quietly remove the unused
1009                          * dependency.
1010                          */
1011                         if (OFL_GUIDANCE(ofl, FLG_OFG_NO_UNUSED) &&
1012                             ((ifl->ifl_flags & FLG_IF_IGNORE) == 0))
1013                                 ld_eprintf(ofl, ERR_GUIDANCE,
1014                                     MSG_INTL(MSG_GUIDE_UNUSED),
1015                                     ifl->ifl_soname);
1016 
1017                         if (ifl->ifl_flags & FLG_IF_NEEDSTR)
1018                                 ifl->ifl_flags |= FLG_IF_DEPREQD;
1019                         else if (ifl->ifl_flags & FLG_IF_IGNORE)
1020                                 continue;
1021                 }
1022 
1023                 /*
1024                  * If this object requires a DT_POSFLAG_1 entry, reserve it.
1025                  */
1026                 if ((ifl->ifl_flags & MSK_IF_POSFLAG1) && not_relobj)
1027                         cnt++;
1028 
1029                 if (st_insert(strtbl, ifl->ifl_soname) == -1)
1030                         return (S_ERROR);
1031                 cnt++;
1032 
1033                 /*
1034                  * If the needed entry contains the $ORIGIN token make sure
1035                  * the associated DT_1_FLAGS entry is created.
1036                  */
1037                 if (strstr(ifl->ifl_soname, MSG_ORIG(MSG_STR_ORIGIN))) {
1038                         ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1039                         ofl->ofl_dtflags |= DF_ORIGIN;
1040                 }
1041         }
1042 
1043         if (unused)
1044                 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
1045 
1046         if (not_relobj) {
1047                 /*
1048                  * Reserve entries for any per-symbol auxiliary/filter strings.
1049                  */
1050                 cnt += alist_nitems(ofl->ofl_dtsfltrs);
1051 
1052                 /*
1053                  * Reserve entries for _init() and _fini() section addresses.
1054                  */
1055                 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U),
1056                     SYM_NOHASH, NULL, ofl)) != NULL) &&
1057                     (sdp->sd_ref == REF_REL_NEED) &&
1058                     (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1059                         sdp->sd_flags |= FLG_SY_UPREQD;
1060                         cnt++;
1061                 }
1062                 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U),
1063                     SYM_NOHASH, NULL, ofl)) != NULL) &&
1064                     (sdp->sd_ref == REF_REL_NEED) &&
1065                     (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1066                         sdp->sd_flags |= FLG_SY_UPREQD;
1067                         cnt++;
1068                 }
1069 
1070                 /*
1071                  * Reserve entries for any soname, filter name (shared libs
1072                  * only), run-path pointers, cache names and audit requirements.
1073                  */
1074                 if (ofl->ofl_soname) {
1075                         cnt++;
1076                         if (st_insert(strtbl, ofl->ofl_soname) == -1)
1077                                 return (S_ERROR);
1078                 }
1079                 if (ofl->ofl_filtees) {
1080                         cnt++;
1081                         if (st_insert(strtbl, ofl->ofl_filtees) == -1)
1082                                 return (S_ERROR);
1083 
1084                         /*
1085                          * If the filtees entry contains the $ORIGIN token
1086                          * make sure the associated DT_1_FLAGS entry is created.
1087                          */
1088                         if (strstr(ofl->ofl_filtees,
1089                             MSG_ORIG(MSG_STR_ORIGIN))) {
1090                                 ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1091                                 ofl->ofl_dtflags |= DF_ORIGIN;
1092                         }
1093                 }
1094         }
1095 
1096         if (ofl->ofl_rpath) {
1097                 cnt += 2;       /* DT_RPATH & DT_RUNPATH */
1098                 if (st_insert(strtbl, ofl->ofl_rpath) == -1)
1099                         return (S_ERROR);
1100 
1101                 /*
1102                  * If the rpath entry contains the $ORIGIN token make sure
1103                  * the associated DT_1_FLAGS entry is created.
1104                  */
1105                 if (strstr(ofl->ofl_rpath, MSG_ORIG(MSG_STR_ORIGIN))) {
1106                         ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1107                         ofl->ofl_dtflags |= DF_ORIGIN;
1108                 }
1109         }
1110 
1111         if (not_relobj) {
1112                 Aliste  idx;
1113                 Sg_desc *sgp;
1114 
1115                 if (ofl->ofl_config) {
1116                         cnt++;
1117                         if (st_insert(strtbl, ofl->ofl_config) == -1)
1118                                 return (S_ERROR);
1119 
1120                         /*
1121                          * If the config entry contains the $ORIGIN token
1122                          * make sure the associated DT_1_FLAGS entry is created.
1123                          */
1124                         if (strstr(ofl->ofl_config, MSG_ORIG(MSG_STR_ORIGIN))) {
1125                                 ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1126                                 ofl->ofl_dtflags |= DF_ORIGIN;
1127                         }
1128                 }
1129                 if (ofl->ofl_depaudit) {
1130                         cnt++;
1131                         if (st_insert(strtbl, ofl->ofl_depaudit) == -1)
1132                                 return (S_ERROR);
1133                 }
1134                 if (ofl->ofl_audit) {
1135                         cnt++;
1136                         if (st_insert(strtbl, ofl->ofl_audit) == -1)
1137                                 return (S_ERROR);
1138                 }
1139 
1140                 /*
1141                  * Reserve entries for the DT_HASH, DT_STRTAB, DT_STRSZ,
1142                  * DT_SYMTAB, DT_SYMENT, and DT_CHECKSUM.
1143                  */
1144                 cnt += 6;
1145 
1146                 /*
1147                  * If we are including local functions at the head of
1148                  * the dynsym, then also reserve entries for DT_SUNW_SYMTAB
1149                  * and DT_SUNW_SYMSZ.
1150                  */
1151                 if (OFL_ALLOW_LDYNSYM(ofl))
1152                         cnt += 2;
1153 
1154                 if ((ofl->ofl_dynsymsortcnt > 0) ||
1155                     (ofl->ofl_dyntlssortcnt > 0))
1156                         cnt++;          /* DT_SUNW_SORTENT */
1157 
1158                 if (ofl->ofl_dynsymsortcnt > 0)
1159                         cnt += 2;       /* DT_SUNW_[SYMSORT|SYMSORTSZ] */
1160 
1161                 if (ofl->ofl_dyntlssortcnt > 0)
1162                         cnt += 2;       /* DT_SUNW_[TLSSORT|TLSSORTSZ] */
1163 
1164                 if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
1165                     FLG_OF_VERDEF)
1166                         cnt += 2;               /* DT_VERDEF & DT_VERDEFNUM */
1167 
1168                 if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
1169                     FLG_OF_VERNEED)
1170                         cnt += 2;               /* DT_VERNEED & DT_VERNEEDNUM */
1171 
1172                 if ((flags & FLG_OF_COMREL) && ofl->ofl_relocrelcnt)
1173                         cnt++;                  /* DT_RELACOUNT */
1174 
1175                 if (flags & FLG_OF_TEXTREL) /* DT_TEXTREL */
1176                         cnt++;
1177 
1178                 if (ofl->ofl_osfiniarray)    /* DT_FINI_ARRAY */
1179                         cnt += 2;               /*    DT_FINI_ARRAYSZ */
1180 
1181                 if (ofl->ofl_osinitarray)    /* DT_INIT_ARRAY */
1182                         cnt += 2;               /*    DT_INIT_ARRAYSZ */
1183 
1184                 if (ofl->ofl_ospreinitarray) /* DT_PREINIT_ARRAY & */
1185                         cnt += 2;               /*    DT_PREINIT_ARRAYSZ */
1186 
1187                 /*
1188                  * If we have plt's reserve a DT_PLTRELSZ, DT_PLTREL and
1189                  * DT_JMPREL.
1190                  */
1191                 if (ofl->ofl_pltcnt)
1192                         cnt += 3;
1193 
1194                 /*
1195                  * If plt padding is needed (Sparcv9).
1196                  */
1197                 if (ofl->ofl_pltpad)
1198                         cnt += 2;               /* DT_PLTPAD & DT_PLTPADSZ */
1199 
1200                 /*
1201                  * If we have any relocations reserve a DT_REL, DT_RELSZ and
1202                  * DT_RELENT entry.
1203                  */
1204                 if (ofl->ofl_relocsz)
1205                         cnt += 3;
1206 
1207                 /*
1208                  * If a syminfo section is required create DT_SYMINFO,
1209                  * DT_SYMINSZ, and DT_SYMINENT entries.
1210                  */
1211                 if (flags & FLG_OF_SYMINFO)
1212                         cnt += 3;
1213 
1214                 /*
1215                  * If there are any partially initialized sections allocate
1216                  * DT_MOVETAB, DT_MOVESZ and DT_MOVEENT.
1217                  */
1218                 if (ofl->ofl_osmove)
1219                         cnt += 3;
1220 
1221                 /*
1222                  * Allocate one DT_REGISTER entry for every register symbol.
1223                  */
1224                 cnt += ofl->ofl_regsymcnt;
1225 
1226                 /*
1227                  * Reserve a entry for each '-zrtldinfo=...' specified
1228                  * on the command line.
1229                  */
1230                 for (APLIST_TRAVERSE(ofl->ofl_rtldinfo, idx, sdp))
1231                         cnt++;
1232 
1233                 /*
1234                  * The following entry should only be placed in a segment that
1235                  * is writable.
1236                  */
1237                 if (((sgp = osp->os_sgdesc) != NULL) &&
1238                     (sgp->sg_phdr.p_flags & PF_W) && ofl->ofl_osinterp)
1239                         cnt++;          /* DT_DEBUG */
1240 
1241                 /*
1242                  * Capabilities require a .dynamic entry for the .SUNW_cap
1243                  * section.
1244                  */
1245                 if (ofl->ofl_oscap)
1246                         cnt++;                  /* DT_SUNW_CAP */
1247 
1248                 /*
1249                  * Symbol capabilities require a .dynamic entry for the
1250                  * .SUNW_capinfo section.
1251                  */
1252                 if (ofl->ofl_oscapinfo)
1253                         cnt++;                  /* DT_SUNW_CAPINFO */
1254 
1255                 /*
1256                  * Capabilities chain information requires a .SUNW_capchain
1257                  * entry (DT_SUNW_CAPCHAIN), entry size (DT_SUNW_CAPCHAINENT),
1258                  * and total size (DT_SUNW_CAPCHAINSZ).
1259                  */
1260                 if (ofl->ofl_oscapchain)
1261                         cnt += 3;
1262 
1263                 if (flags & FLG_OF_SYMBOLIC)
1264                         cnt++;                  /* DT_SYMBOLIC */
1265         }
1266 
1267         /*
1268          * Account for Architecture dependent .dynamic entries, and defaults.
1269          */
1270         (*ld_targ.t_mr.mr_mach_make_dynamic)(ofl, &cnt);
1271 
1272         /*
1273          * DT_FLAGS, DT_FLAGS_1, DT_SUNW_STRPAD, and DT_NULL. Also,
1274          * allow room for the unused extra DT_NULLs. These are included
1275          * to allow an ELF editor room to add items later.
1276          */
1277         cnt += 4 + DYNAMIC_EXTRA_ELTS;
1278 
1279         /*
1280          * DT_SUNW_LDMACH. Used to hold the ELF machine code of the
1281          * linker that produced the output object. This information
1282          * allows us to determine whether a given object was linked
1283          * natively, or by a linker running on a different type of
1284          * system. This information can be valuable if one suspects
1285          * that a problem might be due to alignment or byte order issues.
1286          */
1287         cnt++;
1288 
1289         /*
1290          * Determine the size of the section from the number of entries.
1291          */
1292         size = cnt * (size_t)shdr->sh_entsize;
1293 
1294         shdr->sh_size = (Xword)size;
1295         data->d_size = size;
1296 
1297         /*
1298          * There are several tags that are specific to the Solaris osabi
1299          * range which we unconditionally put into any dynamic section
1300          * we create (e.g. DT_SUNW_STRPAD or DT_SUNW_LDMACH). As such,
1301          * any Solaris object with a dynamic section should be tagged as
1302          * ELFOSABI_SOLARIS.
1303          */
1304         ofl->ofl_flags |= FLG_OF_OSABI;
1305 
1306         return ((uintptr_t)ofl->ofl_osdynamic);
1307 }
1308 
1309 /*
1310  * Build the GOT section and its associated relocation entries.
1311  */
1312 uintptr_t
1313 ld_make_got(Ofl_desc *ofl)
1314 {
1315         Elf_Data        *data;
1316         Shdr    *shdr;
1317         Is_desc *isec;
1318         size_t  size = (size_t)ofl->ofl_gotcnt * ld_targ.t_m.m_got_entsize;
1319         size_t  rsize = (size_t)ofl->ofl_relocgotsz;
1320 
1321         if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_GOT), 0,
1322             &isec, &shdr, &data) == S_ERROR)
1323                 return (S_ERROR);
1324 
1325         data->d_size = size;
1326 
1327         shdr->sh_flags |= SHF_WRITE;
1328         shdr->sh_size = (Xword)size;
1329         shdr->sh_entsize = ld_targ.t_m.m_got_entsize;
1330 
1331         ofl->ofl_osgot = ld_place_section(ofl, isec, NULL,
1332             ld_targ.t_id.id_got, NULL);
1333         if (ofl->ofl_osgot == (Os_desc *)S_ERROR)
1334                 return (S_ERROR);
1335 
1336         ofl->ofl_osgot->os_szoutrels = (Xword)rsize;
1337 
1338         return (1);
1339 }
1340 
1341 /*
1342  * Build an interpreter section.
1343  */
1344 static uintptr_t
1345 make_interp(Ofl_desc *ofl)
1346 {
1347         Shdr            *shdr;
1348         Elf_Data        *data;
1349         Is_desc         *isec;
1350         const char      *iname = ofl->ofl_interp;
1351         size_t          size;
1352 
1353         /*
1354          * If -z nointerp is in effect, don't create an interpreter section.
1355          */
1356         if (ofl->ofl_flags1 & FLG_OF1_NOINTRP)
1357                 return (1);
1358 
1359         /*
1360          * An .interp section is always created for a dynamic executable.
1361          * A user can define the interpreter to use.  This definition overrides
1362          * the default that would be recorded in an executable, and triggers
1363          * the creation of an .interp section in any other object.  Presumably
1364          * the user knows what they are doing.  Refer to the generic ELF ABI
1365          * section 5-4, and the ld(1) -I option.
1366          */
1367         if (((ofl->ofl_flags & (FLG_OF_DYNAMIC | FLG_OF_EXEC |
1368             FLG_OF_RELOBJ)) != (FLG_OF_DYNAMIC | FLG_OF_EXEC)) && !iname)
1369                 return (1);
1370 
1371         /*
1372          * In the case of a dynamic executable, supply a default interpreter
1373          * if the user has not specified their own.
1374          */
1375         if (iname == NULL)
1376                 iname = ofl->ofl_interp = ld_targ.t_m.m_def_interp;
1377 
1378         size = strlen(iname) + 1;
1379 
1380         if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_INTERP), 0,
1381             &isec, &shdr, &data) == S_ERROR)
1382                 return (S_ERROR);
1383 
1384         data->d_size = size;
1385         shdr->sh_size = (Xword)size;
1386         data->d_align = shdr->sh_addralign = 1;
1387 
1388         ofl->ofl_osinterp =
1389             ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_interp, NULL);
1390         return ((uintptr_t)ofl->ofl_osinterp);
1391 }
1392 
1393 /*
1394  * Common function used to build the SHT_SUNW_versym section, SHT_SUNW_syminfo
1395  * section, and SHT_SUNW_capinfo section.  Each of these sections provide
1396  * additional symbol information, and their size parallels the associated
1397  * symbol table.
1398  */
1399 static Os_desc *
1400 make_sym_sec(Ofl_desc *ofl, const char *sectname, Word stype, int ident)
1401 {
1402         Shdr            *shdr;
1403         Elf_Data        *data;
1404         Is_desc         *isec;
1405 
1406         /*
1407          * We don't know the size of this section yet, so set it to 0.  The
1408          * size gets filled in after the associated symbol table is sized.
1409          */
1410         if (new_section(ofl, stype, sectname, 0, &isec, &shdr, &data) ==
1411             S_ERROR)
1412                 return ((Os_desc *)S_ERROR);
1413 
1414         return (ld_place_section(ofl, isec, NULL, ident, NULL));
1415 }
1416 
1417 /*
1418  * Determine whether a symbol capability is redundant because the object
1419  * capabilities are more restrictive.
1420  */
1421 inline static int
1422 is_cap_redundant(Objcapset *ocapset, Objcapset *scapset)
1423 {
1424         Alist           *oalp, *salp;
1425         elfcap_mask_t   omsk, smsk;
1426 
1427         /*
1428          * Inspect any platform capabilities.  If the object defines platform
1429          * capabilities, then the object will only be loaded for those
1430          * platforms.  A symbol capability set that doesn't define the same
1431          * platforms is redundant, and a symbol capability that does not provide
1432          * at least one platform name that matches a platform name in the object
1433          * capabilities will never execute (as the object wouldn't have been
1434          * loaded).
1435          */
1436         oalp = ocapset->oc_plat.cl_val;
1437         salp = scapset->oc_plat.cl_val;
1438         if (oalp && ((salp == NULL) || cap_names_match(oalp, salp)))
1439                 return (1);
1440 
1441         /*
1442          * If the symbol capability set defines platforms, and the object
1443          * doesn't, then the symbol set is more restrictive.
1444          */
1445         if (salp && (oalp == NULL))
1446                 return (0);
1447 
1448         /*
1449          * Next, inspect any machine name capabilities.  If the object defines
1450          * machine name capabilities, then the object will only be loaded for
1451          * those machines.  A symbol capability set that doesn't define the same
1452          * machine names is redundant, and a symbol capability that does not
1453          * provide at least one machine name that matches a machine name in the
1454          * object capabilities will never execute (as the object wouldn't have
1455          * been loaded).
1456          */
1457         oalp = ocapset->oc_plat.cl_val;
1458         salp = scapset->oc_plat.cl_val;
1459         if (oalp && ((salp == NULL) || cap_names_match(oalp, salp)))
1460                 return (1);
1461 
1462         /*
1463          * If the symbol capability set defines machine names, and the object
1464          * doesn't, then the symbol set is more restrictive.
1465          */
1466         if (salp && (oalp == NULL))
1467                 return (0);
1468 
1469         /*
1470          * Next, inspect any hardware capabilities.  If the objects hardware
1471          * capabilities are greater than or equal to that of the symbols
1472          * capabilities, then the symbol capability set is redundant.  If the
1473          * symbols hardware capabilities are greater that the objects, then the
1474          * symbol set is more restrictive.
1475          *
1476          * Note that this is a somewhat arbitrary definition, as each capability
1477          * bit is independent of the others, and some of the higher order bits
1478          * could be considered to be less important than lower ones.  However,
1479          * this is the only reasonable non-subjective definition.
1480          */
1481         omsk = ocapset->oc_hw_2.cm_val;
1482         smsk = scapset->oc_hw_2.cm_val;
1483         if ((omsk > smsk) || (omsk && (omsk == smsk)))
1484                 return (1);
1485         if (omsk < smsk)
1486                 return (0);
1487 
1488         /*
1489          * Finally, inspect the remaining hardware capabilities.
1490          */
1491         omsk = ocapset->oc_hw_1.cm_val;
1492         smsk = scapset->oc_hw_1.cm_val;
1493         if ((omsk > smsk) || (omsk && (omsk == smsk)))
1494                 return (1);
1495 
1496         return (0);
1497 }
1498 
1499 /*
1500  * Capabilities values might have been assigned excluded values.  These
1501  * excluded values should be removed before calculating any capabilities
1502  * sections size.
1503  */
1504 static void
1505 capmask_value(Lm_list *lml, Word type, Capmask *capmask, int *title)
1506 {
1507         /*
1508          * First determine whether any bits should be excluded.
1509          */
1510         if ((capmask->cm_val & capmask->cm_exc) == 0)
1511                 return;
1512 
1513         DBG_CALL(Dbg_cap_post_title(lml, title));
1514 
1515         DBG_CALL(Dbg_cap_val_entry(lml, DBG_STATE_CURRENT, type,
1516             capmask->cm_val, ld_targ.t_m.m_mach));
1517         DBG_CALL(Dbg_cap_val_entry(lml, DBG_STATE_EXCLUDE, type,
1518             capmask->cm_exc, ld_targ.t_m.m_mach));
1519 
1520         capmask->cm_val &= ~capmask->cm_exc;
1521 
1522         DBG_CALL(Dbg_cap_val_entry(lml, DBG_STATE_RESOLVED, type,
1523             capmask->cm_val, ld_targ.t_m.m_mach));
1524 }
1525 
1526 static void
1527 capstr_value(Lm_list *lml, Word type, Caplist *caplist, int *title)
1528 {
1529         Aliste  idx1, idx2;
1530         char    *estr;
1531         Capstr  *capstr;
1532         Boolean found = FALSE;
1533 
1534         /*
1535          * First determine whether any strings should be excluded.
1536          */
1537         for (APLIST_TRAVERSE(caplist->cl_exc, idx1, estr)) {
1538                 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) {
1539                         if (strcmp(estr, capstr->cs_str) == 0) {
1540                                 found = TRUE;
1541                                 break;
1542                         }
1543                 }
1544         }
1545 
1546         if (found == FALSE)
1547                 return;
1548 
1549         /*
1550          * Traverse the current strings, then delete the excluded strings,
1551          * and finally display the resolved strings.
1552          */
1553         if (DBG_ENABLED) {
1554                 Dbg_cap_post_title(lml, title);
1555                 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) {
1556                         Dbg_cap_ptr_entry(lml, DBG_STATE_CURRENT, type,
1557                             capstr->cs_str);
1558                 }
1559         }
1560         for (APLIST_TRAVERSE(caplist->cl_exc, idx1, estr)) {
1561                 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) {
1562                         if (strcmp(estr, capstr->cs_str) == 0) {
1563                                 DBG_CALL(Dbg_cap_ptr_entry(lml,
1564                                     DBG_STATE_EXCLUDE, type, capstr->cs_str));
1565                                 alist_delete(caplist->cl_val, &idx2);
1566                                 break;
1567                         }
1568                 }
1569         }
1570         if (DBG_ENABLED) {
1571                 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) {
1572                         Dbg_cap_ptr_entry(lml, DBG_STATE_RESOLVED, type,
1573                             capstr->cs_str);
1574                 }
1575         }
1576 }
1577 
1578 /*
1579  * Build a capabilities section.
1580  */
1581 #define CAP_UPDATE(cap, capndx, tag, val)       \
1582         cap->c_tag = tag; \
1583         cap->c_un.c_val = val; \
1584         cap++, capndx++;
1585 
1586 static uintptr_t
1587 make_cap(Ofl_desc *ofl, Word shtype, const char *shname, int ident)
1588 {
1589         Shdr            *shdr;
1590         Elf_Data        *data;
1591         Is_desc         *isec;
1592         Cap             *cap;
1593         size_t          size = 0;
1594         Word            capndx = 0;
1595         Str_tbl         *strtbl;
1596         Objcapset       *ocapset = &ofl->ofl_ocapset;
1597         Aliste          idx1;
1598         Capstr          *capstr;
1599         int             title = 0;
1600 
1601         /*
1602          * Determine which string table to use for any CA_SUNW_MACH,
1603          * CA_SUNW_PLAT, or CA_SUNW_ID strings.
1604          */
1605         if (OFL_IS_STATIC_OBJ(ofl))
1606                 strtbl = ofl->ofl_strtab;
1607         else
1608                 strtbl = ofl->ofl_dynstrtab;
1609 
1610         /*
1611          * If symbol capabilities have been requested, but none have been
1612          * created, warn the user.  This scenario can occur if none of the
1613          * input relocatable objects defined any object capabilities.
1614          */
1615         if ((ofl->ofl_flags & FLG_OF_OTOSCAP) && (ofl->ofl_capsymcnt == 0))
1616                 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_CAP_NOSYMSFOUND));
1617 
1618         /*
1619          * If symbol capabilities have been collected, but no symbols are left
1620          * referencing these capabilities, promote the capability groups back
1621          * to an object capability definition.
1622          */
1623         if ((ofl->ofl_flags & FLG_OF_OTOSCAP) && ofl->ofl_capsymcnt &&
1624             (ofl->ofl_capfamilies == NULL)) {
1625                 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_CAP_NOSYMSFOUND));
1626                 ld_cap_move_symtoobj(ofl);
1627                 ofl->ofl_capsymcnt = 0;
1628                 ofl->ofl_capgroups = NULL;
1629                 ofl->ofl_flags &= ~FLG_OF_OTOSCAP;
1630         }
1631 
1632         /*
1633          * Remove any excluded capabilities.
1634          */
1635         capstr_value(ofl->ofl_lml, CA_SUNW_PLAT, &ocapset->oc_plat, &title);
1636         capstr_value(ofl->ofl_lml, CA_SUNW_MACH, &ocapset->oc_mach, &title);
1637         capmask_value(ofl->ofl_lml, CA_SUNW_HW_2, &ocapset->oc_hw_2, &title);
1638         capmask_value(ofl->ofl_lml, CA_SUNW_HW_1, &ocapset->oc_hw_1, &title);
1639         capmask_value(ofl->ofl_lml, CA_SUNW_SF_1, &ocapset->oc_sf_1, &title);
1640 
1641         /*
1642          * Determine how many entries are required for any object capabilities.
1643          */
1644         size += alist_nitems(ocapset->oc_plat.cl_val);
1645         size += alist_nitems(ocapset->oc_mach.cl_val);
1646         if (ocapset->oc_hw_2.cm_val)
1647                 size++;
1648         if (ocapset->oc_hw_1.cm_val)
1649                 size++;
1650         if (ocapset->oc_sf_1.cm_val)
1651                 size++;
1652 
1653         /*
1654          * Only identify a capabilities group if the group has content.  If a
1655          * capabilities identifier exists, and no other capabilities have been
1656          * supplied, remove the identifier.  This scenario could exist if a
1657          * user mistakenly defined a lone identifier, or if an identified group
1658          * was overridden so as to clear the existing capabilities and the
1659          * identifier was not also cleared.
1660          */
1661         if (ocapset->oc_id.cs_str) {
1662                 if (size)
1663                         size++;
1664                 else
1665                         ocapset->oc_id.cs_str = NULL;
1666         }
1667         if (size)
1668                 size++;                 /* Add CA_SUNW_NULL */
1669 
1670         /*
1671          * Determine how many entries are required for any symbol capabilities.
1672          */
1673         if (ofl->ofl_capsymcnt) {
1674                 /*
1675                  * If there are no object capabilities, a CA_SUNW_NULL entry
1676                  * is required before any symbol capabilities.
1677                  */
1678                 if (size == 0)
1679                         size++;
1680                 size += ofl->ofl_capsymcnt;
1681         }
1682 
1683         if (size == 0)
1684                 return (NULL);
1685 
1686         if (new_section(ofl, shtype, shname, size, &isec,
1687             &shdr, &data) == S_ERROR)
1688                 return (S_ERROR);
1689 
1690         if ((data->d_buf = libld_malloc(shdr->sh_size)) == NULL)
1691                 return (S_ERROR);
1692 
1693         cap = (Cap *)data->d_buf;
1694 
1695         /*
1696          * Fill in any object capabilities.  If there is an identifier, then the
1697          * identifier comes first.  The remaining items follow in precedence
1698          * order, although the order isn't important for runtime verification.
1699          */
1700         if (ocapset->oc_id.cs_str) {
1701                 ofl->ofl_flags |= FLG_OF_CAPSTRS;
1702                 if (st_insert(strtbl, ocapset->oc_id.cs_str) == -1)
1703                         return (S_ERROR);
1704                 ocapset->oc_id.cs_ndx = capndx;
1705                 CAP_UPDATE(cap, capndx, CA_SUNW_ID, 0);
1706         }
1707         if (ocapset->oc_plat.cl_val) {
1708                 ofl->ofl_flags |= (FLG_OF_PTCAP | FLG_OF_CAPSTRS);
1709 
1710                 /*
1711                  * Insert any platform name strings in the appropriate string
1712                  * table.  The capability value can't be filled in yet, as the
1713                  * final offset of the strings isn't known until later.
1714                  */
1715                 for (ALIST_TRAVERSE(ocapset->oc_plat.cl_val, idx1, capstr)) {
1716                         if (st_insert(strtbl, capstr->cs_str) == -1)
1717                                 return (S_ERROR);
1718                         capstr->cs_ndx = capndx;
1719                         CAP_UPDATE(cap, capndx, CA_SUNW_PLAT, 0);
1720                 }
1721         }
1722         if (ocapset->oc_mach.cl_val) {
1723                 ofl->ofl_flags |= (FLG_OF_PTCAP | FLG_OF_CAPSTRS);
1724 
1725                 /*
1726                  * Insert the machine name strings in the appropriate string
1727                  * table.  The capability value can't be filled in yet, as the
1728                  * final offset of the strings isn't known until later.
1729                  */
1730                 for (ALIST_TRAVERSE(ocapset->oc_mach.cl_val, idx1, capstr)) {
1731                         if (st_insert(strtbl, capstr->cs_str) == -1)
1732                                 return (S_ERROR);
1733                         capstr->cs_ndx = capndx;
1734                         CAP_UPDATE(cap, capndx, CA_SUNW_MACH, 0);
1735                 }
1736         }
1737         if (ocapset->oc_hw_2.cm_val) {
1738                 ofl->ofl_flags |= FLG_OF_PTCAP;
1739                 CAP_UPDATE(cap, capndx, CA_SUNW_HW_2, ocapset->oc_hw_2.cm_val);
1740         }
1741         if (ocapset->oc_hw_1.cm_val) {
1742                 ofl->ofl_flags |= FLG_OF_PTCAP;
1743                 CAP_UPDATE(cap, capndx, CA_SUNW_HW_1, ocapset->oc_hw_1.cm_val);
1744         }
1745         if (ocapset->oc_sf_1.cm_val) {
1746                 ofl->ofl_flags |= FLG_OF_PTCAP;
1747                 CAP_UPDATE(cap, capndx, CA_SUNW_SF_1, ocapset->oc_sf_1.cm_val);
1748         }
1749         CAP_UPDATE(cap, capndx, CA_SUNW_NULL, 0);
1750 
1751         /*
1752          * Fill in any symbol capabilities.
1753          */
1754         if (ofl->ofl_capgroups) {
1755                 Cap_group       *cgp;
1756 
1757                 for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
1758                         Objcapset       *scapset = &cgp->cg_set;
1759                         Aliste          idx2;
1760                         Is_desc         *isp;
1761 
1762                         cgp->cg_ndx = capndx;
1763 
1764                         if (scapset->oc_id.cs_str) {
1765                                 ofl->ofl_flags |= FLG_OF_CAPSTRS;
1766                                 /*
1767                                  * Insert the identifier string in the
1768                                  * appropriate string table.  The capability
1769                                  * value can't be filled in yet, as the final
1770                                  * offset of the string isn't known until later.
1771                                  */
1772                                 if (st_insert(strtbl,
1773                                     scapset->oc_id.cs_str) == -1)
1774                                         return (S_ERROR);
1775                                 scapset->oc_id.cs_ndx = capndx;
1776                                 CAP_UPDATE(cap, capndx, CA_SUNW_ID, 0);
1777                         }
1778 
1779                         if (scapset->oc_plat.cl_val) {
1780                                 ofl->ofl_flags |= FLG_OF_CAPSTRS;
1781 
1782                                 /*
1783                                  * Insert the platform name string in the
1784                                  * appropriate string table.  The capability
1785                                  * value can't be filled in yet, as the final
1786                                  * offset of the string isn't known until later.
1787                                  */
1788                                 for (ALIST_TRAVERSE(scapset->oc_plat.cl_val,
1789                                     idx2, capstr)) {
1790                                         if (st_insert(strtbl,
1791                                             capstr->cs_str) == -1)
1792                                                 return (S_ERROR);
1793                                         capstr->cs_ndx = capndx;
1794                                         CAP_UPDATE(cap, capndx,
1795                                             CA_SUNW_PLAT, 0);
1796                                 }
1797                         }
1798                         if (scapset->oc_mach.cl_val) {
1799                                 ofl->ofl_flags |= FLG_OF_CAPSTRS;
1800 
1801                                 /*
1802                                  * Insert the machine name string in the
1803                                  * appropriate string table.  The capability
1804                                  * value can't be filled in yet, as the final
1805                                  * offset of the string isn't known until later.
1806                                  */
1807                                 for (ALIST_TRAVERSE(scapset->oc_mach.cl_val,
1808                                     idx2, capstr)) {
1809                                         if (st_insert(strtbl,
1810                                             capstr->cs_str) == -1)
1811                                                 return (S_ERROR);
1812                                         capstr->cs_ndx = capndx;
1813                                         CAP_UPDATE(cap, capndx,
1814                                             CA_SUNW_MACH, 0);
1815                                 }
1816                         }
1817                         if (scapset->oc_hw_2.cm_val) {
1818                                 CAP_UPDATE(cap, capndx, CA_SUNW_HW_2,
1819                                     scapset->oc_hw_2.cm_val);
1820                         }
1821                         if (scapset->oc_hw_1.cm_val) {
1822                                 CAP_UPDATE(cap, capndx, CA_SUNW_HW_1,
1823                                     scapset->oc_hw_1.cm_val);
1824                         }
1825                         if (scapset->oc_sf_1.cm_val) {
1826                                 CAP_UPDATE(cap, capndx, CA_SUNW_SF_1,
1827                                     scapset->oc_sf_1.cm_val);
1828                         }
1829                         CAP_UPDATE(cap, capndx, CA_SUNW_NULL, 0);
1830 
1831                         /*
1832                          * If any object capabilities are available, determine
1833                          * whether these symbol capabilities are less
1834                          * restrictive, and hence redundant.
1835                          */
1836                         if (((ofl->ofl_flags & FLG_OF_PTCAP) == 0) ||
1837                             (is_cap_redundant(ocapset, scapset) == 0))
1838                                 continue;
1839 
1840                         /*
1841                          * Indicate any files that provide redundant symbol
1842                          * capabilities.
1843                          */
1844                         for (APLIST_TRAVERSE(cgp->cg_secs, idx2, isp)) {
1845                                 ld_eprintf(ofl, ERR_WARNING,
1846                                     MSG_INTL(MSG_CAP_REDUNDANT),
1847                                     isp->is_file->ifl_name,
1848                                     EC_WORD(isp->is_scnndx), isp->is_name);
1849                         }
1850                 }
1851         }
1852 
1853         /*
1854          * If capabilities strings are required, the sh_info field of the
1855          * section header will be set to the associated string table.
1856          */
1857         if (ofl->ofl_flags & FLG_OF_CAPSTRS)
1858                 shdr->sh_flags |= SHF_INFO_LINK;
1859 
1860         /*
1861          * Place these capabilities in the output file.
1862          */
1863         if ((ofl->ofl_oscap = ld_place_section(ofl, isec,
1864             NULL, ident, NULL)) == (Os_desc *)S_ERROR)
1865                 return (S_ERROR);
1866 
1867         /*
1868          * If symbol capabilities are required, then a .SUNW_capinfo section is
1869          * also created.  This table will eventually be sized to match the
1870          * associated symbol table.
1871          */
1872         if (ofl->ofl_capfamilies) {
1873                 if ((ofl->ofl_oscapinfo = make_sym_sec(ofl,
1874                     MSG_ORIG(MSG_SCN_SUNWCAPINFO), SHT_SUNW_capinfo,
1875                     ld_targ.t_id.id_capinfo)) == (Os_desc *)S_ERROR)
1876                         return (S_ERROR);
1877 
1878                 /*
1879                  * If we're generating a dynamic object, capabilities family
1880                  * members are maintained in a .SUNW_capchain section.
1881                  */
1882                 if (ofl->ofl_capchaincnt &&
1883                     ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
1884                         if (new_section(ofl, SHT_SUNW_capchain,
1885                             MSG_ORIG(MSG_SCN_SUNWCAPCHAIN),
1886                             ofl->ofl_capchaincnt, &isec, &shdr,
1887                             &data) == S_ERROR)
1888                                 return (S_ERROR);
1889 
1890                         ofl->ofl_oscapchain = ld_place_section(ofl, isec,
1891                             NULL, ld_targ.t_id.id_capchain, NULL);
1892                         if (ofl->ofl_oscapchain == (Os_desc *)S_ERROR)
1893                                 return (S_ERROR);
1894 
1895                 }
1896         }
1897         return (1);
1898 }
1899 #undef  CAP_UPDATE
1900 
1901 /*
1902  * Build the PLT section and its associated relocation entries.
1903  */
1904 static uintptr_t
1905 make_plt(Ofl_desc *ofl)
1906 {
1907         Shdr            *shdr;
1908         Elf_Data        *data;
1909         Is_desc         *isec;
1910         size_t          size = ld_targ.t_m.m_plt_reservsz +
1911             (((size_t)ofl->ofl_pltcnt + (size_t)ofl->ofl_pltpad) *
1912             ld_targ.t_m.m_plt_entsize);
1913         size_t          rsize = (size_t)ofl->ofl_relocpltsz;
1914 
1915         /*
1916          * On sparc, account for the NOP at the end of the plt.
1917          */
1918         if (ld_targ.t_m.m_mach == LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
1919                 size += sizeof (Word);
1920 
1921         if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_PLT), 0,
1922             &isec, &shdr, &data) == S_ERROR)
1923                 return (S_ERROR);
1924 
1925         data->d_size = size;
1926         data->d_align = ld_targ.t_m.m_plt_align;
1927 
1928         shdr->sh_flags = ld_targ.t_m.m_plt_shf_flags;
1929         shdr->sh_size = (Xword)size;
1930         shdr->sh_addralign = ld_targ.t_m.m_plt_align;
1931         shdr->sh_entsize = ld_targ.t_m.m_plt_entsize;
1932 
1933         ofl->ofl_osplt = ld_place_section(ofl, isec, NULL,
1934             ld_targ.t_id.id_plt, NULL);
1935         if (ofl->ofl_osplt == (Os_desc *)S_ERROR)
1936                 return (S_ERROR);
1937 
1938         ofl->ofl_osplt->os_szoutrels = (Xword)rsize;
1939 
1940         return (1);
1941 }
1942 
1943 /*
1944  * Make the hash table.  Only built for dynamic executables and shared
1945  * libraries, and provides hashed lookup into the global symbol table
1946  * (.dynsym) for the run-time linker to resolve symbol lookups.
1947  */
1948 static uintptr_t
1949 make_hash(Ofl_desc *ofl)
1950 {
1951         Shdr            *shdr;
1952         Elf_Data        *data;
1953         Is_desc         *isec;
1954         size_t          size;
1955         Word            nsyms = ofl->ofl_globcnt;
1956         size_t          cnt;
1957 
1958         /*
1959          * Allocate section header structures. We set entcnt to 0
1960          * because it's going to change after we place this section.
1961          */
1962         if (new_section(ofl, SHT_HASH, MSG_ORIG(MSG_SCN_HASH), 0,
1963             &isec, &shdr, &data) == S_ERROR)
1964                 return (S_ERROR);
1965 
1966         /*
1967          * Place the section first since it will affect the local symbol
1968          * count.
1969          */
1970         ofl->ofl_oshash =
1971             ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_hash, NULL);
1972         if (ofl->ofl_oshash == (Os_desc *)S_ERROR)
1973                 return (S_ERROR);
1974 
1975         /*
1976          * Calculate the number of output hash buckets.
1977          */
1978         ofl->ofl_hashbkts = findprime(nsyms);
1979 
1980         /*
1981          * The size of the hash table is determined by
1982          *
1983          *      i.      the initial nbucket and nchain entries (2)
1984          *      ii.     the number of buckets (calculated above)
1985          *      iii.    the number of chains (this is based on the number of
1986          *              symbols in the .dynsym array).
1987          */
1988         cnt = 2 + ofl->ofl_hashbkts + DYNSYM_ALL_CNT(ofl);
1989         size = cnt * shdr->sh_entsize;
1990 
1991         /*
1992          * Finalize the section header and data buffer initialization.
1993          */
1994         if ((data->d_buf = libld_calloc(size, 1)) == NULL)
1995                 return (S_ERROR);
1996         data->d_size = size;
1997         shdr->sh_size = (Xword)size;
1998 
1999         return (1);
2000 }
2001 
2002 /*
2003  * Generate the standard symbol table.  Contains all locals and globals,
2004  * and resides in a non-allocatable section (ie. it can be stripped).
2005  */
2006 static uintptr_t
2007 make_symtab(Ofl_desc *ofl)
2008 {
2009         Shdr            *shdr;
2010         Elf_Data        *data;
2011         Is_desc         *isec;
2012         Is_desc         *xisec = 0;
2013         size_t          size;
2014         Word            symcnt;
2015 
2016         /*
2017          * Create the section headers. Note that we supply an ent_cnt
2018          * of 0. We won't know the count until the section has been placed.
2019          */
2020         if (new_section(ofl, SHT_SYMTAB, MSG_ORIG(MSG_SCN_SYMTAB), 0,
2021             &isec, &shdr, &data) == S_ERROR)
2022                 return (S_ERROR);
2023 
2024         /*
2025          * Place the section first since it will affect the local symbol
2026          * count.
2027          */
2028         if ((ofl->ofl_ossymtab = ld_place_section(ofl, isec, NULL,
2029             ld_targ.t_id.id_symtab, NULL)) == (Os_desc *)S_ERROR)
2030                 return (S_ERROR);
2031 
2032         /*
2033          * At this point we've created all but the 'shstrtab' section.
2034          * Determine if we have to use 'Extended Sections'.  If so - then
2035          * also create a SHT_SYMTAB_SHNDX section.
2036          */
2037         if ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE) {
2038                 Shdr            *xshdr;
2039                 Elf_Data        *xdata;
2040 
2041                 if (new_section(ofl, SHT_SYMTAB_SHNDX,
2042                     MSG_ORIG(MSG_SCN_SYMTAB_SHNDX), 0, &xisec,
2043                     &xshdr, &xdata) == S_ERROR)
2044                         return (S_ERROR);
2045 
2046                 if ((ofl->ofl_ossymshndx = ld_place_section(ofl, xisec, NULL,
2047                     ld_targ.t_id.id_symtab_ndx, NULL)) == (Os_desc *)S_ERROR)
2048                         return (S_ERROR);
2049         }
2050 
2051         /*
2052          * Calculated number of symbols, which need to be augmented by
2053          * the (yet to be created) .shstrtab entry.
2054          */
2055         symcnt = (size_t)(1 + SYMTAB_ALL_CNT(ofl));
2056         size = symcnt * shdr->sh_entsize;
2057 
2058         /*
2059          * Finalize the section header and data buffer initialization.
2060          */
2061         data->d_size = size;
2062         shdr->sh_size = (Xword)size;
2063 
2064         /*
2065          * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too.
2066          */
2067         if (xisec) {
2068                 size_t  xsize = symcnt * sizeof (Word);
2069 
2070                 xisec->is_indata->d_size = xsize;
2071                 xisec->is_shdr->sh_size = (Xword)xsize;
2072         }
2073 
2074         return (1);
2075 }
2076 
2077 /*
2078  * Build a dynamic symbol table. These tables reside in the text
2079  * segment of a dynamic executable or shared library.
2080  *
2081  *      .SUNW_ldynsym contains local function symbols
2082  *      .dynsym contains only globals symbols
2083  *
2084  * The two tables are created adjacent to each other, with .SUNW_ldynsym
2085  * coming first.
2086  */
2087 static uintptr_t
2088 make_dynsym(Ofl_desc *ofl)
2089 {
2090         Shdr            *shdr, *lshdr;
2091         Elf_Data        *data, *ldata;
2092         Is_desc         *isec, *lisec;
2093         size_t          size;
2094         Xword           cnt;
2095         int             allow_ldynsym;
2096 
2097         /*
2098          * Unless explicitly disabled, always produce a .SUNW_ldynsym section
2099          * when it is allowed by the file type, even if the resulting
2100          * table only ends up with a single STT_FILE in it. There are
2101          * two reasons: (1) It causes the generation of the DT_SUNW_SYMTAB
2102          * entry in the .dynamic section, which is something we would
2103          * like to encourage, and (2) Without it, we cannot generate
2104          * the associated .SUNW_dyn[sym|tls]sort sections, which are of
2105          * value to DTrace.
2106          *
2107          * In practice, it is extremely rare for an object not to have
2108          * local symbols for .SUNW_ldynsym, so 99% of the time, we'd be
2109          * doing it anyway.
2110          */
2111         allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
2112 
2113         /*
2114          * Create the section headers. Note that we supply an ent_cnt
2115          * of 0. We won't know the count until the section has been placed.
2116          */
2117         if (allow_ldynsym && new_section(ofl, SHT_SUNW_LDYNSYM,
2118             MSG_ORIG(MSG_SCN_LDYNSYM), 0, &lisec, &lshdr, &ldata) == S_ERROR)
2119                 return (S_ERROR);
2120 
2121         if (new_section(ofl, SHT_DYNSYM, MSG_ORIG(MSG_SCN_DYNSYM), 0,
2122             &isec, &shdr, &data) == S_ERROR)
2123                 return (S_ERROR);
2124 
2125         /*
2126          * Place the section(s) first since it will affect the local symbol
2127          * count.
2128          */
2129         if (allow_ldynsym &&
2130             ((ofl->ofl_osldynsym = ld_place_section(ofl, lisec, NULL,
2131             ld_targ.t_id.id_ldynsym, NULL)) == (Os_desc *)S_ERROR))
2132                 return (S_ERROR);
2133         ofl->ofl_osdynsym =
2134             ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynsym, NULL);
2135         if (ofl->ofl_osdynsym == (Os_desc *)S_ERROR)
2136                 return (S_ERROR);
2137 
2138         cnt = DYNSYM_ALL_CNT(ofl);
2139         size = (size_t)cnt * shdr->sh_entsize;
2140 
2141         /*
2142          * Finalize the section header and data buffer initialization.
2143          */
2144         data->d_size = size;
2145         shdr->sh_size = (Xword)size;
2146 
2147         /*
2148          * An ldynsym contains local function symbols. It is not
2149          * used for linking, but if present, serves to allow better
2150          * stack traces to be generated in contexts where the symtab
2151          * is not available. (dladdr(), or stripped executable/library files).
2152          */
2153         if (allow_ldynsym) {
2154                 cnt = 1 + ofl->ofl_dynlocscnt + ofl->ofl_dynscopecnt;
2155                 size = (size_t)cnt * shdr->sh_entsize;
2156 
2157                 ldata->d_size = size;
2158                 lshdr->sh_size = (Xword)size;
2159         }
2160 
2161         return (1);
2162 }
2163 
2164 /*
2165  * Build .SUNW_dynsymsort and/or .SUNW_dyntlssort sections. These are
2166  * index sections for the .SUNW_ldynsym/.dynsym pair that present data
2167  * and function symbols sorted by address.
2168  */
2169 static uintptr_t
2170 make_dynsort(Ofl_desc *ofl)
2171 {
2172         Shdr            *shdr;
2173         Elf_Data        *data;
2174         Is_desc         *isec;
2175 
2176         /* Only do it if the .SUNW_ldynsym section is present */
2177         if (!OFL_ALLOW_LDYNSYM(ofl))
2178                 return (1);
2179 
2180         /* .SUNW_dynsymsort */
2181         if (ofl->ofl_dynsymsortcnt > 0) {
2182                 if (new_section(ofl, SHT_SUNW_symsort,
2183                     MSG_ORIG(MSG_SCN_DYNSYMSORT), ofl->ofl_dynsymsortcnt,
2184                     &isec, &shdr, &data) == S_ERROR)
2185                 return (S_ERROR);
2186 
2187                 if ((ofl->ofl_osdynsymsort = ld_place_section(ofl, isec, NULL,
2188                     ld_targ.t_id.id_dynsort, NULL)) == (Os_desc *)S_ERROR)
2189                         return (S_ERROR);
2190         }
2191 
2192         /* .SUNW_dyntlssort */
2193         if (ofl->ofl_dyntlssortcnt > 0) {
2194                 if (new_section(ofl, SHT_SUNW_tlssort,
2195                     MSG_ORIG(MSG_SCN_DYNTLSSORT),
2196                     ofl->ofl_dyntlssortcnt, &isec, &shdr, &data) == S_ERROR)
2197                 return (S_ERROR);
2198 
2199                 if ((ofl->ofl_osdyntlssort = ld_place_section(ofl, isec, NULL,
2200                     ld_targ.t_id.id_dynsort, NULL)) == (Os_desc *)S_ERROR)
2201                         return (S_ERROR);
2202         }
2203 
2204         return (1);
2205 }
2206 
2207 /*
2208  * Helper routine for make_dynsym_shndx. Builds a
2209  * a SHT_SYMTAB_SHNDX for .dynsym or .SUNW_ldynsym, without knowing
2210  * which one it is.
2211  */
2212 static uintptr_t
2213 make_dyn_shndx(Ofl_desc *ofl, const char *shname, Os_desc *symtab,
2214     Os_desc **ret_os)
2215 {
2216         Is_desc         *isec;
2217         Is_desc         *dynsymisp;
2218         Shdr            *shdr, *dynshdr;
2219         Elf_Data        *data;
2220 
2221         dynsymisp = ld_os_first_isdesc(symtab);
2222         dynshdr = dynsymisp->is_shdr;
2223 
2224         if (new_section(ofl, SHT_SYMTAB_SHNDX, shname,
2225             (dynshdr->sh_size / dynshdr->sh_entsize),
2226             &isec, &shdr, &data) == S_ERROR)
2227                 return (S_ERROR);
2228 
2229         if ((*ret_os = ld_place_section(ofl, isec, NULL,
2230             ld_targ.t_id.id_dynsym_ndx, NULL)) == (Os_desc *)S_ERROR)
2231                 return (S_ERROR);
2232 
2233         assert(*ret_os);
2234 
2235         return (1);
2236 }
2237 
2238 /*
2239  * Build a SHT_SYMTAB_SHNDX for the .dynsym, and .SUNW_ldynsym
2240  */
2241 static uintptr_t
2242 make_dynsym_shndx(Ofl_desc *ofl)
2243 {
2244         /*
2245          * If there is a .SUNW_ldynsym, generate a section for its extended
2246          * index section as well.
2247          */
2248         if (OFL_ALLOW_LDYNSYM(ofl)) {
2249                 if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_LDYNSYM_SHNDX),
2250                     ofl->ofl_osldynsym, &ofl->ofl_osldynshndx) == S_ERROR)
2251                         return (S_ERROR);
2252         }
2253 
2254         /* The Generate a section for the dynsym */
2255         if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_DYNSYM_SHNDX),
2256             ofl->ofl_osdynsym, &ofl->ofl_osdynshndx) == S_ERROR)
2257                 return (S_ERROR);
2258 
2259         return (1);
2260 }
2261 
2262 
2263 /*
2264  * Build a string table for the section headers.
2265  */
2266 static uintptr_t
2267 make_shstrtab(Ofl_desc *ofl)
2268 {
2269         Shdr            *shdr;
2270         Elf_Data        *data;
2271         Is_desc         *isec;
2272         size_t          size;
2273 
2274         if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_SHSTRTAB),
2275             0, &isec, &shdr, &data) == S_ERROR)
2276                 return (S_ERROR);
2277 
2278         /*
2279          * Place the section first, as it may effect the number of section
2280          * headers to account for.
2281          */
2282         ofl->ofl_osshstrtab =
2283             ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_note, NULL);
2284         if (ofl->ofl_osshstrtab == (Os_desc *)S_ERROR)
2285                 return (S_ERROR);
2286 
2287         size = st_getstrtab_sz(ofl->ofl_shdrsttab);
2288         assert(size > 0);
2289 
2290         data->d_size = size;
2291         shdr->sh_size = (Xword)size;
2292 
2293         return (1);
2294 }
2295 
2296 /*
2297  * Build a string section for the standard symbol table.
2298  */
2299 static uintptr_t
2300 make_strtab(Ofl_desc *ofl)
2301 {
2302         Shdr            *shdr;
2303         Elf_Data        *data;
2304         Is_desc         *isec;
2305         size_t          size;
2306 
2307         /*
2308          * This string table consists of all the global and local symbols.
2309          * Account for null bytes at end of the file name and the beginning
2310          * of section.
2311          */
2312         if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1)
2313                 return (S_ERROR);
2314 
2315         size = st_getstrtab_sz(ofl->ofl_strtab);
2316         assert(size > 0);
2317 
2318         if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_STRTAB),
2319             0, &isec, &shdr, &data) == S_ERROR)
2320                 return (S_ERROR);
2321 
2322         /* Set the size of the data area */
2323         data->d_size = size;
2324         shdr->sh_size = (Xword)size;
2325 
2326         ofl->ofl_osstrtab =
2327             ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_strtab, NULL);
2328         return ((uintptr_t)ofl->ofl_osstrtab);
2329 }
2330 
2331 /*
2332  * Build a string table for the dynamic symbol table.
2333  */
2334 static uintptr_t
2335 make_dynstr(Ofl_desc *ofl)
2336 {
2337         Shdr            *shdr;
2338         Elf_Data        *data;
2339         Is_desc         *isec;
2340         size_t          size;
2341 
2342         /*
2343          * If producing a .SUNW_ldynsym, account for the initial STT_FILE
2344          * symbol that precedes the scope reduced global symbols.
2345          */
2346         if (OFL_ALLOW_LDYNSYM(ofl)) {
2347                 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_name) == -1)
2348                         return (S_ERROR);
2349                 ofl->ofl_dynscopecnt++;
2350         }
2351 
2352         /*
2353          * Account for any local, named register symbols.  These locals are
2354          * required for reference from DT_REGISTER .dynamic entries.
2355          */
2356         if (ofl->ofl_regsyms) {
2357                 int     ndx;
2358 
2359                 for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
2360                         Sym_desc        *sdp;
2361 
2362                         if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
2363                                 continue;
2364 
2365                         if (!SYM_IS_HIDDEN(sdp) &&
2366                             (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL))
2367                                 continue;
2368 
2369                         if (sdp->sd_sym->st_name == NULL)
2370                                 continue;
2371 
2372                         if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1)
2373                                 return (S_ERROR);
2374                 }
2375         }
2376 
2377         /*
2378          * Reserve entries for any per-symbol auxiliary/filter strings.
2379          */
2380         if (ofl->ofl_dtsfltrs != NULL) {
2381                 Dfltr_desc      *dftp;
2382                 Aliste          idx;
2383 
2384                 for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, idx, dftp))
2385                         if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1)
2386                                 return (S_ERROR);
2387         }
2388 
2389         size = st_getstrtab_sz(ofl->ofl_dynstrtab);
2390         assert(size > 0);
2391 
2392         if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_DYNSTR),
2393             0, &isec, &shdr, &data) == S_ERROR)
2394                 return (S_ERROR);
2395 
2396         /* Make it allocable if necessary */
2397         if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
2398                 shdr->sh_flags |= SHF_ALLOC;
2399 
2400         /* Set the size of the data area */
2401         data->d_size = size + DYNSTR_EXTRA_PAD;
2402 
2403         shdr->sh_size = (Xword)size;
2404 
2405         ofl->ofl_osdynstr =
2406             ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynstr, NULL);
2407         return ((uintptr_t)ofl->ofl_osdynstr);
2408 }
2409 
2410 /*
2411  * Generate an output relocation section which will contain the relocation
2412  * information to be applied to the `osp' section.
2413  *
2414  * If (osp == NULL) then we are creating the coalesced relocation section
2415  * for an executable and/or a shared object.
2416  */
2417 static uintptr_t
2418 make_reloc(Ofl_desc *ofl, Os_desc *osp)
2419 {
2420         Shdr            *shdr;
2421         Elf_Data        *data;
2422         Is_desc         *isec;
2423         size_t          size;
2424         Xword           sh_flags;
2425         char            *sectname;
2426         Os_desc         *rosp;
2427         Word            relsize;
2428         const char      *rel_prefix;
2429 
2430         /* LINTED */
2431         if (ld_targ.t_m.m_rel_sht_type == SHT_REL) {
2432                 /* REL */
2433                 relsize = sizeof (Rel);
2434                 rel_prefix = MSG_ORIG(MSG_SCN_REL);
2435         } else {
2436                 /* RELA */
2437                 relsize = sizeof (Rela);
2438                 rel_prefix = MSG_ORIG(MSG_SCN_RELA);
2439         }
2440 
2441         if (osp) {
2442                 size = osp->os_szoutrels;
2443                 sh_flags = osp->os_shdr->sh_flags;
2444                 if ((sectname = libld_malloc(strlen(rel_prefix) +
2445                     strlen(osp->os_name) + 1)) == 0)
2446                         return (S_ERROR);
2447                 (void) strcpy(sectname, rel_prefix);
2448                 (void) strcat(sectname, osp->os_name);
2449         } else if (ofl->ofl_flags & FLG_OF_COMREL) {
2450                 size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize;
2451                 sh_flags = SHF_ALLOC;
2452                 sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC);
2453         } else {
2454                 size = ofl->ofl_relocrelsz;
2455                 sh_flags = SHF_ALLOC;
2456                 sectname = (char *)rel_prefix;
2457         }
2458 
2459         /*
2460          * Keep track of total size of 'output relocations' (to be stored
2461          * in .dynamic)
2462          */
2463         /* LINTED */
2464         ofl->ofl_relocsz += (Xword)size;
2465 
2466         if (new_section(ofl, ld_targ.t_m.m_rel_sht_type, sectname, 0, &isec,
2467             &shdr, &data) == S_ERROR)
2468                 return (S_ERROR);
2469 
2470         data->d_size = size;
2471 
2472         shdr->sh_size = (Xword)size;
2473         if (OFL_ALLOW_DYNSYM(ofl) && (sh_flags & SHF_ALLOC))
2474                 shdr->sh_flags = SHF_ALLOC;
2475 
2476         if (osp) {
2477                 /*
2478                  * The sh_info field of the SHT_REL* sections points to the
2479                  * section the relocations are to be applied to.
2480                  */
2481                 shdr->sh_flags |= SHF_INFO_LINK;
2482         }
2483 
2484         rosp = ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_rel, NULL);
2485         if (rosp == (Os_desc *)S_ERROR)
2486                 return (S_ERROR);
2487 
2488         /*
2489          * Associate this relocation section to the section its going to
2490          * relocate.
2491          */
2492         if (osp) {
2493                 Aliste  idx;
2494                 Is_desc *risp;
2495 
2496                 /*
2497                  * This is used primarily so that we can update
2498                  * SHT_GROUP[sect_no] entries to point to the
2499                  * created output relocation sections.
2500                  */
2501                 for (APLIST_TRAVERSE(osp->os_relisdescs, idx, risp)) {
2502                         risp->is_osdesc = rosp;
2503 
2504                         /*
2505                          * If the input relocation section had the SHF_GROUP
2506                          * flag set - propagate it to the output relocation
2507                          * section.
2508                          */
2509                         if (risp->is_shdr->sh_flags & SHF_GROUP) {
2510                                 rosp->os_shdr->sh_flags |= SHF_GROUP;
2511                                 break;
2512                         }
2513                 }
2514                 osp->os_relosdesc = rosp;
2515         } else
2516                 ofl->ofl_osrel = rosp;
2517 
2518         /*
2519          * If this is the first relocation section we've encountered save it
2520          * so that the .dynamic entry can be initialized accordingly.
2521          */
2522         if (ofl->ofl_osrelhead == (Os_desc *)0)
2523                 ofl->ofl_osrelhead = rosp;
2524 
2525         return (1);
2526 }
2527 
2528 /*
2529  * Generate version needed section.
2530  */
2531 static uintptr_t
2532 make_verneed(Ofl_desc *ofl)
2533 {
2534         Shdr            *shdr;
2535         Elf_Data        *data;
2536         Is_desc         *isec;
2537 
2538         /*
2539          * verneed sections do not have a constant element size, so the
2540          * value of ent_cnt specified here (0) is meaningless.
2541          */
2542         if (new_section(ofl, SHT_SUNW_verneed, MSG_ORIG(MSG_SCN_SUNWVERSION),
2543             0, &isec, &shdr, &data) == S_ERROR)
2544                 return (S_ERROR);
2545 
2546         /* During version processing we calculated the total size. */
2547         data->d_size = ofl->ofl_verneedsz;
2548         shdr->sh_size = (Xword)ofl->ofl_verneedsz;
2549 
2550         ofl->ofl_osverneed =
2551             ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_version, NULL);
2552         return ((uintptr_t)ofl->ofl_osverneed);
2553 }
2554 
2555 /*
2556  * Generate a version definition section.
2557  *
2558  *  o   the SHT_SUNW_verdef section defines the versions that exist within this
2559  *      image.
2560  */
2561 static uintptr_t
2562 make_verdef(Ofl_desc *ofl)
2563 {
2564         Shdr            *shdr;
2565         Elf_Data        *data;
2566         Is_desc         *isec;
2567         Ver_desc        *vdp;
2568         Str_tbl         *strtab;
2569 
2570         /*
2571          * Reserve a string table entry for the base version dependency (other
2572          * dependencies have symbol representations, which will already be
2573          * accounted for during symbol processing).
2574          */
2575         vdp = (Ver_desc *)ofl->ofl_verdesc->apl_data[0];
2576 
2577         if (OFL_IS_STATIC_OBJ(ofl))
2578                 strtab = ofl->ofl_strtab;
2579         else
2580                 strtab = ofl->ofl_dynstrtab;
2581 
2582         if (st_insert(strtab, vdp->vd_name) == -1)
2583                 return (S_ERROR);
2584 
2585         /*
2586          * verdef sections do not have a constant element size, so the
2587          * value of ent_cnt specified here (0) is meaningless.
2588          */
2589         if (new_section(ofl, SHT_SUNW_verdef, MSG_ORIG(MSG_SCN_SUNWVERSION),
2590             0, &isec, &shdr, &data) == S_ERROR)
2591                 return (S_ERROR);
2592 
2593         /* During version processing we calculated the total size. */
2594         data->d_size = ofl->ofl_verdefsz;
2595         shdr->sh_size = (Xword)ofl->ofl_verdefsz;
2596 
2597         ofl->ofl_osverdef =
2598             ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_version, NULL);
2599         return ((uintptr_t)ofl->ofl_osverdef);
2600 }
2601 
2602 /*
2603  * This routine is called when -z nopartial is in effect.
2604  */
2605 uintptr_t
2606 ld_make_parexpn_data(Ofl_desc *ofl, size_t size, Xword align)
2607 {
2608         Shdr            *shdr;
2609         Elf_Data        *data;
2610         Is_desc         *isec;
2611         Os_desc         *osp;
2612 
2613         if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
2614             &isec, &shdr, &data) == S_ERROR)
2615                 return (S_ERROR);
2616 
2617         shdr->sh_flags |= SHF_WRITE;
2618         data->d_size = size;
2619         shdr->sh_size = (Xword)size;
2620         if (align != 0) {
2621                 data->d_align = align;
2622                 shdr->sh_addralign = align;
2623         }
2624 
2625         if ((data->d_buf = libld_calloc(size, 1)) == NULL)
2626                 return (S_ERROR);
2627 
2628         /*
2629          * Retain handle to this .data input section. Variables using move
2630          * sections (partial initialization) will be redirected here when
2631          * such global references are added and '-z nopartial' is in effect.
2632          */
2633         ofl->ofl_isparexpn = isec;
2634         osp = ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_data, NULL);
2635         if (osp == (Os_desc *)S_ERROR)
2636                 return (S_ERROR);
2637 
2638         if (!(osp->os_flags & FLG_OS_OUTREL)) {
2639                 ofl->ofl_dynshdrcnt++;
2640                 osp->os_flags |= FLG_OS_OUTREL;
2641         }
2642         return (1);
2643 }
2644 
2645 /*
2646  * Make .sunwmove section
2647  */
2648 uintptr_t
2649 ld_make_sunwmove(Ofl_desc *ofl, int mv_nums)
2650 {
2651         Shdr            *shdr;
2652         Elf_Data        *data;
2653         Is_desc         *isec;
2654         Aliste          idx;
2655         Sym_desc        *sdp;
2656         int             cnt = 1;
2657 
2658 
2659         if (new_section(ofl, SHT_SUNW_move, MSG_ORIG(MSG_SCN_SUNWMOVE),
2660             mv_nums, &isec, &shdr, &data) == S_ERROR)
2661                 return (S_ERROR);
2662 
2663         if ((data->d_buf = libld_calloc(data->d_size, 1)) == NULL)
2664                 return (S_ERROR);
2665 
2666         /*
2667          * Copy move entries
2668          */
2669         for (APLIST_TRAVERSE(ofl->ofl_parsyms, idx, sdp)) {
2670                 Aliste          idx2;
2671                 Mv_desc         *mdp;
2672 
2673                 if (sdp->sd_flags & FLG_SY_PAREXPN)
2674                         continue;
2675 
2676                 for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp))
2677                         mdp->md_oidx = cnt++;
2678         }
2679 
2680         if ((ofl->ofl_osmove = ld_place_section(ofl, isec, NULL, 0, NULL)) ==
2681             (Os_desc *)S_ERROR)
2682                 return (S_ERROR);
2683 
2684         return (1);
2685 }
2686 
2687 /*
2688  * Given a relocation descriptor that references a string table
2689  * input section, locate the string referenced and return a pointer
2690  * to it.
2691  */
2692 static const char *
2693 strmerge_get_reloc_str(Ofl_desc *ofl, Rel_desc *rsp)
2694 {
2695         Sym_desc *sdp = rsp->rel_sym;
2696         Xword    str_off;
2697 
2698         /*
2699          * In the case of an STT_SECTION symbol, the addend of the
2700          * relocation gives the offset into the string section. For
2701          * other symbol types, the symbol value is the offset.
2702          */
2703 
2704         if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) {
2705                 str_off = sdp->sd_sym->st_value;
2706         } else if ((rsp->rel_flags & FLG_REL_RELA) == FLG_REL_RELA) {
2707                 /*
2708                  * For SHT_RELA, the addend value is found in the
2709                  * rel_raddend field of the relocation.
2710                  */
2711                 str_off = rsp->rel_raddend;
2712         } else {        /* REL and STT_SECTION */
2713                 /*
2714                  * For SHT_REL, the "addend" is not part of the relocation
2715                  * record. Instead, it is found at the relocation target
2716                  * address.
2717                  */
2718                 uchar_t *addr = (uchar_t *)((uintptr_t)rsp->rel_roffset +
2719                     (uintptr_t)rsp->rel_isdesc->is_indata->d_buf);
2720 
2721                 if (ld_reloc_targval_get(ofl, rsp, addr, &str_off) == 0)
2722                         return (0);
2723         }
2724 
2725         return (str_off + (char *)sdp->sd_isc->is_indata->d_buf);
2726 }
2727 
2728 /*
2729  * First pass over the relocation records for string table merging.
2730  * Build lists of relocations and symbols that will need modification,
2731  * and insert the strings they reference into the mstrtab string table.
2732  *
2733  * entry:
2734  *      ofl, osp - As passed to ld_make_strmerge().
2735  *      mstrtab - String table to receive input strings. This table
2736  *              must be in its first (initialization) pass and not
2737  *              yet cooked (st_getstrtab_sz() not yet called).
2738  *      rel_alpp - APlist to receive pointer to any relocation
2739  *              descriptors with STT_SECTION symbols that reference
2740  *              one of the input sections being merged.
2741  *      sym_alpp - APlist to receive pointer to any symbols that reference
2742  *              one of the input sections being merged.
2743  *      rcp - Pointer to cache of relocation descriptors to examine.
2744  *              Either &ofl->ofl_actrels (active relocations)
2745  *              or &ofl->ofl_outrels (output relocations).
2746  *
2747  * exit:
2748  *      On success, rel_alpp and sym_alpp are updated, and
2749  *      any strings in the mergeable input sections referenced by
2750  *      a relocation has been entered into mstrtab. True (1) is returned.
2751  *
2752  *      On failure, False (0) is returned.
2753  */
2754 static int
2755 strmerge_pass1(Ofl_desc *ofl, Os_desc *osp, Str_tbl *mstrtab,
2756     APlist **rel_alpp, APlist **sym_alpp, Rel_cache *rcp)
2757 {
2758         Aliste          idx;
2759         Rel_cachebuf    *rcbp;
2760         Sym_desc        *sdp;
2761         Sym_desc        *last_sdp = NULL;
2762         Rel_desc        *rsp;
2763         const char      *name;
2764 
2765         REL_CACHE_TRAVERSE(rcp, idx, rcbp, rsp) {
2766                 sdp = rsp->rel_sym;
2767                 if ((sdp->sd_isc == NULL) || ((sdp->sd_isc->is_flags &
2768                     (FLG_IS_DISCARD | FLG_IS_INSTRMRG)) != FLG_IS_INSTRMRG) ||
2769                     (sdp->sd_isc->is_osdesc != osp))
2770                         continue;
2771 
2772                 /*
2773                  * Remember symbol for use in the third pass. There is no
2774                  * reason to save a given symbol more than once, so we take
2775                  * advantage of the fact that relocations to a given symbol
2776                  * tend to cluster in the list. If this is the same symbol
2777                  * we saved last time, don't bother.
2778                  */
2779                 if (last_sdp != sdp) {
2780                         if (aplist_append(sym_alpp, sdp, AL_CNT_STRMRGSYM) ==
2781                             NULL)
2782                                 return (0);
2783                         last_sdp = sdp;
2784                 }
2785 
2786                 /* Enter the string into our new string table */
2787                 name = strmerge_get_reloc_str(ofl, rsp);
2788                 if (st_insert(mstrtab, name) == -1)
2789                         return (0);
2790 
2791                 /*
2792                  * If this is an STT_SECTION symbol, then the second pass
2793                  * will need to modify this relocation, so hang on to it.
2794                  */
2795                 if ((ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) &&
2796                     (aplist_append(rel_alpp, rsp, AL_CNT_STRMRGREL) == NULL))
2797                         return (0);
2798         }
2799 
2800         return (1);
2801 }
2802 
2803 /*
2804  * If the output section has any SHF_MERGE|SHF_STRINGS input sections,
2805  * replace them with a single merged/compressed input section.
2806  *
2807  * entry:
2808  *      ofl - Output file descriptor
2809  *      osp - Output section descriptor
2810  *      rel_alpp, sym_alpp, - Address of 2 APlists, to be used
2811  *              for internal processing. On the initial call to
2812  *              ld_make_strmerge, these list pointers must be NULL.
2813  *              The caller is encouraged to pass the same lists back for
2814  *              successive calls to this function without freeing
2815  *              them in between calls. This causes a single pair of
2816  *              memory allocations to be reused multiple times.
2817  *
2818  * exit:
2819  *      If section merging is possible, it is done. If no errors are
2820  *      encountered, True (1) is returned. On error, S_ERROR.
2821  *
2822  *      The contents of rel_alpp and sym_alpp on exit are
2823  *      undefined. The caller can free them, or pass them back to a subsequent
2824  *      call to this routine, but should not examine their contents.
2825  */
2826 static uintptr_t
2827 ld_make_strmerge(Ofl_desc *ofl, Os_desc *osp, APlist **rel_alpp,
2828     APlist **sym_alpp)
2829 {
2830         Str_tbl         *mstrtab;       /* string table for string merge secs */
2831         Is_desc         *mstrsec;       /* Generated string merge section */
2832         Is_desc         *isp;
2833         Shdr            *mstr_shdr;
2834         Elf_Data        *mstr_data;
2835         Sym_desc        *sdp;
2836         Rel_desc        *rsp;
2837         Aliste          idx;
2838         size_t          data_size;
2839         int             st_setstring_status;
2840         size_t          stoff;
2841 
2842         /* If string table compression is disabled, there's nothing to do */
2843         if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) != 0)
2844                 return (1);
2845 
2846         /*
2847          * Pass over the mergeable input sections, and if they haven't
2848          * all been discarded, create a string table.
2849          */
2850         mstrtab = NULL;
2851         for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) {
2852                 if (isp->is_flags & FLG_IS_DISCARD)
2853                         continue;
2854 
2855                 /*
2856                  * We have at least one non-discarded section.
2857                  * Create a string table descriptor.
2858                  */
2859                 if ((mstrtab = st_new(FLG_STNEW_COMPRESS)) == NULL)
2860                         return (S_ERROR);
2861                 break;
2862         }
2863 
2864         /* If no string table was created, we have no mergeable sections */
2865         if (mstrtab == NULL)
2866                 return (1);
2867 
2868         /*
2869          * This routine has to make 3 passes:
2870          *
2871          *      1) Examine all relocations, insert strings from relocations
2872          *              to the mergeable input sections into the string table.
2873          *      2) Modify the relocation values to be correct for the
2874          *              new merged section.
2875          *      3) Modify the symbols used by the relocations to reference
2876          *              the new section.
2877          *
2878          * These passes cannot be combined:
2879          *      - The string table code works in two passes, and all
2880          *              strings have to be loaded in pass one before the
2881          *              offset of any strings can be determined.
2882          *      - Multiple relocations reference a single symbol, so the
2883          *              symbol cannot be modified until all relocations are
2884          *              fixed.
2885          *
2886          * The number of relocations related to section merging is usually
2887          * a mere fraction of the overall active and output relocation lists,
2888          * and the number of symbols is usually a fraction of the number
2889          * of related relocations. We therefore build APlists for the
2890          * relocations and symbols in the first pass, and then use those
2891          * lists to accelerate the operation of pass 2 and 3.
2892          *
2893          * Reinitialize the lists to a completely empty state.
2894          */
2895         aplist_reset(*rel_alpp);
2896         aplist_reset(*sym_alpp);
2897 
2898         /*
2899          * Pass 1:
2900          *
2901          * Every relocation related to this output section (and the input
2902          * sections that make it up) is found in either the active, or the
2903          * output relocation list, depending on whether the relocation is to
2904          * be processed by this invocation of the linker, or inserted into the
2905          * output object.
2906          *
2907          * Build lists of relocations and symbols that will need modification,
2908          * and insert the strings they reference into the mstrtab string table.
2909          */
2910         if (strmerge_pass1(ofl, osp, mstrtab, rel_alpp, sym_alpp,
2911             &ofl->ofl_actrels) == 0)
2912                 goto return_s_error;
2913         if (strmerge_pass1(ofl, osp, mstrtab, rel_alpp, sym_alpp,
2914             &ofl->ofl_outrels) == 0)
2915                 goto return_s_error;
2916 
2917         /*
2918          * Get the size of the new input section. Requesting the
2919          * string table size "cooks" the table, and finalizes its contents.
2920          */
2921         data_size = st_getstrtab_sz(mstrtab);
2922 
2923         /* Create a new input section to hold the merged strings */
2924         if (new_section_from_template(ofl, isp, data_size,
2925             &mstrsec, &mstr_shdr, &mstr_data) == S_ERROR)
2926                 goto return_s_error;
2927         mstrsec->is_flags |= FLG_IS_GNSTRMRG;
2928 
2929         /*
2930          * Allocate a data buffer for the new input section.
2931          * Then, associate the buffer with the string table descriptor.
2932          */
2933         if ((mstr_data->d_buf = libld_malloc(data_size)) == NULL)
2934                 goto return_s_error;
2935         if (st_setstrbuf(mstrtab, mstr_data->d_buf, data_size) == -1)
2936                 goto return_s_error;
2937 
2938         /* Add the new section to the output image */
2939         if (ld_place_section(ofl, mstrsec, NULL, osp->os_identndx, NULL) ==
2940             (Os_desc *)S_ERROR)
2941                 goto return_s_error;
2942 
2943         /*
2944          * Pass 2:
2945          *
2946          * Revisit the relocation descriptors with STT_SECTION symbols
2947          * that were saved by the first pass. Update each relocation
2948          * record so that the offset it contains is for the new section
2949          * instead of the original.
2950          */
2951         for (APLIST_TRAVERSE(*rel_alpp, idx, rsp)) {
2952                 const char      *name;
2953 
2954                 /* Put the string into the merged string table */
2955                 name = strmerge_get_reloc_str(ofl, rsp);
2956                 st_setstring_status = st_setstring(mstrtab, name, &stoff);
2957                 if (st_setstring_status == -1) {
2958                         /*
2959                          * A failure to insert at this point means that
2960                          * something is corrupt. This isn't a resource issue.
2961                          */
2962                         assert(st_setstring_status != -1);
2963                         goto return_s_error;
2964                 }
2965 
2966                 /*
2967                  * Alter the relocation to access the string at the
2968                  * new offset in our new string table.
2969                  *
2970                  * For SHT_RELA platforms, it suffices to simply
2971                  * update the rel_raddend field of the relocation.
2972                  *
2973                  * For SHT_REL platforms, the new "addend" value
2974                  * needs to be written at the address being relocated.
2975                  * However, we can't alter the input sections which
2976                  * are mapped readonly, and the output image has not
2977                  * been created yet. So, we defer this operation,
2978                  * using the rel_raddend field of the relocation
2979                  * which is normally 0 on a REL platform, to pass the
2980                  * new "addend" value to ld_perform_outreloc() or
2981                  * ld_do_activerelocs(). The FLG_REL_NADDEND flag
2982                  * tells them that this is the case.
2983                  */
2984                 if ((rsp->rel_flags & FLG_REL_RELA) == 0)   /* REL */
2985                         rsp->rel_flags |= FLG_REL_NADDEND;
2986                 rsp->rel_raddend = (Sxword)stoff;
2987 
2988                 /*
2989                  * Generate a symbol name string for STT_SECTION symbols
2990                  * that might reference our merged section. This shows up
2991                  * in debug output and helps show how the relocation has
2992                  * changed from its original input section to our merged one.
2993                  */
2994                 if (ld_stt_section_sym_name(mstrsec) == NULL)
2995                         goto return_s_error;
2996         }
2997 
2998         /*
2999          * Pass 3:
3000          *
3001          * Modify the symbols referenced by the relocation descriptors
3002          * so that they reference the new input section containing the
3003          * merged strings instead of the original input sections.
3004          */
3005         for (APLIST_TRAVERSE(*sym_alpp, idx, sdp)) {
3006                 /*
3007                  * If we've already processed this symbol, don't do it
3008                  * twice. strmerge_pass1() uses a heuristic (relocations to
3009                  * the same symbol clump together) to avoid inserting a
3010                  * given symbol more than once, but repeat symbols in
3011                  * the list can occur.
3012                  */
3013                 if ((sdp->sd_isc->is_flags & FLG_IS_INSTRMRG) == 0)
3014                         continue;
3015 
3016                 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) {
3017                         /*
3018                          * This is not an STT_SECTION symbol, so its
3019                          * value is the offset of the string within the
3020                          * input section. Update the address to reflect
3021                          * the address in our new merged section.
3022                          */
3023                         const char *name = sdp->sd_sym->st_value +
3024                             (char *)sdp->sd_isc->is_indata->d_buf;
3025 
3026                         st_setstring_status =
3027                             st_setstring(mstrtab, name, &stoff);
3028                         if (st_setstring_status == -1) {
3029                                 /*
3030                                  * A failure to insert at this point means
3031                                  * something is corrupt. This isn't a
3032                                  * resource issue.
3033                                  */
3034                                 assert(st_setstring_status != -1);
3035                                 goto return_s_error;
3036                         }
3037 
3038                         if (ld_sym_copy(sdp) == S_ERROR)
3039                                 goto return_s_error;
3040                         sdp->sd_sym->st_value = (Word)stoff;
3041                 }
3042 
3043                 /* Redirect the symbol to our new merged section */
3044                 sdp->sd_isc = mstrsec;
3045         }
3046 
3047         /*
3048          * There are no references left to the original input string sections.
3049          * Mark them as discarded so they don't go into the output image.
3050          * At the same time, add up the sizes of the replaced sections.
3051          */
3052         data_size = 0;
3053         for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) {
3054                 if (isp->is_flags & (FLG_IS_DISCARD | FLG_IS_GNSTRMRG))
3055                         continue;
3056 
3057                 data_size += isp->is_indata->d_size;
3058 
3059                 isp->is_flags |= FLG_IS_DISCARD;
3060                 DBG_CALL(Dbg_sec_discarded(ofl->ofl_lml, isp, mstrsec));
3061         }
3062 
3063         /* Report how much space we saved in the output section */
3064         DBG_CALL(Dbg_sec_genstr_compress(ofl->ofl_lml, osp->os_name, data_size,
3065             mstr_data->d_size));
3066 
3067         st_destroy(mstrtab);
3068         return (1);
3069 
3070 return_s_error:
3071         st_destroy(mstrtab);
3072         return (S_ERROR);
3073 }
3074 
3075 /*
3076  * Update a data buffers size.  A number of sections have to be created, and
3077  * the sections header contributes to the size of the eventual section.  Thus,
3078  * a section may be created, and once all associated sections have been created,
3079  * we return to establish the required section size.
3080  */
3081 inline static void
3082 update_data_size(Os_desc *osp, ulong_t cnt)
3083 {
3084         Is_desc         *isec = ld_os_first_isdesc(osp);
3085         Elf_Data        *data = isec->is_indata;
3086         Shdr            *shdr = osp->os_shdr;
3087         size_t          size = cnt * shdr->sh_entsize;
3088 
3089         shdr->sh_size = (Xword)size;
3090         data->d_size = size;
3091 }
3092 
3093 /*
3094  * The following sections are built after all input file processing and symbol
3095  * validation has been carried out.  The order is important (because the
3096  * addition of a section adds a new symbol there is a chicken and egg problem
3097  * of maintaining the appropriate counts).  By maintaining a known order the
3098  * individual routines can compensate for later, known, additions.
3099  */
3100 uintptr_t
3101 ld_make_sections(Ofl_desc *ofl)
3102 {
3103         ofl_flag_t      flags = ofl->ofl_flags;
3104         Sg_desc         *sgp;
3105 
3106         /*
3107          * Generate any special sections.
3108          */
3109         if (flags & FLG_OF_ADDVERS)
3110                 if (make_comment(ofl) == S_ERROR)
3111                         return (S_ERROR);
3112 
3113         if (make_interp(ofl) == S_ERROR)
3114                 return (S_ERROR);
3115 
3116         /*
3117          * Create a capabilities section if required.
3118          */
3119         if (make_cap(ofl, SHT_SUNW_cap, MSG_ORIG(MSG_SCN_SUNWCAP),
3120             ld_targ.t_id.id_cap) == S_ERROR)
3121                 return (S_ERROR);
3122 
3123         /*
3124          * Create any init/fini array sections.
3125          */
3126         if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY),
3127             ofl->ofl_initarray) == S_ERROR)
3128                 return (S_ERROR);
3129 
3130         if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY),
3131             ofl->ofl_finiarray) == S_ERROR)
3132                 return (S_ERROR);
3133 
3134         if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY),
3135             ofl->ofl_preiarray) == S_ERROR)
3136                 return (S_ERROR);
3137 
3138         /*
3139          * Make the .plt section.  This occurs after any other relocation
3140          * sections are generated (see reloc_init()) to ensure that the
3141          * associated relocation section is after all the other relocation
3142          * sections.
3143          */
3144         if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad))
3145                 if (make_plt(ofl) == S_ERROR)
3146                         return (S_ERROR);
3147 
3148         /*
3149          * Determine whether any sections or files are not referenced.  Under
3150          * -Dunused a diagnostic for any unused components is generated, under
3151          * -zignore the component is removed from the final output.
3152          */
3153         if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) {
3154                 if (ignore_section_processing(ofl) == S_ERROR)
3155                         return (S_ERROR);
3156         }
3157 
3158         /*
3159          * If we have detected a situation in which previously placed
3160          * output sections may have been discarded, perform the necessary
3161          * readjustment.
3162          */
3163         if (ofl->ofl_flags & FLG_OF_ADJOSCNT)
3164                 adjust_os_count(ofl);
3165 
3166         /*
3167          * Do any of the output sections contain input sections that
3168          * are candidates for string table merging? For each such case,
3169          * we create a replacement section, insert it, and discard the
3170          * originals.
3171          *
3172          * rel_alpp and sym_alpp are used by ld_make_strmerge()
3173          * for its internal processing. We are responsible for the
3174          * initialization and cleanup, and ld_make_strmerge() handles the rest.
3175          * This allows us to reuse a single pair of memory buffers, allocated
3176          * for this processing, for all the output sections.
3177          */
3178         if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) == 0) {
3179                 int     error_seen = 0;
3180                 APlist  *rel_alpp = NULL;
3181                 APlist  *sym_alpp = NULL;
3182                 Aliste  idx1;
3183 
3184                 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
3185                         Os_desc *osp;
3186                         Aliste  idx2;
3187 
3188                         for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp))
3189                                 if ((osp->os_mstrisdescs != NULL) &&
3190                                     (ld_make_strmerge(ofl, osp,
3191                                     &rel_alpp, &sym_alpp) ==
3192                                     S_ERROR)) {
3193                                         error_seen = 1;
3194                                         break;
3195                                 }
3196                 }
3197                 if (rel_alpp != NULL)
3198                         libld_free(rel_alpp);
3199                 if (sym_alpp != NULL)
3200                         libld_free(sym_alpp);
3201                 if (error_seen != 0)
3202                         return (S_ERROR);
3203         }
3204 
3205         /*
3206          * Add any necessary versioning information.
3207          */
3208         if (!(flags & FLG_OF_NOVERSEC)) {
3209                 if ((flags & FLG_OF_VERNEED) &&
3210                     (make_verneed(ofl) == S_ERROR))
3211                         return (S_ERROR);
3212                 if ((flags & FLG_OF_VERDEF) &&
3213                     (make_verdef(ofl) == S_ERROR))
3214                         return (S_ERROR);
3215                 if ((flags & (FLG_OF_VERNEED | FLG_OF_VERDEF)) &&
3216                     ((ofl->ofl_osversym = make_sym_sec(ofl,
3217                     MSG_ORIG(MSG_SCN_SUNWVERSYM), SHT_SUNW_versym,
3218                     ld_targ.t_id.id_version)) == (Os_desc*)S_ERROR))
3219                         return (S_ERROR);
3220         }
3221 
3222         /*
3223          * Create a syminfo section if necessary.
3224          */
3225         if (flags & FLG_OF_SYMINFO) {
3226                 if ((ofl->ofl_ossyminfo = make_sym_sec(ofl,
3227                     MSG_ORIG(MSG_SCN_SUNWSYMINFO), SHT_SUNW_syminfo,
3228                     ld_targ.t_id.id_syminfo)) == (Os_desc *)S_ERROR)
3229                         return (S_ERROR);
3230         }
3231 
3232         if (flags & FLG_OF_COMREL) {
3233                 /*
3234                  * If -zcombreloc is enabled then all relocations (except for
3235                  * the PLT's) are coalesced into a single relocation section.
3236                  */
3237                 if (ofl->ofl_reloccnt) {
3238                         if (make_reloc(ofl, NULL) == S_ERROR)
3239                                 return (S_ERROR);
3240                 }
3241         } else {
3242                 Aliste  idx1;
3243 
3244                 /*
3245                  * Create the required output relocation sections.  Note, new
3246                  * sections may be added to the section list that is being
3247                  * traversed.  These insertions can move the elements of the
3248                  * Alist such that a section descriptor is re-read.  Recursion
3249                  * is prevented by maintaining a previous section pointer and
3250                  * insuring that this pointer isn't re-examined.
3251                  */
3252                 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
3253                         Os_desc *osp, *posp = 0;
3254                         Aliste  idx2;
3255 
3256                         for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
3257                                 if ((osp != posp) && osp->os_szoutrels &&
3258                                     (osp != ofl->ofl_osplt)) {
3259                                         if (make_reloc(ofl, osp) == S_ERROR)
3260                                                 return (S_ERROR);
3261                                 }
3262                                 posp = osp;
3263                         }
3264                 }
3265 
3266                 /*
3267                  * If we're not building a combined relocation section, then
3268                  * build a .rel[a] section as required.
3269                  */
3270                 if (ofl->ofl_relocrelsz) {
3271                         if (make_reloc(ofl, NULL) == S_ERROR)
3272                                 return (S_ERROR);
3273                 }
3274         }
3275 
3276         /*
3277          * The PLT relocations are always in their own section, and we try to
3278          * keep them at the end of the PLT table.  We do this to keep the hot
3279          * "data" PLT's at the head of the table nearer the .dynsym & .hash.
3280          */
3281         if (ofl->ofl_osplt && ofl->ofl_relocpltsz) {
3282                 if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR)
3283                         return (S_ERROR);
3284         }
3285 
3286         /*
3287          * Finally build the symbol and section header sections.
3288          */
3289         if (flags & FLG_OF_DYNAMIC) {
3290                 if (make_dynamic(ofl) == S_ERROR)
3291                         return (S_ERROR);
3292 
3293                 /*
3294                  * A number of sections aren't necessary within a relocatable
3295                  * object, even if -dy has been used.
3296                  */
3297                 if (!(flags & FLG_OF_RELOBJ)) {
3298                         if (make_hash(ofl) == S_ERROR)
3299                                 return (S_ERROR);
3300                         if (make_dynstr(ofl) == S_ERROR)
3301                                 return (S_ERROR);
3302                         if (make_dynsym(ofl) == S_ERROR)
3303                                 return (S_ERROR);
3304                         if (ld_unwind_make_hdr(ofl) == S_ERROR)
3305                                 return (S_ERROR);
3306                         if (make_dynsort(ofl) == S_ERROR)
3307                                 return (S_ERROR);
3308                 }
3309         }
3310 
3311         if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
3312             ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
3313                 /*
3314                  * Do we need to make a SHT_SYMTAB_SHNDX section
3315                  * for the dynsym.  If so - do it now.
3316                  */
3317                 if (ofl->ofl_osdynsym &&
3318                     ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) {
3319                         if (make_dynsym_shndx(ofl) == S_ERROR)
3320                                 return (S_ERROR);
3321                 }
3322 
3323                 if (make_strtab(ofl) == S_ERROR)
3324                         return (S_ERROR);
3325                 if (make_symtab(ofl) == S_ERROR)
3326                         return (S_ERROR);
3327         } else {
3328                 /*
3329                  * Do we need to make a SHT_SYMTAB_SHNDX section
3330                  * for the dynsym.  If so - do it now.
3331                  */
3332                 if (ofl->ofl_osdynsym &&
3333                     ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) {
3334                         if (make_dynsym_shndx(ofl) == S_ERROR)
3335                                 return (S_ERROR);
3336                 }
3337         }
3338 
3339         if (make_shstrtab(ofl) == S_ERROR)
3340                 return (S_ERROR);
3341 
3342         /*
3343          * Now that we've created all output sections, adjust the size of the
3344          * SHT_SUNW_versym and SHT_SUNW_syminfo section, which are dependent on
3345          * the associated symbol table sizes.
3346          */
3347         if (ofl->ofl_osversym || ofl->ofl_ossyminfo) {
3348                 ulong_t         cnt;
3349                 Is_desc         *isp;
3350                 Os_desc         *osp;
3351 
3352                 if (OFL_IS_STATIC_OBJ(ofl))
3353                         osp = ofl->ofl_ossymtab;
3354                 else
3355                         osp = ofl->ofl_osdynsym;
3356 
3357                 isp = ld_os_first_isdesc(osp);
3358                 cnt = (isp->is_shdr->sh_size / isp->is_shdr->sh_entsize);
3359 
3360                 if (ofl->ofl_osversym)
3361                         update_data_size(ofl->ofl_osversym, cnt);
3362 
3363                 if (ofl->ofl_ossyminfo)
3364                         update_data_size(ofl->ofl_ossyminfo, cnt);
3365         }
3366 
3367         /*
3368          * Now that we've created all output sections, adjust the size of the
3369          * SHT_SUNW_capinfo, which is dependent on the associated symbol table
3370          * size.
3371          */
3372         if (ofl->ofl_oscapinfo) {
3373                 ulong_t cnt;
3374 
3375                 /*
3376                  * Symbol capabilities symbols are placed directly after the
3377                  * STT_FILE symbol, section symbols, and any register symbols.
3378                  * Effectively these are the first of any series of demoted
3379                  * (scoped) symbols.
3380                  */
3381                 if (OFL_IS_STATIC_OBJ(ofl))
3382                         cnt = SYMTAB_ALL_CNT(ofl);
3383                 else
3384                         cnt = DYNSYM_ALL_CNT(ofl);
3385 
3386                 update_data_size(ofl->ofl_oscapinfo, cnt);
3387         }
3388         return (1);
3389 }
3390 
3391 /*
3392  * Build an additional data section - used to back OBJT symbol definitions
3393  * added with a mapfile.
3394  */
3395 Is_desc *
3396 ld_make_data(Ofl_desc *ofl, size_t size)
3397 {
3398         Shdr            *shdr;
3399         Elf_Data        *data;
3400         Is_desc         *isec;
3401 
3402         if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
3403             &isec, &shdr, &data) == S_ERROR)
3404                 return ((Is_desc *)S_ERROR);
3405 
3406         data->d_size = size;
3407         shdr->sh_size = (Xword)size;
3408         shdr->sh_flags |= SHF_WRITE;
3409 
3410         if (aplist_append(&ofl->ofl_mapdata, isec, AL_CNT_OFL_MAPSECS) == NULL)
3411                 return ((Is_desc *)S_ERROR);
3412 
3413         return (isec);
3414 }
3415 
3416 /*
3417  * Build an additional text section - used to back FUNC symbol definitions
3418  * added with a mapfile.
3419  */
3420 Is_desc *
3421 ld_make_text(Ofl_desc *ofl, size_t size)
3422 {
3423         Shdr            *shdr;
3424         Elf_Data        *data;
3425         Is_desc         *isec;
3426 
3427         /*
3428          * Insure the size is sufficient to contain the minimum return
3429          * instruction.
3430          */
3431         if (size < ld_targ.t_nf.nf_size)
3432                 size = ld_targ.t_nf.nf_size;
3433 
3434         if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_TEXT), 0,
3435             &isec, &shdr, &data) == S_ERROR)
3436                 return ((Is_desc *)S_ERROR);
3437 
3438         data->d_size = size;
3439         shdr->sh_size = (Xword)size;
3440         shdr->sh_flags |= SHF_EXECINSTR;
3441 
3442         /*
3443          * Fill the buffer with the appropriate return instruction.
3444          * Note that there is no need to swap bytes on a non-native,
3445          * link, as the data being copied is given in bytes.
3446          */
3447         if ((data->d_buf = libld_calloc(size, 1)) == NULL)
3448                 return ((Is_desc *)S_ERROR);
3449         (void) memcpy(data->d_buf, ld_targ.t_nf.nf_template,
3450             ld_targ.t_nf.nf_size);
3451 
3452         /*
3453          * If size was larger than required, and the target supplies
3454          * a fill function, use it to fill the balance. If there is no
3455          * fill function, we accept the 0-fill supplied by libld_calloc().
3456          */
3457         if ((ld_targ.t_ff.ff_execfill != NULL) && (size > ld_targ.t_nf.nf_size))
3458                 ld_targ.t_ff.ff_execfill(data->d_buf, ld_targ.t_nf.nf_size,
3459                     size - ld_targ.t_nf.nf_size);
3460 
3461         if (aplist_append(&ofl->ofl_maptext, isec, AL_CNT_OFL_MAPSECS) == NULL)
3462                 return ((Is_desc *)S_ERROR);
3463 
3464         return (isec);
3465 }
3466 
3467 void
3468 ld_comdat_validate(Ofl_desc *ofl, Ifl_desc *ifl)
3469 {
3470         int i;
3471 
3472         for (i = 0; i < ifl->ifl_shnum; i++) {
3473                 Is_desc *isp = ifl->ifl_isdesc[i];
3474                 int types = 0;
3475                 char buf[1024] = "";
3476                 Group_desc *gr = NULL;
3477 
3478                 if ((isp == NULL) || (isp->is_flags & FLG_IS_COMDAT) == 0)
3479                         continue;
3480 
3481                 if (isp->is_shdr->sh_type == SHT_SUNW_COMDAT) {
3482                         types++;
3483                         (void) strlcpy(buf, MSG_ORIG(MSG_STR_SUNW_COMDAT),
3484                             sizeof (buf));
3485                 }
3486 
3487                 if (strncmp(MSG_ORIG(MSG_SCN_GNU_LINKONCE), isp->is_name,
3488                     MSG_SCN_GNU_LINKONCE_SIZE) == 0) {
3489                         types++;
3490                         if (types > 1)
3491                                 (void) strlcat(buf, ", ", sizeof (buf));
3492                         (void) strlcat(buf, MSG_ORIG(MSG_SCN_GNU_LINKONCE),
3493                             sizeof (buf));
3494                 }
3495 
3496                 if ((isp->is_shdr->sh_flags & SHF_GROUP) &&
3497                     ((gr = ld_get_group(ofl, isp)) != NULL) &&
3498                     (gr->gd_data[0] & GRP_COMDAT)) {
3499                         types++;
3500                         if (types > 1)
3501                                 (void) strlcat(buf, ", ", sizeof (buf));
3502                         (void) strlcat(buf, MSG_ORIG(MSG_STR_GROUP),
3503                             sizeof (buf));
3504                 }
3505 
3506                 if (types > 1)
3507                         ld_eprintf(ofl, ERR_FATAL,
3508                             MSG_INTL(MSG_SCN_MULTICOMDAT), ifl->ifl_name,
3509                             EC_WORD(isp->is_scnndx), isp->is_name, buf);
3510         }
3511 }