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  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
  28  * Copyright 2016 RackTop Systems.
  29  */
  30 
  31 /*
  32  * Processing of relocatable objects and shared objects.
  33  */
  34 
  35 #define ELF_TARGET_AMD64
  36 #define ELF_TARGET_SPARC
  37 
  38 #include        <stdio.h>
  39 #include        <string.h>
  40 #include        <fcntl.h>
  41 #include        <unistd.h>
  42 #include        <link.h>
  43 #include        <limits.h>
  44 #include        <sys/stat.h>
  45 #include        <sys/systeminfo.h>
  46 #include        <debug.h>
  47 #include        <msg.h>
  48 #include        <_libld.h>
  49 
  50 /*
  51  * Decide if we can link against this input file.
  52  */
  53 static int
  54 ifl_verify(Ehdr *ehdr, Ofl_desc *ofl, Rej_desc *rej)
  55 {
  56         /*
  57          * Check the validity of the elf header information for compatibility
  58          * with this machine and our own internal elf library.
  59          */
  60         if ((ehdr->e_machine != ld_targ.t_m.m_mach) &&
  61             ((ehdr->e_machine != ld_targ.t_m.m_machplus) &&
  62             ((ehdr->e_flags & ld_targ.t_m.m_flagsplus) == 0))) {
  63                 rej->rej_type = SGS_REJ_MACH;
  64                 rej->rej_info = (uint_t)ehdr->e_machine;
  65                 return (0);
  66         }
  67         if (ehdr->e_ident[EI_DATA] != ld_targ.t_m.m_data) {
  68                 rej->rej_type = SGS_REJ_DATA;
  69                 rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA];
  70                 return (0);
  71         }
  72         if (ehdr->e_version > ofl->ofl_dehdr->e_version) {
  73                 rej->rej_type = SGS_REJ_VERSION;
  74                 rej->rej_info = (uint_t)ehdr->e_version;
  75                 return (0);
  76         }
  77         return (1);
  78 }
  79 
  80 /*
  81  * Check sanity of file header and allocate an infile descriptor
  82  * for the file being processed.
  83  */
  84 static Ifl_desc *
  85 ifl_setup(const char *name, Ehdr *ehdr, Elf *elf, Word flags, Ofl_desc *ofl,
  86     Rej_desc *rej)
  87 {
  88         Ifl_desc        *ifl;
  89         Rej_desc        _rej = { 0 };
  90 
  91         if (ifl_verify(ehdr, ofl, &_rej) == 0) {
  92                 _rej.rej_name = name;
  93                 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
  94                     ld_targ.t_m.m_mach));
  95                 if (rej->rej_type == 0) {
  96                         *rej = _rej;
  97                         rej->rej_name = strdup(_rej.rej_name);
  98                 }
  99                 return (0);
 100         }
 101 
 102         if ((ifl = libld_calloc(1, sizeof (Ifl_desc))) == NULL)
 103                 return ((Ifl_desc *)S_ERROR);
 104         ifl->ifl_name = name;
 105         ifl->ifl_ehdr = ehdr;
 106         ifl->ifl_elf = elf;
 107         ifl->ifl_flags = flags;
 108 
 109         /*
 110          * Is this file using 'extended Section Indexes'.  If so, use the
 111          * e_shnum & e_shstrndx which can be found at:
 112          *
 113          *      e_shnum == Shdr[0].sh_size
 114          *      e_shstrndx == Shdr[0].sh_link
 115          */
 116         if ((ehdr->e_shnum == 0) && (ehdr->e_shoff != 0)) {
 117                 Elf_Scn *scn;
 118                 Shdr    *shdr0;
 119 
 120                 if ((scn = elf_getscn(elf, 0)) == NULL) {
 121                         ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
 122                             name);
 123                         return ((Ifl_desc *)S_ERROR);
 124                 }
 125                 if ((shdr0 = elf_getshdr(scn)) == NULL) {
 126                         ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
 127                             name);
 128                         return ((Ifl_desc *)S_ERROR);
 129                 }
 130                 ifl->ifl_shnum = (Word)shdr0->sh_size;
 131                 if (ehdr->e_shstrndx == SHN_XINDEX)
 132                         ifl->ifl_shstrndx = shdr0->sh_link;
 133                 else
 134                         ifl->ifl_shstrndx = ehdr->e_shstrndx;
 135         } else {
 136                 ifl->ifl_shnum = ehdr->e_shnum;
 137                 ifl->ifl_shstrndx = ehdr->e_shstrndx;
 138         }
 139 
 140         if ((ifl->ifl_isdesc = libld_calloc(ifl->ifl_shnum,
 141             sizeof (Is_desc *))) == NULL)
 142                 return ((Ifl_desc *)S_ERROR);
 143 
 144         /*
 145          * Record this new input file on the shared object or relocatable
 146          * object input file list.
 147          */
 148         if (ifl->ifl_ehdr->e_type == ET_DYN) {
 149                 if (aplist_append(&ofl->ofl_sos, ifl, AL_CNT_OFL_LIBS) == NULL)
 150                         return ((Ifl_desc *)S_ERROR);
 151         } else {
 152                 if (aplist_append(&ofl->ofl_objs, ifl, AL_CNT_OFL_OBJS) == NULL)
 153                         return ((Ifl_desc *)S_ERROR);
 154         }
 155 
 156         return (ifl);
 157 }
 158 
 159 /*
 160  * Process a generic section.  The appropriate section information is added
 161  * to the files input descriptor list.
 162  */
 163 static uintptr_t
 164 process_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
 165     Word ndx, int ident, Ofl_desc *ofl)
 166 {
 167         Is_desc *isp;
 168 
 169         /*
 170          * Create a new input section descriptor.  If this is a NOBITS
 171          * section elf_getdata() will still create a data buffer (the buffer
 172          * will be null and the size will reflect the actual memory size).
 173          */
 174         if ((isp = libld_calloc(sizeof (Is_desc), 1)) == NULL)
 175                 return (S_ERROR);
 176         isp->is_shdr = shdr;
 177         isp->is_file = ifl;
 178         isp->is_name = name;
 179         isp->is_scnndx = ndx;
 180         isp->is_flags = FLG_IS_EXTERNAL;
 181         isp->is_keyident = ident;
 182 
 183         if ((isp->is_indata = elf_getdata(scn, NULL)) == NULL) {
 184                 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETDATA),
 185                     ifl->ifl_name);
 186                 return (0);
 187         }
 188 
 189         if ((shdr->sh_flags & SHF_EXCLUDE) &&
 190             ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
 191                 isp->is_flags |= FLG_IS_DISCARD;
 192         }
 193 
 194         /*
 195          * Add the new input section to the files input section list and
 196          * flag whether the section needs placing in an output section.  This
 197          * placement is deferred until all input section processing has been
 198          * completed, as SHT_GROUP sections can provide information that will
 199          * affect how other sections within the file should be placed.
 200          */
 201         ifl->ifl_isdesc[ndx] = isp;
 202 
 203         if (ident) {
 204                 if (shdr->sh_flags & ALL_SHF_ORDER) {
 205                         isp->is_flags |= FLG_IS_ORDERED;
 206                         ifl->ifl_flags |= FLG_IF_ORDERED;
 207                 }
 208                 isp->is_flags |= FLG_IS_PLACE;
 209         }
 210         return (1);
 211 }
 212 
 213 /*
 214  * Determine the software capabilities of the object being built from the
 215  * capabilities of the input relocatable objects.   One software capability
 216  * is presently recognized, and represented with the following (sys/elf.h):
 217  *
 218  *   SF1_SUNW_FPKNWN    use/non-use of frame pointer is known, and
 219  *   SF1_SUNW_FPUSED    the frame pointer is in use.
 220  *
 221  * The resolution of the present fame pointer state, and the capabilities
 222  * provided by a new input relocatable object are:
 223  *
 224  *                              new input relocatable object
 225  *
 226  *      present      |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |    <unknown>
 227  *       state       |  SF1_SUNW_FPUSED  |                   |
 228  *  ---------------------------------------------------------------------------
 229  *  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN
 230  *  SF1_SUNW_FPUSED  |  SF1_SUNW_FPUSED  |                   |  SF1_SUNW_FPUSED
 231  *  ---------------------------------------------------------------------------
 232  *  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN
 233  *                   |                   |                   |
 234  *  ---------------------------------------------------------------------------
 235  *     <unknown>     |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |    <unknown>
 236  *                   |  SF1_SUNW_FPUSED  |                   |
 237  */
 238 static void
 239 sf1_cap(Ofl_desc *ofl, Xword val, Ifl_desc *ifl, Is_desc *cisp)
 240 {
 241 #define FP_FLAGS        (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)
 242 
 243         Xword   badval;
 244 
 245         /*
 246          * If a mapfile has established definitions to override any object
 247          * capabilities, ignore any new object capabilities.
 248          */
 249         if (ofl->ofl_flags1 & FLG_OF1_OVSFCAP1) {
 250                 DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
 251                     CA_SUNW_SF_1, val, ld_targ.t_m.m_mach));
 252                 return;
 253         }
 254 
 255 #if     !defined(_ELF64)
 256         if (ifl && (ifl->ifl_ehdr->e_type == ET_REL)) {
 257                 /*
 258                  * The SF1_SUNW_ADDR32 is only meaningful when building a 64-bit
 259                  * object.  Warn the user, and remove the setting, if we're
 260                  * building a 32-bit object.
 261                  */
 262                 if (val & SF1_SUNW_ADDR32) {
 263                         ld_eprintf(ofl, ERR_WARNING,
 264                             MSG_INTL(MSG_FIL_INADDR32SF1), ifl->ifl_name,
 265                             EC_WORD(cisp->is_scnndx), cisp->is_name);
 266                         val &= ~SF1_SUNW_ADDR32;
 267                 }
 268         }
 269 #endif
 270         /*
 271          * If this object doesn't specify any capabilities, ignore it, and
 272          * leave the state as is.
 273          */
 274         if (val == 0)
 275                 return;
 276 
 277         /*
 278          * Make sure we only accept known software capabilities.  Note, that
 279          * an F1_SUNW_FPUSED by itself is viewed as bad practice.
 280          */
 281         if ((badval = (val & ~SF1_SUNW_MASK)) != 0) {
 282                 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
 283                     ifl->ifl_name, EC_WORD(cisp->is_scnndx), cisp->is_name,
 284                     EC_XWORD(badval));
 285                 val &= SF1_SUNW_MASK;
 286         }
 287         if ((val & FP_FLAGS) == SF1_SUNW_FPUSED) {
 288                 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
 289                     ifl->ifl_name, EC_WORD(cisp->is_scnndx), cisp->is_name,
 290                     EC_XWORD(val));
 291                 return;
 292         }
 293 
 294         /*
 295          * If the input file is not a relocatable object, then we're only here
 296          * to warn the user of any questionable capabilities.
 297          */
 298         if (ifl->ifl_ehdr->e_type != ET_REL) {
 299 #if     defined(_ELF64)
 300                 /*
 301                  * If we're building a 64-bit executable, and we come across a
 302                  * dependency that requires a restricted address space, then
 303                  * that dependencies requirement can only be satisfied if the
 304                  * executable triggers the restricted address space.  This is a
 305                  * warning rather than a fatal error, as the possibility exists
 306                  * that an appropriate dependency will be provided at runtime.
 307                  * The runtime linker will refuse to use this dependency.
 308                  */
 309                 if ((val & SF1_SUNW_ADDR32) && (ofl->ofl_flags & FLG_OF_EXEC) &&
 310                     ((ofl->ofl_ocapset.oc_sf_1.cm_val &
 311                     SF1_SUNW_ADDR32) == 0)) {
 312                         ld_eprintf(ofl, ERR_WARNING,
 313                             MSG_INTL(MSG_FIL_EXADDR32SF1), ifl->ifl_name,
 314                             EC_WORD(cisp->is_scnndx), cisp->is_name);
 315                 }
 316 #endif
 317                 return;
 318         }
 319 
 320         if (DBG_ENABLED) {
 321                 Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_CURRENT, CA_SUNW_SF_1,
 322                     ofl->ofl_ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach);
 323                 Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_NEW, CA_SUNW_SF_1,
 324                     val, ld_targ.t_m.m_mach);
 325         }
 326 
 327         /*
 328          * Determine the resolution of the present frame pointer and the
 329          * new input relocatable objects frame pointer.
 330          */
 331         if ((ofl->ofl_ocapset.oc_sf_1.cm_val & FP_FLAGS) == FP_FLAGS) {
 332                 /*
 333                  * If the new relocatable object isn't using a frame pointer,
 334                  * reduce the present state to unused.
 335                  */
 336                 if ((val & FP_FLAGS) != FP_FLAGS)
 337                         ofl->ofl_ocapset.oc_sf_1.cm_val &= ~SF1_SUNW_FPUSED;
 338 
 339                 /*
 340                  * Having processed the frame pointer bits, remove them from
 341                  * the value so they don't get OR'd in below.
 342                  */
 343                 val &= ~FP_FLAGS;
 344 
 345         } else if ((ofl->ofl_ocapset.oc_sf_1.cm_val & SF1_SUNW_FPKNWN) == 0) {
 346                 /*
 347                  * If the present frame pointer state is unknown, mask it out
 348                  * and allow the values from the new relocatable object
 349                  * to overwrite them.
 350                  */
 351                 ofl->ofl_ocapset.oc_sf_1.cm_val &= ~FP_FLAGS;
 352         } else {
 353                 /* Do not take the frame pointer flags from the object */
 354                 val &= ~FP_FLAGS;
 355         }
 356 
 357         ofl->ofl_ocapset.oc_sf_1.cm_val |= val;
 358 
 359         DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_RESOLVED,
 360             CA_SUNW_SF_1, ofl->ofl_ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach));
 361 
 362 #undef FP_FLAGS
 363 }
 364 
 365 /*
 366  * Determine the hardware capabilities of the object being built from the
 367  * capabilities of the input relocatable objects.  There's really little to
 368  * do here, other than to offer diagnostics, hardware capabilities are simply
 369  * additive.
 370  */
 371 static void
 372 hw_cap(Ofl_desc *ofl, Xword tag, Xword val)
 373 {
 374         elfcap_mask_t   *hwcap;
 375         ofl_flag_t      flags1;
 376 
 377         if (tag == CA_SUNW_HW_1) {
 378                 hwcap = &ofl->ofl_ocapset.oc_hw_1.cm_val;
 379                 flags1 = FLG_OF1_OVHWCAP1;
 380         } else {
 381                 hwcap = &ofl->ofl_ocapset.oc_hw_2.cm_val;
 382                 flags1 = FLG_OF1_OVHWCAP2;
 383         }
 384 
 385         /*
 386          * If a mapfile has established definitions to override any object
 387          * capabilities, ignore any new object capabilities.
 388          */
 389         if (ofl->ofl_flags1 & flags1) {
 390                 DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
 391                     tag, val, ld_targ.t_m.m_mach));
 392                 return;
 393         }
 394 
 395         /*
 396          * If this object doesn't specify any capabilities, ignore it, and
 397          * leave the state as is.
 398          */
 399         if (val == 0)
 400                 return;
 401 
 402         if (DBG_ENABLED) {
 403                 Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_CURRENT, CA_SUNW_HW_1,
 404                     ofl->ofl_ocapset.oc_hw_1.cm_val, ld_targ.t_m.m_mach);
 405                 Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_NEW, CA_SUNW_HW_1,
 406                     val, ld_targ.t_m.m_mach);
 407         }
 408 
 409         *hwcap |= val;
 410 
 411         DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_RESOLVED, tag,
 412             *hwcap, ld_targ.t_m.m_mach));
 413 }
 414 
 415 /*
 416  * Promote a machine capability or platform capability to the output file.
 417  * Multiple instances of these names can be defined.
 418  */
 419 static void
 420 str_cap(Ofl_desc *ofl, char *pstr, ofl_flag_t flags, Xword tag, Caplist *list)
 421 {
 422         Capstr          *capstr;
 423         Aliste          idx;
 424         Boolean         found = FALSE;
 425 
 426         /*
 427          * If a mapfile has established definitions to override this capability,
 428          * ignore any new capability.
 429          */
 430         if (ofl->ofl_flags1 & flags) {
 431                 DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
 432                     tag, pstr));
 433                 return;
 434         }
 435 
 436         for (ALIST_TRAVERSE(list->cl_val, idx, capstr)) {
 437                 DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
 438                     DBG_STATE_CURRENT, tag, capstr->cs_str));
 439                 if (strcmp(capstr->cs_str, pstr) == 0)
 440                         found = TRUE;
 441         }
 442 
 443         DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_NEW, tag, pstr));
 444 
 445         if (found == FALSE) {
 446                 if ((capstr = alist_append(&list->cl_val, NULL,
 447                     sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL) {
 448                         ofl->ofl_flags |= FLG_OF_FATAL;
 449                         return;
 450                 }
 451                 capstr->cs_str = pstr;
 452         }
 453 
 454         if (DBG_ENABLED) {
 455                 for (ALIST_TRAVERSE(list->cl_val, idx, capstr)) {
 456                         DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
 457                             DBG_STATE_RESOLVED, tag, capstr->cs_str));
 458                 }
 459         }
 460 }
 461 
 462 /*
 463  * Promote a capability identifier to the output file.  A capability group can
 464  * only have one identifier, and thus only the first identifier seen from any
 465  * input relocatable objects is retained.  An explicit user defined identifier,
 466  * rather than an an identifier fabricated by ld(1) with -z symbcap processing,
 467  * takes precedence.  Note, a user may have defined an identifier via a mapfile,
 468  * in which case the mapfile identifier is retained.
 469  */
 470 static void
 471 id_cap(Ofl_desc *ofl, char *pstr, oc_flag_t flags)
 472 {
 473         Objcapset       *ocapset = &ofl->ofl_ocapset;
 474 
 475         if (ocapset->oc_id.cs_str) {
 476                 DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_CURRENT,
 477                     CA_SUNW_ID, ocapset->oc_id.cs_str));
 478 
 479                 if ((ocapset->oc_flags & FLG_OCS_USRDEFID) ||
 480                     ((flags & FLG_OCS_USRDEFID) == 0)) {
 481                         DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
 482                             DBG_STATE_IGNORED, CA_SUNW_ID, pstr));
 483                         return;
 484                 }
 485         }
 486 
 487         DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_NEW,
 488             CA_SUNW_ID, pstr));
 489 
 490         ocapset->oc_id.cs_str = pstr;
 491         ocapset->oc_flags |= flags;
 492 
 493         DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_RESOLVED,
 494             CA_SUNW_ID, pstr));
 495 }
 496 
 497 /*
 498  * Promote a capabilities group to the object capabilities.  This catches a
 499  * corner case.  An object capabilities file can be converted to symbol
 500  * capabilities with -z symbolcap.  However, if the user has indicated that all
 501  * the symbols should be demoted, we'd be left with a symbol capabilities file,
 502  * with no associated symbols.  Catch this case by promoting the symbol
 503  * capabilities back to object capabilities.
 504  */
 505 void
 506 ld_cap_move_symtoobj(Ofl_desc *ofl)
 507 {
 508         Cap_group       *cgp;
 509         Aliste          idx1;
 510 
 511         for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
 512                 Objcapset       *scapset = &cgp->cg_set;
 513                 Capstr          *capstr;
 514                 Aliste          idx2;
 515 
 516                 if (scapset->oc_id.cs_str) {
 517                         if (scapset->oc_flags & FLG_OCS_USRDEFID)
 518                                 id_cap(ofl, scapset->oc_id.cs_str,
 519                                     scapset->oc_flags);
 520                 }
 521                 if (scapset->oc_plat.cl_val) {
 522                         for (ALIST_TRAVERSE(scapset->oc_plat.cl_val, idx2,
 523                             capstr)) {
 524                                 str_cap(ofl, capstr->cs_str, FLG_OF1_OVPLATCAP,
 525                                     CA_SUNW_PLAT, &ofl->ofl_ocapset.oc_plat);
 526                         }
 527                 }
 528                 if (scapset->oc_mach.cl_val) {
 529                         for (ALIST_TRAVERSE(scapset->oc_mach.cl_val, idx2,
 530                             capstr)) {
 531                                 str_cap(ofl, capstr->cs_str, FLG_OF1_OVMACHCAP,
 532                                     CA_SUNW_MACH, &ofl->ofl_ocapset.oc_mach);
 533                         }
 534                 }
 535                 if (scapset->oc_hw_2.cm_val)
 536                         hw_cap(ofl, CA_SUNW_HW_2, scapset->oc_hw_2.cm_val);
 537 
 538                 if (scapset->oc_hw_1.cm_val)
 539                         hw_cap(ofl, CA_SUNW_HW_1, scapset->oc_hw_1.cm_val);
 540 
 541                 if (scapset->oc_sf_1.cm_val)
 542                         sf1_cap(ofl, scapset->oc_sf_1.cm_val, NULL, NULL);
 543         }
 544 }
 545 
 546 /*
 547  * Determine whether a capabilities group already exists that describes this
 548  * new capabilities group.
 549  *
 550  * Note, a capability group identifier, CA_SUNW_ID, isn't used as part of the
 551  * comparison.  This attribute simply assigns a diagnostic name to the group,
 552  * and in the case of multiple identifiers, the first will be taken.
 553  */
 554 static Cap_group *
 555 get_cap_group(Objcapset *ocapset, Word cnum, Ofl_desc *ofl, Is_desc *isp)
 556 {
 557         Aliste          idx;
 558         Cap_group       *cgp;
 559         Word            ccnum = cnum;
 560 
 561         /*
 562          * If the new capabilities contains a CA_SUNW_ID, drop the count of the
 563          * number of comparable items.
 564          */
 565         if (ocapset->oc_id.cs_str)
 566                 ccnum--;
 567 
 568         /*
 569          * Traverse the existing symbols capabilities groups.
 570          */
 571         for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx, cgp)) {
 572                 Word    onum = cgp->cg_num;
 573                 Alist   *calp, *oalp;
 574 
 575                 if (cgp->cg_set.oc_id.cs_str)
 576                         onum--;
 577 
 578                 if (onum != ccnum)
 579                         continue;
 580 
 581                 if (cgp->cg_set.oc_hw_1.cm_val != ocapset->oc_hw_1.cm_val)
 582                         continue;
 583                 if (cgp->cg_set.oc_sf_1.cm_val != ocapset->oc_sf_1.cm_val)
 584                         continue;
 585                 if (cgp->cg_set.oc_hw_2.cm_val != ocapset->oc_hw_2.cm_val)
 586                         continue;
 587 
 588                 calp = cgp->cg_set.oc_plat.cl_val;
 589                 oalp = ocapset->oc_plat.cl_val;
 590                 if ((calp == NULL) && oalp)
 591                         continue;
 592                 if (calp && ((oalp == NULL) || cap_names_match(calp, oalp)))
 593                         continue;
 594 
 595                 calp = cgp->cg_set.oc_mach.cl_val;
 596                 oalp = ocapset->oc_mach.cl_val;
 597                 if ((calp == NULL) && oalp)
 598                         continue;
 599                 if (calp && ((oalp == NULL) || cap_names_match(calp, oalp)))
 600                         continue;
 601 
 602                 /*
 603                  * If a matching group is found, then this new group has
 604                  * already been supplied by a previous file, and hence the
 605                  * existing group can be used.  Record this new input section,
 606                  * from which we can also derive the input file name, on the
 607                  * existing groups input sections.
 608                  */
 609                 if (aplist_append(&(cgp->cg_secs), isp,
 610                     AL_CNT_CAP_SECS) == NULL)
 611                         return (NULL);
 612                 return (cgp);
 613         }
 614 
 615         /*
 616          * If a capabilities group is not found, create a new one.
 617          */
 618         if (((cgp = libld_calloc(sizeof (Cap_group), 1)) == NULL) ||
 619             (aplist_append(&(ofl->ofl_capgroups), cgp,
 620             AL_CNT_CAP_DESCS) == NULL))
 621                 return (NULL);
 622 
 623         /*
 624          * If we're converting object capabilities to symbol capabilities and
 625          * no CA_SUNW_ID is defined, fabricate one.  This identifier is appended
 626          * to all symbol names that are converted into capabilities symbols,
 627          * see ld_sym_process().
 628          */
 629         if ((isp->is_file->ifl_flags & FLG_IF_OTOSCAP) &&
 630             (ocapset->oc_id.cs_str == NULL)) {
 631                 size_t  len;
 632 
 633                 /*
 634                  * Create an identifier using the group number together with a
 635                  * default template.  We allocate a buffer large enough for any
 636                  * possible number of items (way more than we need).
 637                  */
 638                 len = MSG_STR_CAPGROUPID_SIZE + CONV_INV_BUFSIZE;
 639                 if ((ocapset->oc_id.cs_str = libld_malloc(len)) == NULL)
 640                         return (NULL);
 641 
 642                 (void) snprintf(ocapset->oc_id.cs_str, len,
 643                     MSG_ORIG(MSG_STR_CAPGROUPID),
 644                     aplist_nitems(ofl->ofl_capgroups));
 645                 cnum++;
 646         }
 647 
 648         cgp->cg_set = *ocapset;
 649         cgp->cg_num = cnum;
 650 
 651         /*
 652          * Null the callers alist's as they've effectively been transferred
 653          * to this new Cap_group.
 654          */
 655         ocapset->oc_plat.cl_val = ocapset->oc_mach.cl_val = NULL;
 656 
 657         /*
 658          * Keep track of which input section, and hence input file, established
 659          * this group.
 660          */
 661         if (aplist_append(&(cgp->cg_secs), isp, AL_CNT_CAP_SECS) == NULL)
 662                 return (NULL);
 663 
 664         /*
 665          * Keep track of the number of symbol capabilities entries that will be
 666          * required in the output file.  Each group requires a terminating
 667          * CA_SUNW_NULL.
 668          */
 669         ofl->ofl_capsymcnt += (cnum + 1);
 670         return (cgp);
 671 }
 672 
 673 /*
 674  * Capture symbol capability family information.  This data structure is focal
 675  * in maintaining all symbol capability relationships, and provides for the
 676  * eventual creation of a capabilities information section, and possibly a
 677  * capabilities chain section.
 678  *
 679  * Capabilities families are lead by a CAPINFO_SUNW_GLOB symbol.  This symbol
 680  * provides the visible global symbol that is referenced by all external
 681  * callers.  This symbol may have aliases.  For example, a weak/global symbol
 682  * pair, such as memcpy()/_memcpy() may lead the same capabilities family.
 683  * Each family contains one or more local symbol members.  These members provide
 684  * the capabilities specific functions, and are associated to a capabilities
 685  * group.  For example, the capability members memcpy%sun4u and memcpy%sun4v
 686  * might be associated with the memcpy() capability family.
 687  *
 688  * This routine is called when a relocatable object that provides object
 689  * capabilities is transformed into a symbol capabilities object, using the
 690  * -z symbolcap option.
 691  *
 692  * This routine is also called to collect the SUNW_capinfo section information
 693  * of a relocatable object that contains symbol capability definitions.
 694  */
 695 uintptr_t
 696 ld_cap_add_family(Ofl_desc *ofl, Sym_desc *lsdp, Sym_desc *csdp, Cap_group *cgp,
 697     APlist **csyms)
 698 {
 699         Cap_avlnode     qcav, *cav;
 700         avl_tree_t      *avlt;
 701         avl_index_t     where = 0;
 702         Cap_sym         *mcsp;
 703         Aliste          idx;
 704 
 705         /*
 706          * Make sure the capability families have an initialized AVL tree.
 707          */
 708         if ((avlt = ofl->ofl_capfamilies) == NULL) {
 709                 if ((avlt = libld_calloc(sizeof (avl_tree_t), 1)) == NULL)
 710                         return (S_ERROR);
 711                 avl_create(avlt, &ld_sym_avl_comp, sizeof (Cap_avlnode),
 712                     SGSOFFSETOF(Cap_avlnode, cn_symavlnode.sav_node));
 713                 ofl->ofl_capfamilies = avlt;
 714 
 715                 /*
 716                  * When creating a dynamic object, capability family members
 717                  * are maintained in a .SUNW_capchain, the first entry of
 718                  * which is the version number of the chain.
 719                  */
 720                 ofl->ofl_capchaincnt = 1;
 721         }
 722 
 723         /*
 724          * Determine whether a family already exists, and if not, create one
 725          * using the lead family symbol.
 726          */
 727         qcav.cn_symavlnode.sav_hash = (Word)elf_hash(lsdp->sd_name);
 728         qcav.cn_symavlnode.sav_name = lsdp->sd_name;
 729 
 730         if ((cav = avl_find(avlt, &qcav, &where)) == NULL) {
 731                 if ((cav = libld_calloc(sizeof (Cap_avlnode), 1)) == NULL)
 732                         return (S_ERROR);
 733                 cav->cn_symavlnode.sav_hash = qcav.cn_symavlnode.sav_hash;
 734                 cav->cn_symavlnode.sav_name = qcav.cn_symavlnode.sav_name;
 735                 cav->cn_symavlnode.sav_sdp = lsdp;
 736 
 737                 avl_insert(avlt, cav, where);
 738 
 739                 /*
 740                  * When creating a dynamic object, capability family members
 741                  * are maintained in a .SUNW_capchain, each family starts with
 742                  * this lead symbol, and is terminated with a 0 element.
 743                  */
 744                 ofl->ofl_capchaincnt += 2;
 745         }
 746 
 747         /*
 748          * If no group information is provided then this request is to add a
 749          * lead capability symbol, or lead symbol alias.  If this is the lead
 750          * symbol there's nothing more to do.  Otherwise save the alias.
 751          */
 752         if (cgp == NULL) {
 753                 if ((lsdp != csdp) && (aplist_append(&cav->cn_aliases, csdp,
 754                     AL_CNT_CAP_ALIASES) == NULL))
 755                         return (S_ERROR);
 756 
 757                 return (0);
 758         }
 759 
 760         /*
 761          * Determine whether a member of the same group as this new member is
 762          * already defined within this family.  If so, we have a multiply
 763          * defined symbol.
 764          */
 765         for (APLIST_TRAVERSE(cav->cn_members, idx, mcsp)) {
 766                 Sym_desc        *msdp;
 767 
 768                 if (cgp != mcsp->cs_group)
 769                         continue;
 770 
 771                 /*
 772                  * Diagnose that a multiple symbol definition exists.
 773                  */
 774                 msdp = mcsp->cs_sdp;
 775 
 776                 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_CAP_MULDEF),
 777                     demangle(lsdp->sd_name));
 778                 ld_eprintf(ofl, ERR_NONE, MSG_INTL(MSG_CAP_MULDEFSYMS),
 779                     msdp->sd_file->ifl_name, msdp->sd_name,
 780                     csdp->sd_file->ifl_name, csdp->sd_name);
 781         }
 782 
 783         /*
 784          * Add this capabilities symbol member to the family.
 785          */
 786         if (((mcsp = libld_malloc(sizeof (Cap_sym))) == NULL) ||
 787             (aplist_append(&cav->cn_members, mcsp, AL_CNT_CAP_MEMS) == NULL))
 788                 return (S_ERROR);
 789 
 790         mcsp->cs_sdp = csdp;
 791         mcsp->cs_group = cgp;
 792 
 793         /*
 794          * When creating a dynamic object, capability family members are
 795          * maintained in a .SUNW_capchain.  Account for this family member.
 796          */
 797         ofl->ofl_capchaincnt++;
 798 
 799         /*
 800          * If this input file is undergoing object capabilities to symbol
 801          * capabilities conversion, then this member is a new local symbol
 802          * that has been generated from an original global symbol.  Keep track
 803          * of this symbol so that the output file symbol table can be populated
 804          * with these new symbol entries.
 805          */
 806         if (csyms && (aplist_append(csyms, mcsp, AL_CNT_CAP_SYMS) == NULL))
 807                 return (S_ERROR);
 808 
 809         return (0);
 810 }
 811 
 812 /*
 813  * Process a SHT_SUNW_cap capabilities section.
 814  */
 815 static uintptr_t
 816 process_cap(Ofl_desc *ofl, Ifl_desc *ifl, Is_desc *cisp)
 817 {
 818         Objcapset       ocapset = { 0 };
 819         Cap_desc        *cdp;
 820         Cap             *data, *cdata;
 821         char            *strs;
 822         Word            ndx, cnum;
 823         int             objcapndx, descapndx, symcapndx;
 824         int             nulls, capstrs = 0;
 825 
 826         /*
 827          * Determine the capabilities data and size.
 828          */
 829         cdata = (Cap *)cisp->is_indata->d_buf;
 830         cnum = (Word)(cisp->is_shdr->sh_size / cisp->is_shdr->sh_entsize);
 831 
 832         if ((cdata == NULL) || (cnum == 0))
 833                 return (0);
 834 
 835         DBG_CALL(Dbg_cap_sec_title(ofl->ofl_lml, ifl->ifl_name));
 836 
 837         /*
 838          * Traverse the section to determine what capabilities groups are
 839          * available.
 840          *
 841          * A capabilities section can contain one or more, CA_SUNW_NULL
 842          * terminated groups.
 843          *
 844          *  -   The first group defines the object capabilities.
 845          *  -   Additional groups define symbol capabilities.
 846          *  -   Since the initial group is always reserved for object
 847          *      capabilities, any object with symbol capabilities must also
 848          *      have an object capabilities group.  If the object has no object
 849          *      capabilities, an empty object group is defined, consisting of a
 850          *      CA_SUNW_NULL element in index [0].
 851          *  -   If any capabilities require references to a named string, then
 852          *      the section header sh_info points to the associated string
 853          *      table.
 854          *  -   If an object contains symbol capability groups, then the
 855          *      section header sh_link points to the associated capinfo table.
 856          */
 857         objcapndx = 0;
 858         descapndx = symcapndx = -1;
 859         nulls = 0;
 860 
 861         for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
 862                 switch (data->c_tag) {
 863                 case CA_SUNW_NULL:
 864                         /*
 865                          * If this is the first CA_SUNW_NULL entry, and no
 866                          * capabilities group has been found, then this object
 867                          * does not define any object capabilities.
 868                          */
 869                         if (nulls++ == 0) {
 870                                 if (ndx == 0)
 871                                         objcapndx = -1;
 872                         } else if ((symcapndx == -1) && (descapndx != -1))
 873                                 symcapndx = descapndx;
 874 
 875                         break;
 876 
 877                 case CA_SUNW_PLAT:
 878                 case CA_SUNW_MACH:
 879                 case CA_SUNW_ID:
 880                         capstrs++;
 881                         /* FALLTHROUGH */
 882 
 883                 case CA_SUNW_HW_1:
 884                 case CA_SUNW_SF_1:
 885                 case CA_SUNW_HW_2:
 886                         /*
 887                          * If this is the start of a new group, save it.
 888                          */
 889                         if (descapndx == -1)
 890                                 descapndx = ndx;
 891                         break;
 892 
 893                 default:
 894                         ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_UNKCAP),
 895                             ifl->ifl_name, EC_WORD(cisp->is_scnndx),
 896                             cisp->is_name, data->c_tag);
 897                 }
 898         }
 899 
 900         /*
 901          * If a string capabilities entry has been found, the capabilities
 902          * section must reference the associated string table.
 903          */
 904         if (capstrs) {
 905                 Word    info = cisp->is_shdr->sh_info;
 906 
 907                 if ((info == 0) || (info > ifl->ifl_shnum)) {
 908                         ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
 909                             ifl->ifl_name, EC_WORD(cisp->is_scnndx),
 910                             cisp->is_name, EC_XWORD(info));
 911                         return (S_ERROR);
 912                 }
 913                 strs = (char *)ifl->ifl_isdesc[info]->is_indata->d_buf;
 914         }
 915 
 916         /*
 917          * The processing of capabilities groups is as follows:
 918          *
 919          *  -   if a relocatable object provides only object capabilities, and
 920          *      the -z symbolcap option is in effect, then the object
 921          *      capabilities are transformed into symbol capabilities and the
 922          *      symbol capabilities are carried over to the output file.
 923          *  -   in all other cases, any capabilities present in an input
 924          *      relocatable object are carried from the input object to the
 925          *      output without any transformation or conversion.
 926          *
 927          * Capture any object capabilities that are to be carried over to the
 928          * output file.
 929          */
 930         if ((objcapndx == 0) &&
 931             ((symcapndx != -1) || ((ofl->ofl_flags & FLG_OF_OTOSCAP) == 0))) {
 932                 for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
 933                         /*
 934                          * Object capabilities end at the first null.
 935                          */
 936                         if (data->c_tag == CA_SUNW_NULL)
 937                                 break;
 938 
 939                         /*
 940                          * Only the object software capabilities that are
 941                          * defined in a relocatable object become part of the
 942                          * object software capabilities in the output file.
 943                          * However, check the validity of any object software
 944                          * capabilities of any dependencies.
 945                          */
 946                         if (data->c_tag == CA_SUNW_SF_1) {
 947                                 sf1_cap(ofl, data->c_un.c_val, ifl, cisp);
 948                                 continue;
 949                         }
 950 
 951                         /*
 952                          * The remaining capability types must come from a
 953                          * relocatable object in order to contribute to the
 954                          * output.
 955                          */
 956                         if (ifl->ifl_ehdr->e_type != ET_REL)
 957                                 continue;
 958 
 959                         switch (data->c_tag) {
 960                         case CA_SUNW_HW_1:
 961                         case CA_SUNW_HW_2:
 962                                 hw_cap(ofl, data->c_tag, data->c_un.c_val);
 963                                 break;
 964 
 965                         case CA_SUNW_PLAT:
 966                                 str_cap(ofl, strs + data->c_un.c_ptr,
 967                                     FLG_OF1_OVPLATCAP, CA_SUNW_PLAT,
 968                                     &ofl->ofl_ocapset.oc_plat);
 969                                 break;
 970 
 971                         case CA_SUNW_MACH:
 972                                 str_cap(ofl, strs + data->c_un.c_ptr,
 973                                     FLG_OF1_OVMACHCAP, CA_SUNW_MACH,
 974                                     &ofl->ofl_ocapset.oc_mach);
 975                                 break;
 976 
 977                         case CA_SUNW_ID:
 978                                 id_cap(ofl, strs + data->c_un.c_ptr,
 979                                     FLG_OCS_USRDEFID);
 980                                 break;
 981 
 982                         default:
 983                                 assert(0);      /* Unknown capability type */
 984                         }
 985                 }
 986 
 987                 /*
 988                  * If there are no symbol capabilities, or this objects
 989                  * capabilities aren't being transformed into a symbol
 990                  * capabilities, then we're done.
 991                  */
 992                 if ((symcapndx == -1) &&
 993                     ((ofl->ofl_flags & FLG_OF_OTOSCAP) == 0))
 994                         return (1);
 995         }
 996 
 997         /*
 998          * If these capabilities don't originate from a relocatable object
 999          * there's no further processing required.
1000          */
1001         if (ifl->ifl_ehdr->e_type != ET_REL)
1002                 return (1);
1003 
1004         /*
1005          * If this object only defines an object capabilities group, and the
1006          * -z symbolcap option is in effect, then all global function symbols
1007          * and initialized global data symbols are renamed and assigned to the
1008          * transformed symbol capabilities group.
1009          */
1010         if ((objcapndx == 0) &&
1011             (symcapndx == -1) && (ofl->ofl_flags & FLG_OF_OTOSCAP))
1012                 ifl->ifl_flags |= FLG_IF_OTOSCAP;
1013 
1014         /*
1015          * Allocate a capabilities descriptor to collect the capabilities data
1016          * for this input file.  Allocate a mirror of the raw capabilities data
1017          * that points to the individual symbol capabilities groups.  An APlist
1018          * is used, although it will be sparsely populated, as the list provides
1019          * a convenient mechanism for traversal later.
1020          */
1021         if (((cdp = libld_calloc(sizeof (Cap_desc), 1)) == NULL) ||
1022             (aplist_append(&(cdp->ca_groups), NULL, cnum) == NULL))
1023                 return (S_ERROR);
1024 
1025         /*
1026          * Clear the allocated APlist data array, and assign the number of
1027          * items as the total number of array items.
1028          */
1029         (void) memset(&cdp->ca_groups->apl_data[0], 0,
1030             (cnum * sizeof (void *)));
1031         cdp->ca_groups->apl_nitems = cnum;
1032 
1033         ifl->ifl_caps = cdp;
1034 
1035         /*
1036          * Traverse the capabilities data, unpacking the data into a
1037          * capabilities set.  Process each capabilities set as a unique group.
1038          */
1039         descapndx = -1;
1040         nulls = 0;
1041 
1042         for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
1043                 Capstr  *capstr;
1044 
1045                 switch (data->c_tag) {
1046                 case CA_SUNW_NULL:
1047                         nulls++;
1048 
1049                         /*
1050                          * Process the capabilities group that this null entry
1051                          * terminates.  The capabilities group that is returned
1052                          * will either point to this file's data, or to a
1053                          * matching capabilities group that has already been
1054                          * processed.
1055                          *
1056                          * Note, if this object defines object capabilities,
1057                          * the first group descriptor points to these object
1058                          * capabilities.  It is only necessary to save this
1059                          * descriptor when object capabilities are being
1060                          * transformed into symbol capabilities (-z symbolcap).
1061                          */
1062                         if (descapndx != -1) {
1063                                 if ((nulls > 1) ||
1064                                     (ifl->ifl_flags & FLG_IF_OTOSCAP)) {
1065                                         APlist  *alp = cdp->ca_groups;
1066 
1067                                         if ((alp->apl_data[descapndx] =
1068                                             get_cap_group(&ocapset,
1069                                             (ndx - descapndx), ofl,
1070                                             cisp)) == NULL)
1071                                                 return (S_ERROR);
1072                                 }
1073 
1074                                 /*
1075                                  * Clean up the capabilities data in preparation
1076                                  * for processing additional groups.  If the
1077                                  * collected capabilities strings were used to
1078                                  * establish a new output group, they will have
1079                                  * been saved in get_cap_group().  If these
1080                                  * descriptors still exist, then an existing
1081                                  * descriptor has been used to associate with
1082                                  * this file, and these string descriptors can
1083                                  * be freed.
1084                                  */
1085                                 ocapset.oc_hw_1.cm_val =
1086                                     ocapset.oc_sf_1.cm_val =
1087                                     ocapset.oc_hw_2.cm_val = 0;
1088                                 if (ocapset.oc_plat.cl_val) {
1089                                         free((void *)ocapset.oc_plat.cl_val);
1090                                         ocapset.oc_plat.cl_val = NULL;
1091                                 }
1092                                 if (ocapset.oc_mach.cl_val) {
1093                                         free((void *)ocapset.oc_mach.cl_val);
1094                                         ocapset.oc_mach.cl_val = NULL;
1095                                 }
1096                                 descapndx = -1;
1097                         }
1098                         continue;
1099 
1100                 case CA_SUNW_HW_1:
1101                         ocapset.oc_hw_1.cm_val = data->c_un.c_val;
1102                         DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
1103                             DBG_STATE_ORIGINAL, CA_SUNW_HW_1,
1104                             ocapset.oc_hw_1.cm_val, ld_targ.t_m.m_mach));
1105                         break;
1106 
1107                 case CA_SUNW_SF_1:
1108                         ocapset.oc_sf_1.cm_val = data->c_un.c_val;
1109                         DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
1110                             DBG_STATE_ORIGINAL, CA_SUNW_SF_1,
1111                             ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach));
1112                         break;
1113 
1114                 case CA_SUNW_HW_2:
1115                         ocapset.oc_hw_2.cm_val = data->c_un.c_val;
1116                         DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
1117                             DBG_STATE_ORIGINAL, CA_SUNW_HW_2,
1118                             ocapset.oc_hw_2.cm_val, ld_targ.t_m.m_mach));
1119                         break;
1120 
1121                 case CA_SUNW_PLAT:
1122                         if ((capstr = alist_append(&ocapset.oc_plat.cl_val,
1123                             NULL, sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL)
1124                                 return (S_ERROR);
1125                         capstr->cs_str = strs + data->c_un.c_ptr;
1126                         DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
1127                             DBG_STATE_ORIGINAL, CA_SUNW_PLAT, capstr->cs_str));
1128                         break;
1129 
1130                 case CA_SUNW_MACH:
1131                         if ((capstr = alist_append(&ocapset.oc_mach.cl_val,
1132                             NULL, sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL)
1133                                 return (S_ERROR);
1134                         capstr->cs_str = strs + data->c_un.c_ptr;
1135                         DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
1136                             DBG_STATE_ORIGINAL, CA_SUNW_MACH, capstr->cs_str));
1137                         break;
1138 
1139                 case CA_SUNW_ID:
1140                         ocapset.oc_id.cs_str = strs + data->c_un.c_ptr;
1141                         DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
1142                             DBG_STATE_ORIGINAL, CA_SUNW_ID,
1143                             ocapset.oc_id.cs_str));
1144                         break;
1145                 }
1146 
1147                 /*
1148                  * Save the start of this new group.
1149                  */
1150                 if (descapndx == -1)
1151                         descapndx = ndx;
1152         }
1153         return (1);
1154 }
1155 
1156 /*
1157  * Capture any symbol capabilities symbols.  An object file that contains symbol
1158  * capabilities has an associated .SUNW_capinfo section.  This section
1159  * identifies which symbols are associated to which capabilities, together with
1160  * their associated lead symbol.  Each of these symbol pairs are recorded for
1161  * processing later.
1162  */
1163 static uintptr_t
1164 process_capinfo(Ofl_desc *ofl, Ifl_desc *ifl, Is_desc *isp)
1165 {
1166         Cap_desc        *cdp = ifl->ifl_caps;
1167         Capinfo         *capinfo = isp->is_indata->d_buf;
1168         Shdr            *shdr = isp->is_shdr;
1169         Word            cndx, capinfonum;
1170 
1171         capinfonum = (Word)(shdr->sh_size / shdr->sh_entsize);
1172 
1173         if ((cdp == NULL) || (capinfo == NULL) || (capinfonum == 0))
1174                 return (0);
1175 
1176         for (cndx = 1, capinfo++; cndx < capinfonum; cndx++, capinfo++) {
1177                 Sym_desc        *sdp, *lsdp;
1178                 Word            lndx;
1179                 uchar_t         gndx;
1180 
1181                 if ((gndx = (uchar_t)ELF_C_GROUP(*capinfo)) == 0)
1182                         continue;
1183                 lndx = (Word)ELF_C_SYM(*capinfo);
1184 
1185                 /*
1186                  * Catch any anomalies.  A capabilities symbol should be valid,
1187                  * and the capabilities lead symbol should also be global.
1188                  * Note, ld(1) -z symbolcap would create local capabilities
1189                  * symbols, but we don't enforce this so as to give the
1190                  * compilation environment a little more freedom.
1191                  */
1192                 if ((sdp = ifl->ifl_oldndx[cndx]) == NULL) {
1193                         ld_eprintf(ofl, ERR_WARNING,
1194                             MSG_INTL(MSG_CAPINFO_INVALSYM), ifl->ifl_name,
1195                             EC_WORD(isp->is_scnndx), isp->is_name, cndx,
1196                             MSG_INTL(MSG_STR_UNKNOWN));
1197                         continue;
1198                 }
1199                 if ((lndx == 0) || (lndx >= ifl->ifl_symscnt) ||
1200                     ((lsdp = ifl->ifl_oldndx[lndx]) == NULL) ||
1201                     (ELF_ST_BIND(lsdp->sd_sym->st_info) != STB_GLOBAL)) {
1202                         ld_eprintf(ofl, ERR_WARNING,
1203                             MSG_INTL(MSG_CAPINFO_INVALLEAD), ifl->ifl_name,
1204                             EC_WORD(isp->is_scnndx), isp->is_name, cndx, lsdp ?
1205                             demangle(lsdp->sd_name) : MSG_INTL(MSG_STR_UNKNOWN),
1206                             lndx);
1207                         continue;
1208                 }
1209 
1210                 /*
1211                  * Indicate that this is a capabilities symbol.
1212                  */
1213                 sdp->sd_flags |= FLG_SY_CAP;
1214 
1215                 /*
1216                  * Save any global capability symbols.  Global capability
1217                  * symbols are identified with a CAPINFO_SUNW_GLOB group id.
1218                  * The lead symbol for this global capability symbol is either
1219                  * the symbol itself, or an alias.
1220                  */
1221                 if (gndx == CAPINFO_SUNW_GLOB) {
1222                         if (ld_cap_add_family(ofl, lsdp, sdp,
1223                             NULL, NULL) == S_ERROR)
1224                                 return (S_ERROR);
1225                         continue;
1226                 }
1227 
1228                 /*
1229                  * Track the number of non-global capabilities symbols, as these
1230                  * are used to size any symbol tables.  If we're generating a
1231                  * dynamic object, this symbol will be added to the dynamic
1232                  * symbol table, therefore ensure there is space in the dynamic
1233                  * string table.
1234                  */
1235                 ofl->ofl_caploclcnt++;
1236                 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
1237                     (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1))
1238                         return (S_ERROR);
1239 
1240                 /*
1241                  * As we're tracking this local symbol as a capabilities symbol,
1242                  * reduce the local symbol count to compensate.
1243                  */
1244                 ofl->ofl_locscnt--;
1245 
1246                 /*
1247                  * Determine whether the associated lead symbol indicates
1248                  * NODYNSORT.  If so, remove this local entry from the
1249                  * SUNW_dynsort section too.  NODYNSORT tagging can only be
1250                  * obtained from a mapfile symbol definition, and thus any
1251                  * global definition that has this tagging has already been
1252                  * instantiated and this instance resolved to it.
1253                  */
1254                 if (lsdp->sd_flags & FLG_SY_NODYNSORT) {
1255                         Sym     *lsym = lsdp->sd_sym;
1256                         uchar_t ltype = ELF_ST_TYPE(lsym->st_info);
1257 
1258                         DYNSORT_COUNT(lsdp, lsym, ltype, --);
1259                         lsdp->sd_flags |= FLG_SY_NODYNSORT;
1260                 }
1261 
1262                 /*
1263                  * Track this family member, together with its associated group.
1264                  */
1265                 if (ld_cap_add_family(ofl, lsdp, sdp,
1266                     cdp->ca_groups->apl_data[gndx], NULL) == S_ERROR)
1267                         return (S_ERROR);
1268         }
1269 
1270         return (0);
1271 }
1272 
1273 /*
1274  * Simply process the section so that we have pointers to the data for use
1275  * in later routines, however don't add the section to the output section
1276  * list as we will be creating our own replacement sections later (ie.
1277  * symtab and relocation).
1278  */
1279 static uintptr_t
1280 /* ARGSUSED5 */
1281 process_input(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1282     Word ndx, int ident, Ofl_desc *ofl)
1283 {
1284         return (process_section(name, ifl, shdr, scn, ndx,
1285             ld_targ.t_id.id_null, ofl));
1286 }
1287 
1288 /*
1289  * Keep a running count of relocation entries from input relocatable objects for
1290  * sizing relocation buckets later.  If we're building an executable, save any
1291  * relocations from shared objects to determine if any copy relocation symbol
1292  * has a displacement relocation against it.
1293  */
1294 static uintptr_t
1295 /* ARGSUSED5 */
1296 process_reloc(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1297     Word ndx, int ident, Ofl_desc *ofl)
1298 {
1299         if (process_section(name, ifl,
1300             shdr, scn, ndx, ld_targ.t_id.id_null, ofl) == S_ERROR)
1301                 return (S_ERROR);
1302 
1303         if (ifl->ifl_ehdr->e_type == ET_REL) {
1304                 if (shdr->sh_entsize && (shdr->sh_entsize <= shdr->sh_size))
1305                         /* LINTED */
1306                         ofl->ofl_relocincnt +=
1307                             (Word)(shdr->sh_size / shdr->sh_entsize);
1308         } else if (ofl->ofl_flags & FLG_OF_EXEC) {
1309                 if (aplist_append(&ifl->ifl_relsect, ifl->ifl_isdesc[ndx],
1310                     AL_CNT_IFL_RELSECS) == NULL)
1311                         return (S_ERROR);
1312         }
1313         return (1);
1314 }
1315 
1316 /*
1317  * Process a string table section.  A valid section contains an initial and
1318  * final null byte.
1319  */
1320 static uintptr_t
1321 process_strtab(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1322     Word ndx, int ident, Ofl_desc *ofl)
1323 {
1324         char            *data;
1325         size_t          size;
1326         Is_desc         *isp;
1327         uintptr_t       error;
1328 
1329         /*
1330          * Never include .stab.excl sections in any output file.
1331          * If the -s flag has been specified strip any .stab sections.
1332          */
1333         if (((ofl->ofl_flags & FLG_OF_STRIP) && ident &&
1334             (strncmp(name, MSG_ORIG(MSG_SCN_STAB), MSG_SCN_STAB_SIZE) == 0)) ||
1335             (strcmp(name, MSG_ORIG(MSG_SCN_STABEXCL)) == 0) && ident)
1336                 return (1);
1337 
1338         /*
1339          * If we got here to process a .shstrtab or .dynstr table, `ident' will
1340          * be null.  Otherwise make sure we don't have a .strtab section as this
1341          * should not be added to the output section list either.
1342          */
1343         if ((ident != ld_targ.t_id.id_null) &&
1344             (strcmp(name, MSG_ORIG(MSG_SCN_STRTAB)) == 0))
1345                 ident = ld_targ.t_id.id_null;
1346 
1347         error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
1348         if ((error == 0) || (error == S_ERROR))
1349                 return (error);
1350 
1351         /*
1352          * String tables should start and end with a NULL byte.  Note, it has
1353          * been known for the assembler to create empty string tables, so check
1354          * the size before attempting to verify the data itself.
1355          */
1356         isp = ifl->ifl_isdesc[ndx];
1357         size = isp->is_indata->d_size;
1358         if (size) {
1359                 data = isp->is_indata->d_buf;
1360                 if (data[0] != '\0' || data[size - 1] != '\0')
1361                         ld_eprintf(ofl, ERR_WARNING,
1362                             MSG_INTL(MSG_FIL_MALSTR), ifl->ifl_name,
1363                             EC_WORD(isp->is_scnndx), name);
1364         } else
1365                 isp->is_indata->d_buf = (void *)MSG_ORIG(MSG_STR_EMPTY);
1366 
1367         ifl->ifl_flags |= FLG_IF_HSTRTAB;
1368         return (1);
1369 }
1370 
1371 /*
1372  * Invalid sections produce a warning and are skipped.
1373  */
1374 static uintptr_t
1375 /* ARGSUSED3 */
1376 invalid_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1377     Word ndx, int ident, Ofl_desc *ofl)
1378 {
1379         Conv_inv_buf_t inv_buf;
1380 
1381         ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_INVALSEC),
1382             ifl->ifl_name, EC_WORD(ndx), name,
1383             conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
1384             ifl->ifl_ehdr->e_machine, shdr->sh_type, 0, &inv_buf));
1385         return (1);
1386 }
1387 
1388 /*
1389  * Compare an input section name to a given string, taking the ELF '%'
1390  * section naming convention into account. If an input section name
1391  * contains a '%' character, the '%' and all following characters are
1392  * ignored in the comparison.
1393  *
1394  * entry:
1395  *      is_name - Name of input section
1396  *      match_name - Name to compare to
1397  *      match_len - strlen(match_name)
1398  *
1399  * exit:
1400  *      Returns True (1) if the names match, and False (0) otherwise.
1401  */
1402 inline static int
1403 is_name_cmp(const char *is_name, const char *match_name, size_t match_len)
1404 {
1405         /*
1406          * If the start of is_name is not a match for name,
1407          * the match fails.
1408          */
1409         if (strncmp(is_name, match_name, match_len) != 0)
1410                 return (0);
1411 
1412         /*
1413          * The prefix matched. The next character must be either '%', or
1414          * NULL, in order for a match to be true.
1415          */
1416         is_name += match_len;
1417         return ((*is_name == '\0') || (*is_name == '%'));
1418 }
1419 
1420 /*
1421  * Helper routine for process_progbits() to process allocable sections.
1422  *
1423  * entry:
1424  *      name, ifl, shdr, ndx, ident, ofl - As passed to process_progbits().
1425  *      is_stab_index - TRUE if section is .index.
1426  *      is_flags - Additional flags to be added to the input section.
1427  *
1428  * exit:
1429  *      The allocable section has been processed. *ident and *is_flags
1430  *      are updated as necessary to reflect the changes. Returns TRUE
1431  *      for success, FALSE for failure.
1432  */
1433 /*ARGSUSED*/
1434 inline static Boolean
1435 process_progbits_alloc(const char *name, Ifl_desc *ifl, Shdr *shdr,
1436     Word ndx, int *ident, Ofl_desc *ofl, Boolean is_stab_index,
1437     Word *is_flags)
1438 {
1439         Boolean done = FALSE;
1440 
1441         if (name[0] == '.') {
1442                 switch (name[1]) {
1443                 case 'e':
1444                         if (!is_name_cmp(name, MSG_ORIG(MSG_SCN_EHFRAME),
1445                             MSG_SCN_EHFRAME_SIZE))
1446                                 break;
1447 
1448                         *ident = ld_targ.t_id.id_unwind;
1449                         *is_flags |= FLG_IS_EHFRAME;
1450                         done = TRUE;
1451 
1452                         /*
1453                          * Historically, the section containing the logic to
1454                          * unwind stack frames -- the .eh_frame section -- was
1455                          * of type SHT_PROGBITS.  Apparently the most
1456                          * aesthetically galling aspect of this was not the
1457                          * .eh_frame section's dubious purpose or its filthy
1458                          * implementation, but rather its section type; with the
1459                          * introduction of the AMD64 ABI, a new section header
1460                          * type (SHT_AMD64_UNWIND) was introduced for (and
1461                          * dedicated to) this section.  When both the Sun
1462                          * compilers and the GNU compilers had been modified to
1463                          * generate this new section type, the linker became
1464                          * much more pedantic about .eh_frame: it refused to
1465                          * link an AMD64 object that contained a .eh_frame with
1466                          * the legacy SHT_PROGBITS.  That this was too fussy is
1467                          * evidenced by searching the net for the error message
1468                          * that it generated ("section type is SHT_PROGBITS:
1469                          * expected SHT_AMD64_UNWIND"), which reveals a myriad
1470                          * of problems, including legacy objects, hand-coded
1471                          * assembly and otherwise cross-platform objects
1472                          * created on other platforms (the GNU toolchain was
1473                          * only modified to create the new section type on
1474                          * Solaris and derivatives).  We therefore always accept
1475                          * a .eh_frame of SHT_PROGBITS -- regardless of
1476                          * m_sht_unwind.
1477                          */
1478                         break;
1479                 case 'g':
1480                         if (is_name_cmp(name, MSG_ORIG(MSG_SCN_GOT),
1481                             MSG_SCN_GOT_SIZE)) {
1482                                 *ident = ld_targ.t_id.id_null;
1483                                 done = TRUE;
1484                                 break;
1485                         }
1486                         if ((ld_targ.t_m.m_sht_unwind == SHT_PROGBITS) &&
1487                             is_name_cmp(name, MSG_ORIG(MSG_SCN_GCC_X_TBL),
1488                             MSG_SCN_GCC_X_TBL_SIZE)) {
1489                                 *ident = ld_targ.t_id.id_unwind;
1490                                 done = TRUE;
1491                                 break;
1492                         }
1493                         break;
1494                 case 'p':
1495                         if (is_name_cmp(name, MSG_ORIG(MSG_SCN_PLT),
1496                             MSG_SCN_PLT_SIZE)) {
1497                                 *ident = ld_targ.t_id.id_null;
1498                                 done = TRUE;
1499                         }
1500                         break;
1501                 }
1502         }
1503         if (!done) {
1504                 if (is_stab_index) {
1505                         /*
1506                          * This is a work-around for x86 compilers that have
1507                          * set SHF_ALLOC for the .stab.index section.
1508                          *
1509                          * Because of this, make sure that the .stab.index
1510                          * does not end up as the last section in the text
1511                          * segment. Older linkers can produce segmentation
1512                          * violations when they strip (ld -s) against a
1513                          * shared object whose last section in the text
1514                          * segment is a .stab.
1515                          */
1516                         *ident = ld_targ.t_id.id_interp;
1517                 } else {
1518                         *ident = ld_targ.t_id.id_data;
1519                 }
1520         }
1521 
1522         return (TRUE);
1523 }
1524 
1525 /*
1526  * Process a progbits section.
1527  */
1528 static uintptr_t
1529 process_progbits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1530     Word ndx, int ident, Ofl_desc *ofl)
1531 {
1532         Boolean         is_stab_index = FALSE;
1533         Word            is_flags = 0;
1534         uintptr_t       r;
1535 
1536         /*
1537          * Never include .stab.excl sections in any output file.
1538          * If the -s flag has been specified strip any .stab sections.
1539          */
1540         if (ident && (strncmp(name, MSG_ORIG(MSG_SCN_STAB),
1541             MSG_SCN_STAB_SIZE) == 0)) {
1542                 if ((ofl->ofl_flags & FLG_OF_STRIP) ||
1543                     (strcmp((name + MSG_SCN_STAB_SIZE),
1544                     MSG_ORIG(MSG_SCN_EXCL)) == 0))
1545                         return (1);
1546 
1547                 if (strcmp((name + MSG_SCN_STAB_SIZE),
1548                     MSG_ORIG(MSG_SCN_INDEX)) == 0)
1549                         is_stab_index = TRUE;
1550         }
1551 
1552         if ((ofl->ofl_flags & FLG_OF_STRIP) && ident) {
1553                 if ((strncmp(name, MSG_ORIG(MSG_SCN_DEBUG),
1554                     MSG_SCN_DEBUG_SIZE) == 0) ||
1555                     (strcmp(name, MSG_ORIG(MSG_SCN_LINE)) == 0))
1556                         return (1);
1557         }
1558 
1559         /*
1560          * Update the ident to reflect the type of section we've got.
1561          *
1562          * If there is any .plt or .got section to generate we'll be creating
1563          * our own version, so don't allow any input sections of these types to
1564          * be added to the output section list (why a relocatable object would
1565          * have a .plt or .got is a mystery, but stranger things have occurred).
1566          *
1567          * If there are any unwind sections, and this is a platform that uses
1568          * SHT_PROGBITS for unwind sections, then set their ident to reflect
1569          * that.
1570          */
1571         if (ident) {
1572                 if (shdr->sh_flags & SHF_TLS) {
1573                         ident = ld_targ.t_id.id_tls;
1574                 } else if ((shdr->sh_flags & ~ALL_SHF_IGNORE) ==
1575                     (SHF_ALLOC | SHF_EXECINSTR)) {
1576                         ident = ld_targ.t_id.id_text;
1577                 } else if (shdr->sh_flags & SHF_ALLOC) {
1578                         if (process_progbits_alloc(name, ifl, shdr, ndx,
1579                             &ident, ofl, is_stab_index, &is_flags) == FALSE)
1580                                 return (S_ERROR);
1581                 } else {
1582                         ident = ld_targ.t_id.id_note;
1583                 }
1584         }
1585 
1586         r = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
1587 
1588         /*
1589          * On success, process_section() creates an input section descriptor.
1590          * Now that it exists, we can add any pending input section flags.
1591          */
1592         if ((is_flags != 0) && (r == 1))
1593                 ifl->ifl_isdesc[ndx]->is_flags |= is_flags;
1594 
1595         return (r);
1596 }
1597 
1598 /*
1599  * Handles the SHT_SUNW_{DEBUG,DEBUGSTR) sections.
1600  */
1601 static uintptr_t
1602 process_debug(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1603     Word ndx, int ident, Ofl_desc *ofl)
1604 {
1605         /*
1606          * Debug information is discarded when the 'ld -s' flag is invoked.
1607          */
1608         if (ofl->ofl_flags & FLG_OF_STRIP) {
1609                 return (1);
1610         }
1611         return (process_progbits(name, ifl, shdr, scn, ndx, ident, ofl));
1612 }
1613 
1614 /*
1615  * Process a nobits section.
1616  */
1617 static uintptr_t
1618 process_nobits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1619     Word ndx, int ident, Ofl_desc *ofl)
1620 {
1621         if (ident) {
1622                 if (shdr->sh_flags & SHF_TLS)
1623                         ident = ld_targ.t_id.id_tlsbss;
1624 #if     defined(_ELF64)
1625                 else if ((shdr->sh_flags & SHF_AMD64_LARGE) &&
1626                     (ld_targ.t_m.m_mach == EM_AMD64))
1627                         ident = ld_targ.t_id.id_lbss;
1628 #endif
1629                 else
1630                         ident = ld_targ.t_id.id_bss;
1631         }
1632         return (process_section(name, ifl, shdr, scn, ndx, ident, ofl));
1633 }
1634 
1635 /*
1636  * Process a SHT_*_ARRAY section.
1637  */
1638 static uintptr_t
1639 process_array(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1640     Word ndx, int ident, Ofl_desc *ofl)
1641 {
1642         uintptr_t       error;
1643 
1644         if (ident)
1645                 ident = ld_targ.t_id.id_array;
1646 
1647         error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
1648         if ((error == 0) || (error == S_ERROR))
1649                 return (error);
1650 
1651         return (1);
1652 }
1653 
1654 static uintptr_t
1655 /* ARGSUSED1 */
1656 array_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
1657 {
1658         Os_desc *osp;
1659         Shdr    *shdr;
1660 
1661         if ((isc == NULL) || ((osp = isc->is_osdesc) == NULL))
1662                 return (0);
1663 
1664         shdr = isc->is_shdr;
1665 
1666         if ((shdr->sh_type == SHT_FINI_ARRAY) &&
1667             (ofl->ofl_osfiniarray == NULL))
1668                 ofl->ofl_osfiniarray = osp;
1669         else if ((shdr->sh_type == SHT_INIT_ARRAY) &&
1670             (ofl->ofl_osinitarray == NULL))
1671                 ofl->ofl_osinitarray = osp;
1672         else if ((shdr->sh_type == SHT_PREINIT_ARRAY) &&
1673             (ofl->ofl_ospreinitarray == NULL))
1674                 ofl->ofl_ospreinitarray = osp;
1675 
1676         return (1);
1677 }
1678 
1679 /*
1680  * Process a SHT_SYMTAB_SHNDX section.
1681  */
1682 static uintptr_t
1683 process_sym_shndx(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1684     Word ndx, int ident, Ofl_desc *ofl)
1685 {
1686         if (process_input(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR)
1687                 return (S_ERROR);
1688 
1689         /*
1690          * Have we already seen the related SYMTAB - if so verify it now.
1691          */
1692         if (shdr->sh_link < ndx) {
1693                 Is_desc *isp = ifl->ifl_isdesc[shdr->sh_link];
1694 
1695                 if ((isp == NULL) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
1696                     (isp->is_shdr->sh_type != SHT_DYNSYM))) {
1697                         ld_eprintf(ofl, ERR_FATAL,
1698                             MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name,
1699                             EC_WORD(ndx), name, EC_XWORD(shdr->sh_link));
1700                         return (S_ERROR);
1701                 }
1702                 isp->is_symshndx = ifl->ifl_isdesc[ndx];
1703         }
1704         return (1);
1705 }
1706 
1707 /*
1708  * Final processing for SHT_SYMTAB_SHNDX section.
1709  */
1710 static uintptr_t
1711 /* ARGSUSED2 */
1712 sym_shndx_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
1713 {
1714         if (isc->is_shdr->sh_link > isc->is_scnndx) {
1715                 Is_desc *isp = ifl->ifl_isdesc[isc->is_shdr->sh_link];
1716 
1717                 if ((isp == NULL) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
1718                     (isp->is_shdr->sh_type != SHT_DYNSYM))) {
1719                         ld_eprintf(ofl, ERR_FATAL,
1720                             MSG_INTL(MSG_FIL_INVSHLINK), isc->is_file->ifl_name,
1721                             EC_WORD(isc->is_scnndx), isc->is_name,
1722                             EC_XWORD(isc->is_shdr->sh_link));
1723                         return (S_ERROR);
1724                 }
1725                 isp->is_symshndx = isc;
1726         }
1727         return (1);
1728 }
1729 
1730 /*
1731  * Process .dynamic section from a relocatable object.
1732  *
1733  * Note: That the .dynamic section is only considered interesting when
1734  *       dlopen()ing a relocatable object (thus FLG_OF1_RELDYN can only get
1735  *       set when libld is called from ld.so.1).
1736  */
1737 /*ARGSUSED*/
1738 static uintptr_t
1739 process_rel_dynamic(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1740     Word ndx, int ident, Ofl_desc *ofl)
1741 {
1742         Dyn             *dyn;
1743         Elf_Scn         *strscn;
1744         Elf_Data        *dp;
1745         char            *str;
1746 
1747         /*
1748          * Process .dynamic sections from relocatable objects ?
1749          */
1750         if ((ofl->ofl_flags1 & FLG_OF1_RELDYN) == 0)
1751                 return (1);
1752 
1753         /*
1754          * Find the string section associated with the .dynamic section.
1755          */
1756         if ((strscn = elf_getscn(ifl->ifl_elf, shdr->sh_link)) == NULL) {
1757                 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
1758                     ifl->ifl_name);
1759                 return (0);
1760         }
1761         dp = elf_getdata(strscn, NULL);
1762         str = (char *)dp->d_buf;
1763 
1764         /*
1765          * And get the .dynamic data
1766          */
1767         dp = elf_getdata(scn, NULL);
1768 
1769         for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
1770                 Ifl_desc        *difl;
1771 
1772                 switch (dyn->d_tag) {
1773                 case DT_NEEDED:
1774                 case DT_USED:
1775                         if (((difl = libld_calloc(1,
1776                             sizeof (Ifl_desc))) == NULL) ||
1777                             (aplist_append(&ofl->ofl_sos, difl,
1778                             AL_CNT_OFL_LIBS) == NULL))
1779                                 return (S_ERROR);
1780 
1781                         difl->ifl_name = MSG_ORIG(MSG_STR_DYNAMIC);
1782                         difl->ifl_soname = str + (size_t)dyn->d_un.d_val;
1783                         difl->ifl_flags = FLG_IF_NEEDSTR;
1784                         break;
1785                 case DT_RPATH:
1786                 case DT_RUNPATH:
1787                         if ((ofl->ofl_rpath = add_string(ofl->ofl_rpath,
1788                             (str + (size_t)dyn->d_un.d_val))) ==
1789                             (const char *)S_ERROR)
1790                                 return (S_ERROR);
1791                         break;
1792                 case DT_VERSYM:
1793                         /*
1794                          * The Solaris ld does not put DT_VERSYM in the
1795                          * dynamic section. If the object has DT_VERSYM,
1796                          * then it must have been produced by the GNU ld,
1797                          * and is using the GNU style of versioning.
1798                          */
1799                         ifl->ifl_flags |= FLG_IF_GNUVER;
1800                         break;
1801                 }
1802         }
1803         return (1);
1804 }
1805 
1806 /*
1807  * Expand implicit references.  Dependencies can be specified in terms of the
1808  * $ORIGIN, $MACHINE, $PLATFORM, $OSREL and $OSNAME tokens, either from their
1809  * needed name, or via a runpath.  In addition runpaths may also specify the
1810  * $ISALIST token.
1811  *
1812  * Probably the most common reference to explicit dependencies (via -L) will be
1813  * sufficient to find any associated implicit dependencies, but just in case we
1814  * expand any occurrence of these known tokens here.
1815  *
1816  * Note, if any errors occur we simply return the original name.
1817  *
1818  * This code is remarkably similar to expand() in rtld/common/paths.c.
1819  */
1820 static char             *machine = NULL;
1821 static size_t           machine_sz = 0;
1822 static char             *platform = NULL;
1823 static size_t           platform_sz = 0;
1824 static Isa_desc         *isa = NULL;
1825 static Uts_desc         *uts = NULL;
1826 
1827 static char *
1828 expand(const char *parent, const char *name, char **next)
1829 {
1830         char            _name[PATH_MAX], *nptr, *_next;
1831         const char      *optr;
1832         size_t          nrem = PATH_MAX - 1;
1833         int             expanded = 0, _expanded, isaflag = 0;
1834 
1835         optr = name;
1836         nptr = _name;
1837 
1838         while (*optr) {
1839                 if (nrem == 0)
1840                         return ((char *)name);
1841 
1842                 if (*optr != '$') {
1843                         *nptr++ = *optr++, nrem--;
1844                         continue;
1845                 }
1846 
1847                 _expanded = 0;
1848 
1849                 if (strncmp(optr, MSG_ORIG(MSG_STR_ORIGIN),
1850                     MSG_STR_ORIGIN_SIZE) == 0) {
1851                         char *eptr;
1852 
1853                         /*
1854                          * For $ORIGIN, expansion is really just a concatenation
1855                          * of the parents directory name.  For example, an
1856                          * explicit dependency foo/bar/lib1.so with a dependency
1857                          * on $ORIGIN/lib2.so would be expanded to
1858                          * foo/bar/lib2.so.
1859                          */
1860                         if ((eptr = strrchr(parent, '/')) == NULL) {
1861                                 *nptr++ = '.';
1862                                 nrem--;
1863                         } else {
1864                                 size_t  len = eptr - parent;
1865 
1866                                 if (len >= nrem)
1867                                         return ((char *)name);
1868 
1869                                 (void) strncpy(nptr, parent, len);
1870                                 nptr = nptr + len;
1871                                 nrem -= len;
1872                         }
1873                         optr += MSG_STR_ORIGIN_SIZE;
1874                         expanded = _expanded = 1;
1875 
1876                 } else if (strncmp(optr, MSG_ORIG(MSG_STR_MACHINE),
1877                     MSG_STR_MACHINE_SIZE) == 0) {
1878                         /*
1879                          * Establish the machine from sysconf - like uname -i.
1880                          */
1881                         if ((machine == NULL) && (machine_sz == 0)) {
1882                                 char    info[SYS_NMLN];
1883                                 long    size;
1884 
1885                                 size = sysinfo(SI_MACHINE, info, SYS_NMLN);
1886                                 if ((size != -1) &&
1887                                     (machine = libld_malloc((size_t)size))) {
1888                                         (void) strcpy(machine, info);
1889                                         machine_sz = (size_t)size - 1;
1890                                 } else
1891                                         machine_sz = 1;
1892                         }
1893                         if (machine) {
1894                                 if (machine_sz >= nrem)
1895                                         return ((char *)name);
1896 
1897                                 (void) strncpy(nptr, machine, machine_sz);
1898                                 nptr = nptr + machine_sz;
1899                                 nrem -= machine_sz;
1900 
1901                                 optr += MSG_STR_MACHINE_SIZE;
1902                                 expanded = _expanded = 1;
1903                         }
1904 
1905                 } else if (strncmp(optr, MSG_ORIG(MSG_STR_PLATFORM),
1906                     MSG_STR_PLATFORM_SIZE) == 0) {
1907                         /*
1908                          * Establish the platform from sysconf - like uname -i.
1909                          */
1910                         if ((platform == NULL) && (platform_sz == 0)) {
1911                                 char    info[SYS_NMLN];
1912                                 long    size;
1913 
1914                                 size = sysinfo(SI_PLATFORM, info, SYS_NMLN);
1915                                 if ((size != -1) &&
1916                                     (platform = libld_malloc((size_t)size))) {
1917                                         (void) strcpy(platform, info);
1918                                         platform_sz = (size_t)size - 1;
1919                                 } else
1920                                         platform_sz = 1;
1921                         }
1922                         if (platform) {
1923                                 if (platform_sz >= nrem)
1924                                         return ((char *)name);
1925 
1926                                 (void) strncpy(nptr, platform, platform_sz);
1927                                 nptr = nptr + platform_sz;
1928                                 nrem -= platform_sz;
1929 
1930                                 optr += MSG_STR_PLATFORM_SIZE;
1931                                 expanded = _expanded = 1;
1932                         }
1933 
1934                 } else if (strncmp(optr, MSG_ORIG(MSG_STR_OSNAME),
1935                     MSG_STR_OSNAME_SIZE) == 0) {
1936                         /*
1937                          * Establish the os name - like uname -s.
1938                          */
1939                         if (uts == NULL)
1940                                 uts = conv_uts();
1941 
1942                         if (uts && uts->uts_osnamesz) {
1943                                 if (uts->uts_osnamesz >= nrem)
1944                                         return ((char *)name);
1945 
1946                                 (void) strncpy(nptr, uts->uts_osname,
1947                                     uts->uts_osnamesz);
1948                                 nptr = nptr + uts->uts_osnamesz;
1949                                 nrem -= uts->uts_osnamesz;
1950 
1951                                 optr += MSG_STR_OSNAME_SIZE;
1952                                 expanded = _expanded = 1;
1953                         }
1954 
1955                 } else if (strncmp(optr, MSG_ORIG(MSG_STR_OSREL),
1956                     MSG_STR_OSREL_SIZE) == 0) {
1957                         /*
1958                          * Establish the os release - like uname -r.
1959                          */
1960                         if (uts == NULL)
1961                                 uts = conv_uts();
1962 
1963                         if (uts && uts->uts_osrelsz) {
1964                                 if (uts->uts_osrelsz >= nrem)
1965                                         return ((char *)name);
1966 
1967                                 (void) strncpy(nptr, uts->uts_osrel,
1968                                     uts->uts_osrelsz);
1969                                 nptr = nptr + uts->uts_osrelsz;
1970                                 nrem -= uts->uts_osrelsz;
1971 
1972                                 optr += MSG_STR_OSREL_SIZE;
1973                                 expanded = _expanded = 1;
1974                         }
1975 
1976                 } else if ((strncmp(optr, MSG_ORIG(MSG_STR_ISALIST),
1977                     MSG_STR_ISALIST_SIZE) == 0) && next && (isaflag++ == 0)) {
1978                         /*
1979                          * Establish instruction sets from sysconf.  Note that
1980                          * this is only meaningful from runpaths.
1981                          */
1982                         if (isa == NULL)
1983                                 isa = conv_isalist();
1984 
1985                         if (isa && isa->isa_listsz &&
1986                             (nrem > isa->isa_opt->isa_namesz)) {
1987                                 size_t          mlen, tlen, hlen = optr - name;
1988                                 size_t          no;
1989                                 char            *lptr;
1990                                 Isa_opt         *opt = isa->isa_opt;
1991 
1992                                 (void) strncpy(nptr, opt->isa_name,
1993                                     opt->isa_namesz);
1994                                 nptr = nptr + opt->isa_namesz;
1995                                 nrem -= opt->isa_namesz;
1996 
1997                                 optr += MSG_STR_ISALIST_SIZE;
1998                                 expanded = _expanded = 1;
1999 
2000                                 tlen = strlen(optr);
2001 
2002                                 /*
2003                                  * As ISALIST expands to a number of elements,
2004                                  * establish a new list to return to the caller.
2005                                  * This will contain the present path being
2006                                  * processed redefined for each isalist option,
2007                                  * plus the original remaining list entries.
2008                                  */
2009                                 mlen = ((hlen + tlen) * (isa->isa_optno - 1)) +
2010                                     isa->isa_listsz - opt->isa_namesz;
2011                                 if (*next)
2012                                         mlen += strlen(*next);
2013                                 if ((_next = lptr = libld_malloc(mlen)) == NULL)
2014                                         return (0);
2015 
2016                                 for (no = 1, opt++; no < isa->isa_optno;
2017                                     no++, opt++) {
2018                                         (void) strncpy(lptr, name, hlen);
2019                                         lptr = lptr + hlen;
2020                                         (void) strncpy(lptr, opt->isa_name,
2021                                             opt->isa_namesz);
2022                                         lptr = lptr + opt->isa_namesz;
2023                                         (void) strncpy(lptr, optr, tlen);
2024                                         lptr = lptr + tlen;
2025                                         *lptr++ = ':';
2026                                 }
2027                                 if (*next)
2028                                         (void) strcpy(lptr, *next);
2029                                 else
2030                                         *--lptr = '\0';
2031                         }
2032                 }
2033 
2034                 /*
2035                  * If no expansion occurred skip the $ and continue.
2036                  */
2037                 if (_expanded == 0)
2038                         *nptr++ = *optr++, nrem--;
2039         }
2040 
2041         /*
2042          * If any ISALIST processing has occurred not only do we return the
2043          * expanded node we're presently working on, but we must also update the
2044          * remaining list so that it is effectively prepended with this node
2045          * expanded to all remaining isalist options.  Note that we can only
2046          * handle one ISALIST per node.  For more than one ISALIST to be
2047          * processed we'd need a better algorithm than above to replace the
2048          * newly generated list.  Whether we want to encourage the number of
2049          * pathname permutations this would provide is another question. So, for
2050          * now if more than one ISALIST is encountered we return the original
2051          * node untouched.
2052          */
2053         if (isaflag) {
2054                 if (isaflag == 1)
2055                         *next = _next;
2056                 else
2057                         return ((char *)name);
2058         }
2059 
2060         *nptr = '\0';
2061 
2062         if (expanded) {
2063                 if ((nptr = libld_malloc(strlen(_name) + 1)) == NULL)
2064                         return ((char *)name);
2065                 (void) strcpy(nptr, _name);
2066                 return (nptr);
2067         }
2068         return ((char *)name);
2069 }
2070 
2071 /*
2072  * The Solaris ld does not put DT_VERSYM in the dynamic section, but the
2073  * GNU ld does, and it is used by the runtime linker to implement their
2074  * versioning scheme. Use this fact to determine if the sharable object
2075  * was produced by the GNU ld rather than the Solaris one, and to set
2076  * FLG_IF_GNUVER if so. This needs to be done before the symbols are
2077  * processed, since the answer determines whether we interpret the
2078  * symbols versions according to Solaris or GNU rules.
2079  */
2080 /*ARGSUSED*/
2081 static uintptr_t
2082 process_dynamic_isgnu(const char *name, Ifl_desc *ifl, Shdr *shdr,
2083     Elf_Scn *scn, Word ndx, int ident, Ofl_desc *ofl)
2084 {
2085         Dyn             *dyn;
2086         Elf_Data        *dp;
2087         uintptr_t       error;
2088 
2089         error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
2090         if ((error == 0) || (error == S_ERROR))
2091                 return (error);
2092 
2093         /* Get the .dynamic data */
2094         dp = elf_getdata(scn, NULL);
2095 
2096         for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
2097                 if (dyn->d_tag == DT_VERSYM) {
2098                         ifl->ifl_flags |= FLG_IF_GNUVER;
2099                         break;
2100                 }
2101         }
2102         return (1);
2103 }
2104 
2105 /*
2106  * Process a dynamic section.  If we are processing an explicit shared object
2107  * then we need to determine if it has a recorded SONAME, if so, this name will
2108  * be recorded in the output file being generated as the NEEDED entry rather
2109  * than the shared objects filename itself.
2110  * If the mode of the link-edit indicates that no undefined symbols should
2111  * remain, then we also need to build up a list of any additional shared object
2112  * dependencies this object may have.  In this case save any NEEDED entries
2113  * together with any associated run-path specifications.  This information is
2114  * recorded on the `ofl_soneed' list and will be analyzed after all explicit
2115  * file processing has been completed (refer finish_libs()).
2116  */
2117 static uintptr_t
2118 process_dynamic(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
2119 {
2120         Dyn             *data, *dyn;
2121         char            *str, *rpath = NULL;
2122         const char      *soname, *needed;
2123         Boolean         no_undef;
2124 
2125         data = (Dyn *)isc->is_indata->d_buf;
2126         str = (char *)ifl->ifl_isdesc[isc->is_shdr->sh_link]->is_indata->d_buf;
2127 
2128         /* Determine if we need to examine the runpaths and NEEDED entries */
2129         no_undef = (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) ||
2130             OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS);
2131 
2132         /*
2133          * First loop through the dynamic section looking for a run path.
2134          */
2135         if (no_undef) {
2136                 for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
2137                         if ((dyn->d_tag != DT_RPATH) &&
2138                             (dyn->d_tag != DT_RUNPATH))
2139                                 continue;
2140                         if ((rpath = str + (size_t)dyn->d_un.d_val) == NULL)
2141                                 continue;
2142                         break;
2143                 }
2144         }
2145 
2146         /*
2147          * Now look for any needed dependencies (which may use the rpath)
2148          * or a new SONAME.
2149          */
2150         for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
2151                 if (dyn->d_tag == DT_SONAME) {
2152                         if ((soname = str + (size_t)dyn->d_un.d_val) == NULL)
2153                                 continue;
2154 
2155                         /*
2156                          * Update the input file structure with this new name.
2157                          */
2158                         ifl->ifl_soname = soname;
2159 
2160                 } else if ((dyn->d_tag == DT_NEEDED) ||
2161                     (dyn->d_tag == DT_USED)) {
2162                         Sdf_desc        *sdf;
2163 
2164                         if (!no_undef)
2165                                 continue;
2166                         if ((needed = str + (size_t)dyn->d_un.d_val) == NULL)
2167                                 continue;
2168 
2169                         /*
2170                          * Determine if this needed entry is already recorded on
2171                          * the shared object needed list, if not create a new
2172                          * definition for later processing (see finish_libs()).
2173                          */
2174                         needed = expand(ifl->ifl_name, needed, NULL);
2175 
2176                         if ((sdf = sdf_find(needed, ofl->ofl_soneed)) == NULL) {
2177                                 if ((sdf = sdf_add(needed,
2178                                     &ofl->ofl_soneed)) == (Sdf_desc *)S_ERROR)
2179                                         return (S_ERROR);
2180                                 sdf->sdf_rfile = ifl->ifl_name;
2181                         }
2182 
2183                         /*
2184                          * Record the runpath (Note that we take the first
2185                          * runpath which is exactly what ld.so.1 would do during
2186                          * its dependency processing).
2187                          */
2188                         if (rpath && (sdf->sdf_rpath == NULL))
2189                                 sdf->sdf_rpath = rpath;
2190 
2191                 } else if (dyn->d_tag == DT_FLAGS_1) {
2192                         if (dyn->d_un.d_val & (DF_1_INITFIRST | DF_1_INTERPOSE))
2193                                 ifl->ifl_flags &= ~FLG_IF_LAZYLD;
2194                         if (dyn->d_un.d_val & DF_1_DISPRELPND)
2195                                 ifl->ifl_flags |= FLG_IF_DISPPEND;
2196                         if (dyn->d_un.d_val & DF_1_DISPRELDNE)
2197                                 ifl->ifl_flags |= FLG_IF_DISPDONE;
2198                         if (dyn->d_un.d_val & DF_1_NODIRECT)
2199                                 ifl->ifl_flags |= FLG_IF_NODIRECT;
2200 
2201                         /*
2202                          * If we are building an executable, and this
2203                          * dependency is tagged as an interposer, then
2204                          * assume that it is required even if symbol
2205                          * resolution uncovers no evident use.
2206                          *
2207                          * If we are building a shared object, then an
2208                          * interposer dependency has no special meaning, and we
2209                          * treat it as a regular dependency. By definition, all
2210                          * interposers must be visible to the runtime linker
2211                          * at initialization time, and cannot be added later.
2212                          */
2213                         if ((dyn->d_un.d_val & DF_1_INTERPOSE) &&
2214                             (ofl->ofl_flags & FLG_OF_EXEC))
2215                                 ifl->ifl_flags |= FLG_IF_DEPREQD;
2216 
2217                 } else if ((dyn->d_tag == DT_AUDIT) &&
2218                     (ifl->ifl_flags & FLG_IF_NEEDED)) {
2219                         /*
2220                          * Record audit string as DT_DEPAUDIT.
2221                          */
2222                         if ((ofl->ofl_depaudit = add_string(ofl->ofl_depaudit,
2223                             (str + (size_t)dyn->d_un.d_val))) ==
2224                             (const char *)S_ERROR)
2225                                 return (S_ERROR);
2226 
2227                 } else if (dyn->d_tag == DT_SUNW_RTLDINF) {
2228                         /*
2229                          * If this dependency has the DT_SUNW_RTLDINF .dynamic
2230                          * entry, then ensure no specialized dependency
2231                          * processing is in effect.  This tag identifies libc,
2232                          * which provides critical startup information (TLS
2233                          * routines, threads initialization, etc.) that must
2234                          * be exercised as part of process initialization.
2235                          */
2236                         ifl->ifl_flags &= ~MSK_IF_POSFLAG1;
2237 
2238                         /*
2239                          * libc is not subject to the usual guidance checks
2240                          * for lazy loading. It cannot be lazy loaded, libld
2241                          * ignores the request, and rtld would ignore the
2242                          * setting if it were present.
2243                          */
2244                         ifl->ifl_flags |= FLG_IF_RTLDINF;
2245                 }
2246         }
2247 
2248         /*
2249          * Perform some SONAME sanity checks.
2250          */
2251         if (ifl->ifl_flags & FLG_IF_NEEDED) {
2252                 Ifl_desc        *sifl;
2253                 Aliste          idx;
2254 
2255                 /*
2256                  * Determine if anyone else will cause the same SONAME to be
2257                  * used (this is either caused by two different files having the
2258                  * same SONAME, or by one file SONAME actually matching another
2259                  * file basename (if no SONAME is specified within a shared
2260                  * library its basename will be used)). Probably rare, but some
2261                  * idiot will do it.
2262                  */
2263                 for (APLIST_TRAVERSE(ofl->ofl_sos, idx, sifl)) {
2264                         if ((strcmp(ifl->ifl_soname, sifl->ifl_soname) == 0) &&
2265                             (ifl != sifl)) {
2266                                 const char      *hint, *iflb, *siflb;
2267 
2268                                 /*
2269                                  * Determine the basename of each file. Perhaps
2270                                  * there are multiple copies of the same file
2271                                  * being brought in using different -L search
2272                                  * paths, and if so give an extra hint in the
2273                                  * error message.
2274                                  */
2275                                 iflb = strrchr(ifl->ifl_name, '/');
2276                                 if (iflb == NULL)
2277                                         iflb = ifl->ifl_name;
2278                                 else
2279                                         iflb++;
2280 
2281                                 siflb = strrchr(sifl->ifl_name, '/');
2282                                 if (siflb == NULL)
2283                                         siflb = sifl->ifl_name;
2284                                 else
2285                                         siflb++;
2286 
2287                                 if (strcmp(iflb, siflb) == 0)
2288                                         hint = MSG_INTL(MSG_REC_CNFLTHINT);
2289                                 else
2290                                         hint = MSG_ORIG(MSG_STR_EMPTY);
2291 
2292                                 ld_eprintf(ofl, ERR_FATAL,
2293                                     MSG_INTL(MSG_REC_OBJCNFLT), sifl->ifl_name,
2294                                     ifl->ifl_name, sifl->ifl_soname, hint);
2295                                 return (0);
2296                         }
2297                 }
2298 
2299                 /*
2300                  * If the SONAME is the same as the name the user wishes to
2301                  * record when building a dynamic library (refer -h option),
2302                  * we also have a name clash.
2303                  */
2304                 if (ofl->ofl_soname &&
2305                     (strcmp(ofl->ofl_soname, ifl->ifl_soname) == 0)) {
2306                         ld_eprintf(ofl, ERR_FATAL,
2307                             MSG_INTL(MSG_REC_OPTCNFLT), ifl->ifl_name,
2308                             MSG_INTL(MSG_MARG_SONAME), ifl->ifl_soname);
2309                         return (0);
2310                 }
2311         }
2312         return (1);
2313 }
2314 
2315 /*
2316  * Process a progbits section from a relocatable object (ET_REL).
2317  * This is used on non-amd64 objects to recognize .eh_frame sections.
2318  */
2319 /*ARGSUSED1*/
2320 static uintptr_t
2321 process_progbits_final(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
2322 {
2323         if (isc->is_osdesc && (isc->is_flags & FLG_IS_EHFRAME) &&
2324             (ld_unwind_register(isc->is_osdesc, ofl) == S_ERROR))
2325                 return (S_ERROR);
2326 
2327         return (1);
2328 }
2329 
2330 /*
2331  * Process a group section.
2332  */
2333 static uintptr_t
2334 process_group(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
2335     Word ndx, int ident, Ofl_desc *ofl)
2336 {
2337         uintptr_t       error;
2338 
2339         error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
2340         if ((error == 0) || (error == S_ERROR))
2341                 return (error);
2342 
2343         /*
2344          * Indicate that this input file has groups to process.  Groups are
2345          * processed after all input sections have been processed.
2346          */
2347         ifl->ifl_flags |= FLG_IF_GROUPS;
2348 
2349         return (1);
2350 }
2351 
2352 /*
2353  * Process a relocation entry. At this point all input sections from this
2354  * input file have been assigned an input section descriptor which is saved
2355  * in the `ifl_isdesc' array.
2356  */
2357 static uintptr_t
2358 rel_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
2359 {
2360         Word    rndx;
2361         Is_desc *risc;
2362         Os_desc *osp;
2363         Shdr    *shdr = isc->is_shdr;
2364         Conv_inv_buf_t inv_buf;
2365 
2366         /*
2367          * Make sure this is a valid relocation we can handle.
2368          */
2369         if (shdr->sh_type != ld_targ.t_m.m_rel_sht_type) {
2370                 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVALSEC),
2371                     ifl->ifl_name, EC_WORD(isc->is_scnndx), isc->is_name,
2372                     conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
2373                     ifl->ifl_ehdr->e_machine, shdr->sh_type, 0, &inv_buf));
2374                 return (0);
2375         }
2376 
2377         /*
2378          * From the relocation section header information determine which
2379          * section needs the actual relocation.  Determine which output section
2380          * this input section has been assigned to and add to its relocation
2381          * list.  Note that the relocation section may be null if it is not
2382          * required (ie. .debug, .stabs, etc).
2383          */
2384         rndx = shdr->sh_info;
2385         if (rndx >= ifl->ifl_shnum) {
2386                 /*
2387                  * Broken input file.
2388                  */
2389                 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
2390                     ifl->ifl_name, EC_WORD(isc->is_scnndx), isc->is_name,
2391                     EC_XWORD(rndx));
2392                 return (0);
2393         }
2394         if (rndx == 0) {
2395                 if (aplist_append(&ofl->ofl_extrarels, isc,
2396                     AL_CNT_OFL_RELS) == NULL)
2397                         return (S_ERROR);
2398 
2399         } else if ((risc = ifl->ifl_isdesc[rndx]) != NULL) {
2400                 /*
2401                  * Discard relocations if they are against a section
2402                  * which has been discarded.
2403                  */
2404                 if (risc->is_flags & FLG_IS_DISCARD)
2405                         return (1);
2406 
2407                 if ((osp = risc->is_osdesc) == NULL) {
2408                         if (risc->is_shdr->sh_type == SHT_SUNW_move) {
2409                                 /*
2410                                  * This section is processed later in
2411                                  * process_movereloc().
2412                                  */
2413                                 if (aplist_append(&ofl->ofl_ismoverel,
2414                                     isc, AL_CNT_OFL_MOVE) == NULL)
2415                                         return (S_ERROR);
2416                                 return (1);
2417                         }
2418                         ld_eprintf(ofl, ERR_FATAL,
2419                             MSG_INTL(MSG_FIL_INVRELOC1), ifl->ifl_name,
2420                             EC_WORD(isc->is_scnndx), isc->is_name,
2421                             EC_WORD(risc->is_scnndx), risc->is_name);
2422                         return (0);
2423                 }
2424                 if (aplist_append(&osp->os_relisdescs, isc,
2425                     AL_CNT_OS_RELISDESCS) == NULL)
2426                         return (S_ERROR);
2427         }
2428         return (1);
2429 }
2430 
2431 /*
2432  * SHF_EXCLUDE flags is set for this section.
2433  */
2434 static uintptr_t
2435 process_exclude(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
2436     Word ndx, Ofl_desc *ofl)
2437 {
2438         /*
2439          * Sections SHT_SYMTAB and SHT_DYNDYM, even if SHF_EXCLUDE is on, might
2440          * be needed for ld processing.  These sections need to be in the
2441          * internal table.  Later it will be determined whether they can be
2442          * eliminated or not.
2443          */
2444         if (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM)
2445                 return (0);
2446 
2447         /*
2448          * Other checks
2449          */
2450         if (shdr->sh_flags & SHF_ALLOC) {
2451                 /*
2452                  * A conflict, issue an warning message, and ignore the section.
2453                  */
2454                 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_EXCLUDE),
2455                     ifl->ifl_name, EC_WORD(ndx), name);
2456                 return (0);
2457         }
2458 
2459         /*
2460          * This sections is not going to the output file.
2461          */
2462         return (process_section(name, ifl, shdr, scn, ndx, 0, ofl));
2463 }
2464 
2465 /*
2466  * Section processing state table.  `Initial' describes the required initial
2467  * procedure to be called (if any), `Final' describes the final processing
2468  * procedure (ie. things that can only be done when all required sections
2469  * have been collected).
2470  */
2471 typedef uintptr_t       (* initial_func_t)(const char *, Ifl_desc *, Shdr *,
2472                             Elf_Scn *, Word, int, Ofl_desc *);
2473 
2474 static initial_func_t Initial[SHT_NUM][2] = {
2475 /*                      ET_REL                  ET_DYN                  */
2476 
2477 /* SHT_NULL     */      invalid_section,        invalid_section,
2478 /* SHT_PROGBITS */      process_progbits,       process_progbits,
2479 /* SHT_SYMTAB   */      process_input,          process_input,
2480 /* SHT_STRTAB   */      process_strtab,         process_strtab,
2481 /* SHT_RELA     */      process_reloc,          process_reloc,
2482 /* SHT_HASH     */      invalid_section,        NULL,
2483 /* SHT_DYNAMIC  */      process_rel_dynamic,    process_dynamic_isgnu,
2484 /* SHT_NOTE     */      process_section,        NULL,
2485 /* SHT_NOBITS   */      process_nobits,         process_nobits,
2486 /* SHT_REL      */      process_reloc,          process_reloc,
2487 /* SHT_SHLIB    */      process_section,        invalid_section,
2488 /* SHT_DYNSYM   */      invalid_section,        process_input,
2489 /* SHT_UNKNOWN12 */     process_progbits,       process_progbits,
2490 /* SHT_UNKNOWN13 */     process_progbits,       process_progbits,
2491 /* SHT_INIT_ARRAY */    process_array,          NULL,
2492 /* SHT_FINI_ARRAY */    process_array,          NULL,
2493 /* SHT_PREINIT_ARRAY */ process_array,          NULL,
2494 /* SHT_GROUP */         process_group,          invalid_section,
2495 /* SHT_SYMTAB_SHNDX */  process_sym_shndx,      NULL
2496 };
2497 
2498 typedef uintptr_t       (* final_func_t)(Is_desc *, Ifl_desc *, Ofl_desc *);
2499 
2500 static final_func_t Final[SHT_NUM][2] = {
2501 /*                      ET_REL                  ET_DYN                  */
2502 
2503 /* SHT_NULL     */      NULL,                   NULL,
2504 /* SHT_PROGBITS */      process_progbits_final, NULL,
2505 /* SHT_SYMTAB   */      ld_sym_process,         ld_sym_process,
2506 /* SHT_STRTAB   */      NULL,                   NULL,
2507 /* SHT_RELA     */      rel_process,            NULL,
2508 /* SHT_HASH     */      NULL,                   NULL,
2509 /* SHT_DYNAMIC  */      NULL,                   process_dynamic,
2510 /* SHT_NOTE     */      NULL,                   NULL,
2511 /* SHT_NOBITS   */      NULL,                   NULL,
2512 /* SHT_REL      */      rel_process,            NULL,
2513 /* SHT_SHLIB    */      NULL,                   NULL,
2514 /* SHT_DYNSYM   */      NULL,                   ld_sym_process,
2515 /* SHT_UNKNOWN12 */     NULL,                   NULL,
2516 /* SHT_UNKNOWN13 */     NULL,                   NULL,
2517 /* SHT_INIT_ARRAY */    array_process,          NULL,
2518 /* SHT_FINI_ARRAY */    array_process,          NULL,
2519 /* SHT_PREINIT_ARRAY */ array_process,          NULL,
2520 /* SHT_GROUP */         NULL,                   NULL,
2521 /* SHT_SYMTAB_SHNDX */  sym_shndx_process,      NULL
2522 };
2523 
2524 #define MAXNDXSIZE      10
2525 
2526 /*
2527  * Process an elf file.  Each section is compared against the section state
2528  * table to determine whether it should be processed (saved), ignored, or
2529  * is invalid for the type of input file being processed.
2530  */
2531 static uintptr_t
2532 process_elf(Ifl_desc *ifl, Elf *elf, Ofl_desc *ofl)
2533 {
2534         Elf_Scn         *scn;
2535         Shdr            *shdr;
2536         Word            ndx, sndx, ordndx = 0, ordcnt = 0;
2537         char            *str, *name;
2538         Word            row, column;
2539         int             ident;
2540         uintptr_t       error;
2541         Is_desc         *vdfisp, *vndisp, *vsyisp, *sifisp;
2542         Is_desc         *capinfoisp, *capisp;
2543         Sdf_desc        *sdf;
2544         Place_path_info path_info_buf, *path_info;
2545 
2546         /*
2547          * Path information buffer used by ld_place_section() and related
2548          * routines. This information is used to evaluate entrance criteria
2549          * with non-empty file matching lists (ec_files).
2550          */
2551         path_info = ld_place_path_info_init(ofl, ifl, &path_info_buf);
2552 
2553         /*
2554          * First process the .shstrtab section so that later sections can
2555          * reference their name.
2556          */
2557         ld_sup_file(ofl, ifl->ifl_name, elf_kind(elf), ifl->ifl_flags, elf);
2558 
2559         sndx = ifl->ifl_shstrndx;
2560         if ((scn = elf_getscn(elf, (size_t)sndx)) == NULL) {
2561                 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
2562                     ifl->ifl_name);
2563                 return (0);
2564         }
2565         if ((shdr = elf_getshdr(scn)) == NULL) {
2566                 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
2567                     ifl->ifl_name);
2568                 return (0);
2569         }
2570         if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
2571             NULL) {
2572                 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
2573                     ifl->ifl_name);
2574                 return (0);
2575         }
2576 
2577         if (ld_sup_input_section(ofl, ifl, name, &shdr, sndx, scn,
2578             elf) == S_ERROR)
2579                 return (S_ERROR);
2580 
2581         /*
2582          * Reset the name since the shdr->sh_name could have been changed as
2583          * part of ld_sup_input_section().
2584          */
2585         if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
2586             NULL) {
2587                 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
2588                     ifl->ifl_name);
2589                 return (0);
2590         }
2591 
2592         error = process_strtab(name, ifl, shdr, scn, sndx, FALSE, ofl);
2593         if ((error == 0) || (error == S_ERROR))
2594                 return (error);
2595         str = ifl->ifl_isdesc[sndx]->is_indata->d_buf;
2596 
2597         /*
2598          * Determine the state table column from the input file type.  Note,
2599          * shared library sections are not added to the output section list.
2600          */
2601         if (ifl->ifl_ehdr->e_type == ET_DYN) {
2602                 column = 1;
2603                 ofl->ofl_soscnt++;
2604                 ident = ld_targ.t_id.id_null;
2605         } else {
2606                 column = 0;
2607                 ofl->ofl_objscnt++;
2608                 ident = ld_targ.t_id.id_unknown;
2609         }
2610 
2611         DBG_CALL(Dbg_file_generic(ofl->ofl_lml, ifl));
2612         ndx = 0;
2613         vdfisp = vndisp = vsyisp = sifisp = capinfoisp = capisp = NULL;
2614         scn = NULL;
2615         while (scn = elf_nextscn(elf, scn)) {
2616                 ndx++;
2617 
2618                 /*
2619                  * As we've already processed the .shstrtab don't do it again.
2620                  */
2621                 if (ndx == sndx)
2622                         continue;
2623 
2624                 if ((shdr = elf_getshdr(scn)) == NULL) {
2625                         ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
2626                             ifl->ifl_name);
2627                         return (0);
2628                 }
2629                 name = str + (size_t)(shdr->sh_name);
2630 
2631                 if (ld_sup_input_section(ofl, ifl, name, &shdr, ndx, scn,
2632                     elf) == S_ERROR)
2633                         return (S_ERROR);
2634 
2635                 /*
2636                  * Reset the name since the shdr->sh_name could have been
2637                  * changed as part of ld_sup_input_section().
2638                  */
2639                 name = str + (size_t)(shdr->sh_name);
2640 
2641                 row = shdr->sh_type;
2642 
2643                 /*
2644                  * If the section has the SHF_EXCLUDE flag on, and we're not
2645                  * generating a relocatable object, exclude the section.
2646                  */
2647                 if (((shdr->sh_flags & SHF_EXCLUDE) != 0) &&
2648                     ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
2649                         if ((error = process_exclude(name, ifl, shdr, scn,
2650                             ndx, ofl)) == S_ERROR)
2651                                 return (S_ERROR);
2652                         if (error == 1)
2653                                 continue;
2654                 }
2655 
2656                 /*
2657                  * If this is a standard section type process it via the
2658                  * appropriate action routine.
2659                  */
2660                 if (row < SHT_NUM) {
2661                         if (Initial[row][column] != NULL) {
2662                                 if (Initial[row][column](name, ifl, shdr, scn,
2663                                     ndx, ident, ofl) == S_ERROR)
2664                                         return (S_ERROR);
2665                         }
2666                 } else {
2667                         /*
2668                          * If this section is below SHT_LOSUNW then we don't
2669                          * really know what to do with it, issue a warning
2670                          * message but do the basic section processing anyway.
2671                          */
2672                         if (row < (Word)SHT_LOSUNW) {
2673                                 Conv_inv_buf_t inv_buf;
2674 
2675                                 ld_eprintf(ofl, ERR_WARNING,
2676                                     MSG_INTL(MSG_FIL_INVALSEC), ifl->ifl_name,
2677                                     EC_WORD(ndx), name, conv_sec_type(
2678                                     ifl->ifl_ehdr->e_ident[EI_OSABI],
2679                                     ifl->ifl_ehdr->e_machine,
2680                                     shdr->sh_type, 0, &inv_buf));
2681                         }
2682 
2683                         /*
2684                          * Handle sections greater than SHT_LOSUNW.
2685                          */
2686                         switch (row) {
2687                         case SHT_SUNW_dof:
2688                                 if (process_section(name, ifl, shdr, scn,
2689                                     ndx, ident, ofl) == S_ERROR)
2690                                         return (S_ERROR);
2691                                 break;
2692                         case SHT_SUNW_cap:
2693                                 if (process_section(name, ifl, shdr, scn, ndx,
2694                                     ld_targ.t_id.id_null, ofl) == S_ERROR)
2695                                         return (S_ERROR);
2696                                 capisp = ifl->ifl_isdesc[ndx];
2697                                 break;
2698                         case SHT_SUNW_capinfo:
2699                                 if (process_section(name, ifl, shdr, scn, ndx,
2700                                     ld_targ.t_id.id_null, ofl) == S_ERROR)
2701                                         return (S_ERROR);
2702                                 capinfoisp = ifl->ifl_isdesc[ndx];
2703                                 break;
2704                         case SHT_SUNW_DEBUGSTR:
2705                         case SHT_SUNW_DEBUG:
2706                                 if (process_debug(name, ifl, shdr, scn,
2707                                     ndx, ident, ofl) == S_ERROR)
2708                                         return (S_ERROR);
2709                                 break;
2710                         case SHT_SUNW_move:
2711                                 if (process_section(name, ifl, shdr, scn, ndx,
2712                                     ld_targ.t_id.id_null, ofl) == S_ERROR)
2713                                         return (S_ERROR);
2714                                 break;
2715                         case SHT_SUNW_syminfo:
2716                                 if (process_section(name, ifl, shdr, scn, ndx,
2717                                     ld_targ.t_id.id_null, ofl) == S_ERROR)
2718                                         return (S_ERROR);
2719                                 sifisp = ifl->ifl_isdesc[ndx];
2720                                 break;
2721                         case SHT_SUNW_ANNOTATE:
2722                                 if (process_progbits(name, ifl, shdr, scn,
2723                                     ndx, ident, ofl) == S_ERROR)
2724                                         return (S_ERROR);
2725                                 break;
2726                         case SHT_SUNW_COMDAT:
2727                                 if (process_progbits(name, ifl, shdr, scn,
2728                                     ndx, ident, ofl) == S_ERROR)
2729                                         return (S_ERROR);
2730                                 ifl->ifl_isdesc[ndx]->is_flags |= FLG_IS_COMDAT;
2731                                 break;
2732                         case SHT_SUNW_verdef:
2733                                 if (process_section(name, ifl, shdr, scn, ndx,
2734                                     ld_targ.t_id.id_null, ofl) == S_ERROR)
2735                                         return (S_ERROR);
2736                                 vdfisp = ifl->ifl_isdesc[ndx];
2737                                 break;
2738                         case SHT_SUNW_verneed:
2739                                 if (process_section(name, ifl, shdr, scn, ndx,
2740                                     ld_targ.t_id.id_null, ofl) == S_ERROR)
2741                                         return (S_ERROR);
2742                                 vndisp = ifl->ifl_isdesc[ndx];
2743                                 break;
2744                         case SHT_SUNW_versym:
2745                                 if (process_section(name, ifl, shdr, scn, ndx,
2746                                     ld_targ.t_id.id_null, ofl) == S_ERROR)
2747                                         return (S_ERROR);
2748                                 vsyisp = ifl->ifl_isdesc[ndx];
2749                                 break;
2750                         case SHT_SPARC_GOTDATA:
2751                                 /*
2752                                  * SHT_SPARC_GOTDATA (0x70000000) is in the
2753                                  * SHT_LOPROC - SHT_HIPROC range reserved
2754                                  * for processor-specific semantics. It is
2755                                  * only meaningful for sparc targets.
2756                                  */
2757                                 if (ld_targ.t_m.m_mach !=
2758                                     LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
2759                                         goto do_default;
2760                                 if (process_section(name, ifl, shdr, scn, ndx,
2761                                     ld_targ.t_id.id_gotdata, ofl) == S_ERROR)
2762                                         return (S_ERROR);
2763                                 break;
2764 #if     defined(_ELF64)
2765                         case SHT_AMD64_UNWIND:
2766                                 /*
2767                                  * SHT_AMD64_UNWIND (0x70000001) is in the
2768                                  * SHT_LOPROC - SHT_HIPROC range reserved
2769                                  * for processor-specific semantics. It is
2770                                  * only meaningful for amd64 targets.
2771                                  */
2772                                 if (ld_targ.t_m.m_mach != EM_AMD64)
2773                                         goto do_default;
2774 
2775                                 /*
2776                                  * Target is x86, so this really is
2777                                  * SHT_AMD64_UNWIND
2778                                  */
2779                                 if (column == 0) {
2780                                         /*
2781                                          * column == ET_REL
2782                                          */
2783                                         if (process_section(name, ifl, shdr,
2784                                             scn, ndx, ld_targ.t_id.id_unwind,
2785                                             ofl) == S_ERROR)
2786                                                 return (S_ERROR);
2787                                         ifl->ifl_isdesc[ndx]->is_flags |=
2788                                             FLG_IS_EHFRAME;
2789                                 }
2790                                 break;
2791 #endif
2792                         default:
2793                         do_default:
2794                                 if (process_section(name, ifl, shdr, scn, ndx,
2795                                     ((ident == ld_targ.t_id.id_null) ?
2796                                     ident : ld_targ.t_id.id_user), ofl) ==
2797                                     S_ERROR)
2798                                         return (S_ERROR);
2799                                 break;
2800                         }
2801                 }
2802         }
2803 
2804         /*
2805          * Now that all input sections have been analyzed, and prior to placing
2806          * any input sections to their output sections, process any groups.
2807          * Groups can contribute COMDAT items, which may get discarded as part
2808          * of placement.  In addition, COMDAT names may require transformation
2809          * to indicate different output section placement.
2810          */
2811         if (ifl->ifl_flags & FLG_IF_GROUPS) {
2812                 for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
2813                         Is_desc *isp;
2814 
2815                         if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
2816                             (isp->is_shdr->sh_type != SHT_GROUP))
2817                                 continue;
2818 
2819                         if (ld_group_process(isp, ofl) == S_ERROR)
2820                                 return (S_ERROR);
2821                 }
2822         }
2823 
2824         /*
2825          * Now group information has been processed, we can safely validate
2826          * that nothing is fishy about the section COMDAT description.  We
2827          * need to do this prior to placing the section (where any
2828          * SHT_SUNW_COMDAT sections will be restored to being PROGBITS)
2829          */
2830         ld_comdat_validate(ofl, ifl);
2831 
2832         /*
2833          * Now that all of the input sections have been processed, place
2834          * them in the appropriate output sections.
2835          */
2836         for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
2837                 Is_desc *isp;
2838 
2839                 if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
2840                     ((isp->is_flags & FLG_IS_PLACE) == 0))
2841                         continue;
2842 
2843                 /*
2844                  * Place all non-ordered sections within their appropriate
2845                  * output section.
2846                  */
2847                 if ((isp->is_flags & FLG_IS_ORDERED) == 0) {
2848                         if (ld_place_section(ofl, isp, path_info,
2849                             isp->is_keyident, NULL) == (Os_desc *)S_ERROR)
2850                                 return (S_ERROR);
2851                         continue;
2852                 }
2853 
2854                 /*
2855                  * Count the number of ordered sections and retain the first
2856                  * ordered section index. This will be used to optimize the
2857                  * ordered section loop that immediately follows this one.
2858                  */
2859                 ordcnt++;
2860                 if (ordndx == 0)
2861                         ordndx = ndx;
2862         }
2863 
2864         /*
2865          * Having placed all the non-ordered sections, it is now
2866          * safe to place SHF_ORDERED/SHF_LINK_ORDER sections.
2867          */
2868         if (ifl->ifl_flags & FLG_IF_ORDERED) {
2869                 for (ndx = ordndx; ndx < ifl->ifl_shnum; ndx++) {
2870                         Is_desc *isp;
2871 
2872                         if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
2873                             ((isp->is_flags &
2874                             (FLG_IS_PLACE | FLG_IS_ORDERED)) !=
2875                             (FLG_IS_PLACE | FLG_IS_ORDERED)))
2876                                 continue;
2877 
2878                         /* ld_process_ordered() calls ld_place_section() */
2879                         if (ld_process_ordered(ofl, ifl, path_info, ndx) ==
2880                             S_ERROR)
2881                                 return (S_ERROR);
2882 
2883                         /* If we've done them all, stop searching */
2884                         if (--ordcnt == 0)
2885                                 break;
2886                 }
2887         }
2888 
2889         /*
2890          * If this is a shared object explicitly specified on the command
2891          * line (as opposed to being a dependency of such an object),
2892          * determine if the user has specified a control definition. This
2893          * descriptor may specify which version definitions can be used
2894          * from this object. It may also update the dependency to USED and
2895          * supply an alternative SONAME.
2896          */
2897         sdf = NULL;
2898         if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) {
2899                 const char      *base;
2900 
2901                 /*
2902                  * Use the basename of the input file (typically this is the
2903                  * compilation environment name, ie. libfoo.so).
2904                  */
2905                 if ((base = strrchr(ifl->ifl_name, '/')) == NULL)
2906                         base = ifl->ifl_name;
2907                 else
2908                         base++;
2909 
2910                 if ((sdf = sdf_find(base, ofl->ofl_socntl)) != NULL) {
2911                         sdf->sdf_file = ifl;
2912                         ifl->ifl_sdfdesc = sdf;
2913                 }
2914         }
2915 
2916         /*
2917          * Before symbol processing, process any capabilities.  Capabilities
2918          * can reference a string table, which is why this processing is
2919          * carried out after the initial section processing.  Capabilities,
2920          * together with -z symbolcap, can require the conversion of global
2921          * symbols to local symbols.
2922          */
2923         if (capisp && (process_cap(ofl, ifl, capisp) == S_ERROR))
2924                 return (S_ERROR);
2925 
2926         /*
2927          * Process any version dependencies.  These will establish shared object
2928          * `needed' entries in the same manner as will be generated from the
2929          * .dynamic's NEEDED entries.
2930          */
2931         if (vndisp && ((ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) ||
2932             OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS)))
2933                 if (ld_vers_need_process(vndisp, ifl, ofl) == S_ERROR)
2934                         return (S_ERROR);
2935 
2936         /*
2937          * Before processing any symbol resolution or relocations process any
2938          * version sections.
2939          */
2940         if (vsyisp)
2941                 (void) ld_vers_sym_process(ofl, vsyisp, ifl);
2942 
2943         if (ifl->ifl_versym &&
2944             (vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT))))
2945                 if (ld_vers_def_process(vdfisp, ifl, ofl) == S_ERROR)
2946                         return (S_ERROR);
2947 
2948         /*
2949          * Having collected the appropriate sections carry out any additional
2950          * processing if necessary.
2951          */
2952         for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) {
2953                 Is_desc *isp;
2954 
2955                 if ((isp = ifl->ifl_isdesc[ndx]) == NULL)
2956                         continue;
2957                 row = isp->is_shdr->sh_type;
2958 
2959                 if ((isp->is_flags & FLG_IS_DISCARD) == 0)
2960                         ld_sup_section(ofl, isp->is_name, isp->is_shdr, ndx,
2961                             isp->is_indata, elf);
2962 
2963                 /*
2964                  * If this is a SHT_SUNW_move section from a relocatable file,
2965                  * keep track of the section for later processing.
2966                  */
2967                 if ((row == SHT_SUNW_move) && (column == 0)) {
2968                         if (aplist_append(&(ofl->ofl_ismove), isp,
2969                             AL_CNT_OFL_MOVE) == NULL)
2970                                 return (S_ERROR);
2971                 }
2972 
2973                 /*
2974                  * If this is a standard section type process it via the
2975                  * appropriate action routine.
2976                  */
2977                 if (row < SHT_NUM) {
2978                         if (Final[row][column] != NULL) {
2979                                 if (Final[row][column](isp, ifl,
2980                                     ofl) == S_ERROR)
2981                                         return (S_ERROR);
2982                         }
2983 #if     defined(_ELF64)
2984                 } else if ((row == SHT_AMD64_UNWIND) && (column == 0)) {
2985                         Os_desc *osp = isp->is_osdesc;
2986 
2987                         /*
2988                          * SHT_AMD64_UNWIND (0x70000001) is in the SHT_LOPROC -
2989                          * SHT_HIPROC range reserved for processor-specific
2990                          * semantics, and is only meaningful for amd64 targets.
2991                          *
2992                          * Only process unwind contents from relocatable
2993                          * objects.
2994                          */
2995                         if (osp && (ld_targ.t_m.m_mach == EM_AMD64) &&
2996                             (ld_unwind_register(osp, ofl) == S_ERROR))
2997                                 return (S_ERROR);
2998 #endif
2999                 }
3000         }
3001 
3002         /*
3003          * Following symbol processing, if this relocatable object input file
3004          * provides symbol capabilities, tag the associated symbols so that
3005          * the symbols can be re-assigned to the new capabilities symbol
3006          * section that will be created for the output file.
3007          */
3008         if (capinfoisp && (ifl->ifl_ehdr->e_type == ET_REL) &&
3009             (process_capinfo(ofl, ifl, capinfoisp) == S_ERROR))
3010                 return (S_ERROR);
3011 
3012         /*
3013          * After processing any symbol resolution, and if this dependency
3014          * indicates it contains symbols that can't be directly bound to,
3015          * set the symbols appropriately.
3016          */
3017         if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) ==
3018             (FLG_IF_NEEDED | FLG_IF_NODIRECT)))
3019                 (void) ld_sym_nodirect(sifisp, ifl, ofl);
3020 
3021         return (1);
3022 }
3023 
3024 /*
3025  * Process the current input file.  There are basically three types of files
3026  * that come through here:
3027  *
3028  *  -   files explicitly defined on the command line (ie. foo.o or bar.so),
3029  *      in this case only the `name' field is valid.
3030  *
3031  *  -   libraries determined from the -l command line option (ie. -lbar),
3032  *      in this case the `soname' field contains the basename of the located
3033  *      file.
3034  *
3035  * Any shared object specified via the above two conventions must be recorded
3036  * as a needed dependency.
3037  *
3038  *  -   libraries specified as dependencies of those libraries already obtained
3039  *      via the command line (ie. bar.so has a DT_NEEDED entry of fred.so.1),
3040  *      in this case the `soname' field contains either a full pathname (if the
3041  *      needed entry contained a `/'), or the basename of the located file.
3042  *      These libraries are processed to verify symbol binding but are not
3043  *      recorded as dependencies of the output file being generated.
3044  *
3045  * entry:
3046  *      name - File name
3047  *      soname - SONAME for needed sharable library, as described above
3048  *      fd - Open file descriptor
3049  *      elf - Open ELF handle
3050  *      flags - FLG_IF_ flags applicable to file
3051  *      ofl - Output file descriptor
3052  *      rej - Rejection descriptor used to record rejection reason
3053  *      ifl_ret - NULL, or address of pointer to receive reference to
3054  *              resulting input descriptor for file. If ifl_ret is non-NULL,
3055  *              the file cannot be an archive or it will be rejected.
3056  *
3057  * exit:
3058  *      If a error occurs in examining the file, S_ERROR is returned.
3059  *      If the file can be examined, but is not suitable, *rej is updated,
3060  *      and 0 is returned. If the file is acceptable, 1 is returned, and if
3061  *      ifl_ret is non-NULL, *ifl_ret is set to contain the pointer to the
3062  *      resulting input descriptor.
3063  */
3064 uintptr_t
3065 ld_process_ifl(const char *name, const char *soname, int fd, Elf *elf,
3066     Word flags, Ofl_desc *ofl, Rej_desc *rej, Ifl_desc **ifl_ret)
3067 {
3068         Ifl_desc        *ifl;
3069         Ehdr            *ehdr;
3070         uintptr_t       error = 0;
3071         struct stat     status;
3072         Ar_desc         *adp;
3073         Rej_desc        _rej;
3074 
3075         /*
3076          * If this file was not extracted from an archive obtain its device
3077          * information.  This will be used to determine if the file has already
3078          * been processed (rather than simply comparing filenames, the device
3079          * information provides a quicker comparison and detects linked files).
3080          */
3081         if (fd && ((flags & FLG_IF_EXTRACT) == 0))
3082                 (void) fstat(fd, &status);
3083         else {
3084                 status.st_dev = 0;
3085                 status.st_ino = 0;
3086         }
3087 
3088         switch (elf_kind(elf)) {
3089         case ELF_K_AR:
3090                 /*
3091                  * If the caller has supplied a non-NULL ifl_ret, then
3092                  * we cannot process archives, for there will be no
3093                  * input file descriptor for us to return. In this case,
3094                  * reject the attempt.
3095                  */
3096                 if (ifl_ret != NULL) {
3097                         _rej.rej_type = SGS_REJ_ARCHIVE;
3098                         _rej.rej_name = name;
3099                         DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
3100                             ld_targ.t_m.m_mach));
3101                         if (rej->rej_type == 0) {
3102                                 *rej = _rej;
3103                                 rej->rej_name = strdup(_rej.rej_name);
3104                         }
3105                         return (0);
3106                 }
3107 
3108                 /*
3109                  * Determine if we've already come across this archive file.
3110                  */
3111                 if (!(flags & FLG_IF_EXTRACT)) {
3112                         Aliste  idx;
3113 
3114                         for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) {
3115                                 if ((adp->ad_stdev != status.st_dev) ||
3116                                     (adp->ad_stino != status.st_ino))
3117                                         continue;
3118 
3119                                 /*
3120                                  * We've seen this file before so reuse the
3121                                  * original archive descriptor and discard the
3122                                  * new elf descriptor.  Note that a file
3123                                  * descriptor is unnecessary, as the file is
3124                                  * already available in memory.
3125                                  */
3126                                 DBG_CALL(Dbg_file_reuse(ofl->ofl_lml, name,
3127                                     adp->ad_name));
3128                                 (void) elf_end(elf);
3129                                 if (!ld_process_archive(name, -1, adp, ofl))
3130                                         return (S_ERROR);
3131                                 return (1);
3132                         }
3133                 }
3134 
3135                 /*
3136                  * As we haven't processed this file before establish a new
3137                  * archive descriptor.
3138                  */
3139                 adp = ld_ar_setup(name, elf, ofl);
3140                 if ((adp == NULL) || (adp == (Ar_desc *)S_ERROR))
3141                         return ((uintptr_t)adp);
3142                 adp->ad_stdev = status.st_dev;
3143                 adp->ad_stino = status.st_ino;
3144 
3145                 ld_sup_file(ofl, name, ELF_K_AR, flags, elf);
3146 
3147                 /*
3148                  * Indicate that the ELF descriptor no longer requires a file
3149                  * descriptor by reading the entire file.  The file is already
3150                  * read via the initial mmap(2) behind elf_begin(3elf), thus
3151                  * this operation is effectively a no-op.  However, a side-
3152                  * effect is that the internal file descriptor, maintained in
3153                  * the ELF descriptor, is set to -1.  This setting will not
3154                  * be compared with any file descriptor that is passed to
3155                  * elf_begin(), should this archive, or one of the archive
3156                  * members, be processed again from the command line or
3157                  * because of a -z rescan.
3158                  */
3159                 if (elf_cntl(elf, ELF_C_FDREAD) == -1) {
3160                         ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_CNTL),
3161                             name);
3162                         return (0);
3163                 }
3164 
3165                 if (!ld_process_archive(name, -1, adp, ofl))
3166                         return (S_ERROR);
3167                 return (1);
3168 
3169         case ELF_K_ELF:
3170                 /*
3171                  * Obtain the elf header so that we can determine what type of
3172                  * elf ELF_K_ELF file this is.
3173                  */
3174                 if ((ehdr = elf_getehdr(elf)) == NULL) {
3175                         int     _class = gelf_getclass(elf);
3176 
3177                         /*
3178                          * This can fail for a number of reasons. Typically
3179                          * the object class is incorrect (ie. user is building
3180                          * 64-bit but managed to point at 32-bit libraries).
3181                          * Other ELF errors can include a truncated or corrupt
3182                          * file. Try to get the best error message possible.
3183                          */
3184                         if (ld_targ.t_m.m_class != _class) {
3185                                 _rej.rej_type = SGS_REJ_CLASS;
3186                                 _rej.rej_info = (uint_t)_class;
3187                         } else {
3188                                 _rej.rej_type = SGS_REJ_STR;
3189                                 _rej.rej_str = elf_errmsg(-1);
3190                         }
3191                         _rej.rej_name = name;
3192                         DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
3193                             ld_targ.t_m.m_mach));
3194                         if (rej->rej_type == 0) {
3195                                 *rej = _rej;
3196                                 rej->rej_name = strdup(_rej.rej_name);
3197                         }
3198                         return (0);
3199                 }
3200 
3201                 /*
3202                  * Determine if we've already come across this file.
3203                  */
3204                 if (!(flags & FLG_IF_EXTRACT)) {
3205                         APlist  *apl;
3206                         Aliste  idx;
3207 
3208                         if (ehdr->e_type == ET_REL)
3209                                 apl = ofl->ofl_objs;
3210                         else
3211                                 apl = ofl->ofl_sos;
3212 
3213                         /*
3214                          * Traverse the appropriate file list and determine if
3215                          * a dev/inode match is found.
3216                          */
3217                         for (APLIST_TRAVERSE(apl, idx, ifl)) {
3218                                 /*
3219                                  * Ifl_desc generated via -Nneed, therefore no
3220                                  * actual file behind it.
3221                                  */
3222                                 if (ifl->ifl_flags & FLG_IF_NEEDSTR)
3223                                         continue;
3224 
3225                                 if ((ifl->ifl_stino != status.st_ino) ||
3226                                     (ifl->ifl_stdev != status.st_dev))
3227                                         continue;
3228 
3229                                 /*
3230                                  * Disregard (skip) this image.
3231                                  */
3232                                 DBG_CALL(Dbg_file_skip(ofl->ofl_lml,
3233                                     ifl->ifl_name, name));
3234                                 (void) elf_end(elf);
3235 
3236                                 /*
3237                                  * If the file was explicitly defined on the
3238                                  * command line (this is always the case for
3239                                  * relocatable objects, and is true for shared
3240                                  * objects when they weren't specified via -l or
3241                                  * were dragged in as an implicit dependency),
3242                                  * then warn the user.
3243                                  */
3244                                 if (((flags & FLG_IF_CMDLINE) ||
3245                                     (ifl->ifl_flags & FLG_IF_CMDLINE)) &&
3246                                     !(ofl->ofl_flags & FLG_OF_MULINCL)) {
3247                                         const char      *errmsg;
3248 
3249                                         /*
3250                                          * Determine whether this is the same
3251                                          * file name as originally encountered
3252                                          * so as to provide the most
3253                                          * descriptive diagnostic.
3254                                          */
3255                                         errmsg =
3256                                             (strcmp(name, ifl->ifl_name) == 0) ?
3257                                             MSG_INTL(MSG_FIL_MULINC_1) :
3258                                             MSG_INTL(MSG_FIL_MULINC_2);
3259                                         ld_eprintf(ofl, ERR_WARNING,
3260                                             errmsg, name, ifl->ifl_name);
3261                                 }
3262                                 if (ifl_ret)
3263                                         *ifl_ret = ifl;
3264                                 return (1);
3265                         }
3266                 }
3267 
3268                 /*
3269                  * At this point, we know we need the file.  Establish an input
3270                  * file descriptor and continue processing.
3271                  */
3272                 ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej);
3273                 if ((ifl == NULL) || (ifl == (Ifl_desc *)S_ERROR))
3274                         return ((uintptr_t)ifl);
3275                 ifl->ifl_stdev = status.st_dev;
3276                 ifl->ifl_stino = status.st_ino;
3277 
3278                 /*
3279                  * If -zignore is in effect, mark this file as a potential
3280                  * candidate (the files use isn't actually determined until
3281                  * symbol resolution and relocation processing are completed).
3282                  */
3283                 if (ofl->ofl_flags1 & FLG_OF1_IGNORE)
3284                         ifl->ifl_flags |= FLG_IF_IGNORE;
3285 
3286                 switch (ehdr->e_type) {
3287                 case ET_REL:
3288                         (*ld_targ.t_mr.mr_mach_eflags)(ehdr, ofl);
3289                         error = process_elf(ifl, elf, ofl);
3290                         break;
3291                 case ET_DYN:
3292                         if ((ofl->ofl_flags & FLG_OF_STATIC) ||
3293                             !(ofl->ofl_flags & FLG_OF_DYNLIBS)) {
3294                                 ld_eprintf(ofl, ERR_FATAL,
3295                                     MSG_INTL(MSG_FIL_SOINSTAT), name);
3296                                 return (0);
3297                         }
3298 
3299                         /*
3300                          * Record any additional shared object information.
3301                          * If no soname is specified (eg. this file was
3302                          * derived from a explicit filename declaration on the
3303                          * command line, ie. bar.so) use the pathname.
3304                          * This entry may be overridden if the files dynamic
3305                          * section specifies an DT_SONAME value.
3306                          */
3307                         if (soname == NULL)
3308                                 ifl->ifl_soname = ifl->ifl_name;
3309                         else
3310                                 ifl->ifl_soname = soname;
3311 
3312                         /*
3313                          * If direct bindings, lazy loading, group permissions,
3314                          * or deferred dependencies need to be established, mark
3315                          * this object.
3316                          */
3317                         if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT)
3318                                 ifl->ifl_flags |= FLG_IF_DIRECT;
3319                         if (ofl->ofl_flags1 & FLG_OF1_LAZYLD)
3320                                 ifl->ifl_flags |= FLG_IF_LAZYLD;
3321                         if (ofl->ofl_flags1 & FLG_OF1_GRPPRM)
3322                                 ifl->ifl_flags |= FLG_IF_GRPPRM;
3323                         if (ofl->ofl_flags1 & FLG_OF1_DEFERRED)
3324                                 ifl->ifl_flags |=
3325                                     (FLG_IF_LAZYLD | FLG_IF_DEFERRED);
3326 
3327                         error = process_elf(ifl, elf, ofl);
3328 
3329                         /*
3330                          * Determine whether this dependency requires a syminfo.
3331                          */
3332                         if (ifl->ifl_flags & MSK_IF_SYMINFO)
3333                                 ofl->ofl_flags |= FLG_OF_SYMINFO;
3334 
3335                         /*
3336                          * Guidance: Use -z lazyload/nolazyload.
3337                          * libc is exempt from this advice, because it cannot
3338                          * be lazy loaded, and requests to do so are ignored.
3339                          */
3340                         if (OFL_GUIDANCE(ofl, FLG_OFG_NO_LAZY) &&
3341                             ((ifl->ifl_flags & FLG_IF_RTLDINF) == 0)) {
3342                                 ld_eprintf(ofl, ERR_GUIDANCE,
3343                                     MSG_INTL(MSG_GUIDE_LAZYLOAD));
3344                                 ofl->ofl_guideflags |= FLG_OFG_NO_LAZY;
3345                         }
3346 
3347                         /*
3348                          * Guidance: Use -B direct/nodirect or
3349                          * -z direct/nodirect.
3350                          */
3351                         if (OFL_GUIDANCE(ofl, FLG_OFG_NO_DB)) {
3352                                 ld_eprintf(ofl, ERR_GUIDANCE,
3353                                     MSG_INTL(MSG_GUIDE_DIRECT));
3354                                 ofl->ofl_guideflags |= FLG_OFG_NO_DB;
3355                         }
3356 
3357                         break;
3358                 default:
3359                         (void) elf_errno();
3360                         _rej.rej_type = SGS_REJ_UNKFILE;
3361                         _rej.rej_name = name;
3362                         DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
3363                             ld_targ.t_m.m_mach));
3364                         if (rej->rej_type == 0) {
3365                                 *rej = _rej;
3366                                 rej->rej_name = strdup(_rej.rej_name);
3367                         }
3368                         return (0);
3369                 }
3370                 break;
3371         default:
3372                 (void) elf_errno();
3373                 _rej.rej_type = SGS_REJ_UNKFILE;
3374                 _rej.rej_name = name;
3375                 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
3376                     ld_targ.t_m.m_mach));
3377                 if (rej->rej_type == 0) {
3378                         *rej = _rej;
3379                         rej->rej_name = strdup(_rej.rej_name);
3380                 }
3381                 return (0);
3382         }
3383         if ((error == 0) || (error == S_ERROR))
3384                 return (error);
3385 
3386         if (ifl_ret)
3387                 *ifl_ret = ifl;
3388         return (1);
3389 }
3390 
3391 /*
3392  * Having successfully opened a file, set up the necessary elf structures to
3393  * process it further.  This small section of processing is slightly different
3394  * from the elf initialization required to process a relocatable object from an
3395  * archive (see libs.c: ld_process_archive()).
3396  */
3397 uintptr_t
3398 ld_process_open(const char *opath, const char *ofile, int *fd, Ofl_desc *ofl,
3399     Word flags, Rej_desc *rej, Ifl_desc **ifl_ret)
3400 {
3401         Elf             *elf;
3402         const char      *npath = opath;
3403         const char      *nfile = ofile;
3404 
3405         if ((elf = elf_begin(*fd, ELF_C_READ, NULL)) == NULL) {
3406                 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), npath);
3407                 return (0);
3408         }
3409 
3410         /*
3411          * Determine whether the support library wishes to process this open.
3412          * The support library may return:
3413          *   .  a different ELF descriptor (in which case they should have
3414          *      closed the original)
3415          *   .  a different file descriptor (in which case they should have
3416          *      closed the original)
3417          *   .  a different path and file name (presumably associated with
3418          *      a different file descriptor)
3419          *
3420          * A file descriptor of -1, or and ELF descriptor of zero indicates
3421          * the file should be ignored.
3422          */
3423         ld_sup_open(ofl, &npath, &nfile, fd, flags, &elf, NULL, 0,
3424             elf_kind(elf));
3425 
3426         if ((*fd == -1) || (elf == NULL))
3427                 return (0);
3428 
3429         return (ld_process_ifl(npath, nfile, *fd, elf, flags, ofl, rej,
3430             ifl_ret));
3431 }
3432 
3433 /*
3434  * Having successfully mapped a file, set up the necessary elf structures to
3435  * process it further.  This routine is patterned after ld_process_open() and
3436  * is only called by ld.so.1(1) to process a relocatable object.
3437  */
3438 Ifl_desc *
3439 ld_process_mem(const char *path, const char *file, char *addr, size_t size,
3440     Ofl_desc *ofl, Rej_desc *rej)
3441 {
3442         Elf             *elf;
3443         uintptr_t       open_ret;
3444         Ifl_desc        *ifl;
3445 
3446         if ((elf = elf_memory(addr, size)) == NULL) {
3447                 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_MEMORY), path);
3448                 return (0);
3449         }
3450 
3451         open_ret = ld_process_ifl(path, file, 0, elf, 0, ofl, rej, &ifl);
3452         if (open_ret != 1)
3453                 return ((Ifl_desc *) open_ret);
3454         return (ifl);
3455 }
3456 
3457 /*
3458  * Process a required library (i.e. the dependency of a shared object).
3459  * Combine the directory and filename, check the resultant path size, and try
3460  * opening the pathname.
3461  */
3462 static Ifl_desc *
3463 process_req_lib(Sdf_desc *sdf, const char *dir, const char *file,
3464     Ofl_desc *ofl, Rej_desc *rej)
3465 {
3466         size_t          dlen, plen;
3467         int             fd;
3468         char            path[PATH_MAX];
3469         const char      *_dir = dir;
3470 
3471         /*
3472          * Determine the sizes of the directory and filename to insure we don't
3473          * exceed our buffer.
3474          */
3475         if ((dlen = strlen(dir)) == 0) {
3476                 _dir = MSG_ORIG(MSG_STR_DOT);
3477                 dlen = 1;
3478         }
3479         dlen++;
3480         plen = dlen + strlen(file) + 1;
3481         if (plen > PATH_MAX) {
3482                 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG),
3483                     _dir, file);
3484                 return (0);
3485         }
3486 
3487         /*
3488          * Build the entire pathname and try and open the file.
3489          */
3490         (void) strcpy(path, _dir);
3491         (void) strcat(path, MSG_ORIG(MSG_STR_SLASH));
3492         (void) strcat(path, file);
3493         DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
3494             sdf->sdf_rfile, path));
3495 
3496         if ((fd = open(path, O_RDONLY)) == -1)
3497                 return (0);
3498         else {
3499                 uintptr_t       open_ret;
3500                 Ifl_desc        *ifl;
3501                 char            *_path;
3502 
3503                 if ((_path = libld_malloc(strlen(path) + 1)) == NULL)
3504                         return ((Ifl_desc *)S_ERROR);
3505                 (void) strcpy(_path, path);
3506                 open_ret = ld_process_open(_path, &_path[dlen], &fd, ofl,
3507                     0, rej, &ifl);
3508                 if (fd != -1)
3509                         (void) close(fd);
3510                 if (open_ret != 1)
3511                         return ((Ifl_desc *)open_ret);
3512                 return (ifl);
3513         }
3514 }
3515 
3516 /*
3517  * Finish any library processing.  Walk the list of so's that have been listed
3518  * as "included" by shared objects we have previously processed.  Examine them,
3519  * without adding them as explicit dependents of this program, in order to
3520  * complete our symbol definition process.  The search path rules are:
3521  *
3522  *  -   use any user supplied paths, i.e. LD_LIBRARY_PATH and -L, then
3523  *
3524  *  -   use any RPATH defined within the parent shared object, then
3525  *
3526  *  -   use the default directories, i.e. LIBPATH or -YP.
3527  */
3528 uintptr_t
3529 ld_finish_libs(Ofl_desc *ofl)
3530 {
3531         Aliste          idx1;
3532         Sdf_desc        *sdf;
3533         Rej_desc        rej = { 0 };
3534 
3535         /*
3536          * Make sure we are back in dynamic mode.
3537          */
3538         ofl->ofl_flags |= FLG_OF_DYNLIBS;
3539 
3540         for (APLIST_TRAVERSE(ofl->ofl_soneed, idx1, sdf)) {
3541                 Aliste          idx2;
3542                 char            *path, *slash = NULL;
3543                 int             fd;
3544                 Ifl_desc        *ifl;
3545                 char            *file = (char *)sdf->sdf_name;
3546 
3547                 /*
3548                  * See if this file has already been processed.  At the time
3549                  * this implicit dependency was determined there may still have
3550                  * been more explicit dependencies to process.  Note, if we ever
3551                  * do parse the command line three times we would be able to
3552                  * do all this checking when processing the dynamic section.
3553                  */
3554                 if (sdf->sdf_file)
3555                         continue;
3556 
3557                 for (APLIST_TRAVERSE(ofl->ofl_sos, idx2, ifl)) {
3558                         if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) &&
3559                             (strcmp(file, ifl->ifl_soname) == 0)) {
3560                                 sdf->sdf_file = ifl;
3561                                 break;
3562                         }
3563                 }
3564                 if (sdf->sdf_file)
3565                         continue;
3566 
3567                 /*
3568                  * If the current path name element embeds a "/", then it's to
3569                  * be taken "as is", with no searching involved.  Process all
3570                  * "/" occurrences, so that we can deduce the base file name.
3571                  */
3572                 for (path = file; *path; path++) {
3573                         if (*path == '/')
3574                                 slash = path;
3575                 }
3576                 if (slash) {
3577                         DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
3578                             sdf->sdf_rfile, file));
3579                         if ((fd = open(file, O_RDONLY)) == -1) {
3580                                 ld_eprintf(ofl, ERR_WARNING,
3581                                     MSG_INTL(MSG_FIL_NOTFOUND), file,
3582                                     sdf->sdf_rfile);
3583                         } else {
3584                                 uintptr_t       open_ret;
3585                                 Rej_desc        _rej = { 0 };
3586 
3587                                 open_ret = ld_process_open(file, ++slash,
3588                                     &fd, ofl, 0, &_rej, &ifl);
3589                                 if (fd != -1)
3590                                         (void) close(fd);
3591                                 if (open_ret == S_ERROR)
3592                                         return (S_ERROR);
3593 
3594                                 if (_rej.rej_type) {
3595                                         Conv_reject_desc_buf_t rej_buf;
3596 
3597                                         ld_eprintf(ofl, ERR_WARNING,
3598                                             MSG_INTL(reject[_rej.rej_type]),
3599                                             _rej.rej_name ? rej.rej_name :
3600                                             MSG_INTL(MSG_STR_UNKNOWN),
3601                                             conv_reject_desc(&_rej, &rej_buf,
3602                                             ld_targ.t_m.m_mach));
3603                                 } else
3604                                         sdf->sdf_file = ifl;
3605                         }
3606                         continue;
3607                 }
3608 
3609                 /*
3610                  * Now search for this file in any user defined directories.
3611                  */
3612                 for (APLIST_TRAVERSE(ofl->ofl_ulibdirs, idx2, path)) {
3613                         Rej_desc        _rej = { 0 };
3614 
3615                         ifl = process_req_lib(sdf, path, file, ofl, &_rej);
3616                         if (ifl == (Ifl_desc *)S_ERROR) {
3617                                 return (S_ERROR);
3618                         }
3619                         if (_rej.rej_type) {
3620                                 if (rej.rej_type == 0) {
3621                                         rej = _rej;
3622                                         rej.rej_name = strdup(_rej.rej_name);
3623                                 }
3624                         }
3625                         if (ifl) {
3626                                 sdf->sdf_file = ifl;
3627                                 break;
3628                         }
3629                 }
3630                 if (sdf->sdf_file)
3631                         continue;
3632 
3633                 /*
3634                  * Next use the local rules defined within the parent shared
3635                  * object.
3636                  */
3637                 if (sdf->sdf_rpath != NULL) {
3638                         char    *rpath, *next;
3639 
3640                         rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1);
3641                         if (rpath == NULL)
3642                                 return (S_ERROR);
3643                         (void) strcpy(rpath, sdf->sdf_rpath);
3644                         DBG_CALL(Dbg_libs_path(ofl->ofl_lml, rpath,
3645                             LA_SER_RUNPATH, sdf->sdf_rfile));
3646                         if ((path = strtok_r(rpath,
3647                             MSG_ORIG(MSG_STR_COLON), &next)) != NULL) {
3648                                 do {
3649                                         Rej_desc        _rej = { 0 };
3650 
3651                                         path = expand(sdf->sdf_rfile, path,
3652                                             &next);
3653 
3654                                         ifl = process_req_lib(sdf, path,
3655                                             file, ofl, &_rej);
3656                                         if (ifl == (Ifl_desc *)S_ERROR) {
3657                                                 return (S_ERROR);
3658                                         }
3659                                         if ((_rej.rej_type) &&
3660                                             (rej.rej_type == 0)) {
3661                                                 rej = _rej;
3662                                                 rej.rej_name =
3663                                                     strdup(_rej.rej_name);
3664                                         }
3665                                         if (ifl) {
3666                                                 sdf->sdf_file = ifl;
3667                                                 break;
3668                                         }
3669                                 } while ((path = strtok_r(NULL,
3670                                     MSG_ORIG(MSG_STR_COLON), &next)) != NULL);
3671                         }
3672                 }
3673                 if (sdf->sdf_file)
3674                         continue;
3675 
3676                 /*
3677                  * Finally try the default library search directories.
3678                  */
3679                 for (APLIST_TRAVERSE(ofl->ofl_dlibdirs, idx2, path)) {
3680                         Rej_desc        _rej = { 0 };
3681 
3682                         ifl = process_req_lib(sdf, path, file, ofl, &rej);
3683                         if (ifl == (Ifl_desc *)S_ERROR) {
3684                                 return (S_ERROR);
3685                         }
3686                         if (_rej.rej_type) {
3687                                 if (rej.rej_type == 0) {
3688                                         rej = _rej;
3689                                         rej.rej_name = strdup(_rej.rej_name);
3690                                 }
3691                         }
3692                         if (ifl) {
3693                                 sdf->sdf_file = ifl;
3694                                 break;
3695                         }
3696                 }
3697                 if (sdf->sdf_file)
3698                         continue;
3699 
3700                 /*
3701                  * If we've got this far we haven't found the shared object.
3702                  * If an object was found, but was rejected for some reason,
3703                  * print a diagnostic to that effect, otherwise generate a
3704                  * generic "not found" diagnostic.
3705                  */
3706                 if (rej.rej_type) {
3707                         Conv_reject_desc_buf_t rej_buf;
3708 
3709                         ld_eprintf(ofl, ERR_WARNING,
3710                             MSG_INTL(reject[rej.rej_type]),
3711                             rej.rej_name ? rej.rej_name :
3712                             MSG_INTL(MSG_STR_UNKNOWN),
3713                             conv_reject_desc(&rej, &rej_buf,
3714                             ld_targ.t_m.m_mach));
3715                 } else {
3716                         ld_eprintf(ofl, ERR_WARNING,
3717                             MSG_INTL(MSG_FIL_NOTFOUND), file, sdf->sdf_rfile);
3718                 }
3719         }
3720 
3721         /*
3722          * Finally, now that all objects have been input, make sure any version
3723          * requirements have been met.
3724          */
3725         return (ld_vers_verify(ofl));
3726 }