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