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) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
  24  */
  25 
  26 /*
  27  * Object file dependent suport for ELF objects.
  28  */
  29 
  30 #include        <sys/mman.h>
  31 #include        <stdio.h>
  32 #include        <unistd.h>
  33 #include        <libelf.h>
  34 #include        <string.h>
  35 #include        <dlfcn.h>
  36 #include        <debug.h>
  37 #include        <libld.h>
  38 #include        "_rtld.h"
  39 #include        "_audit.h"
  40 #include        "_elf.h"
  41 
  42 static Rt_map   *olmp = NULL;
  43 static Alist    *mpalp = NULL;
  44 
  45 static Ehdr     dehdr = { { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
  46                             M_CLASS, M_DATA }, 0, M_MACH, EV_CURRENT };
  47 
  48 /*
  49  * Process a relocatable object.  The static object link map pointer is used as
  50  * a flag to determine whether a concatenation is already in progress (ie. an
  51  * LD_PRELOAD may specify a list of objects).  The link map returned simply
  52  * specifies an `object' flag which the caller can interpret and thus call
  53  * elf_obj_fini() to complete the concatenation.
  54  */
  55 static Rt_map *
  56 elf_obj_init(Lm_list *lml, Aliste lmco, const char *oname)
  57 {
  58         Ofl_desc        *ofl;
  59         const char      *name;
  60         size_t          lmsz;
  61 
  62         /*
  63          * Allocate the name of this object, as the original name may be
  64          * associated with a data buffer that can be reused to load the
  65          * dependencies needed to processes this object.
  66          */
  67         if ((name = stravl_insert(oname, 0, 0, 0)) == NULL)
  68                 return (NULL);
  69 
  70         /*
  71          * Initialize an output file descriptor and the entrance criteria.
  72          */
  73         if ((ofl = calloc(1, sizeof (Ofl_desc))) == NULL)
  74                 return (NULL);
  75 
  76         ofl->ofl_dehdr = &dehdr;
  77 
  78         ofl->ofl_flags = (FLG_OF_DYNAMIC | FLG_OF_SHAROBJ | FLG_OF_STRIP);
  79         ofl->ofl_flags1 = (FLG_OF1_RELDYN | FLG_OF1_TEXTOFF | FLG_OF1_MEMORY);
  80         ofl->ofl_lml = lml;
  81 
  82         /*
  83          * As ent_setup() will effectively lazy load the necessary support
  84          * libraries, make sure ld.so.1 is initialized for plt relocations.
  85          * Then configure libld.so to process objects of the desired target
  86          * type (this is the first call to libld.so, which will effectively
  87          * lazyload it).
  88          */
  89         if ((elf_rtld_load() == 0) || (ld_init_target(lml, M_MACH) != 0)) {
  90                 free(ofl);
  91                 return (NULL);
  92         }
  93 
  94         /*
  95          * Obtain a generic set of entrance criteria, and generate a link map
  96          * place holder and use the ELFPRV() element to maintain the output
  97          * file descriptor.
  98          */
  99         if ((ld_ent_setup(ofl, syspagsz) == S_ERROR) ||
 100             ((olmp = calloc(1, sizeof (Rt_map))) == NULL)) {
 101                 free(ofl);
 102                 return (NULL);
 103         }
 104         DBG_CALL(Dbg_file_elf(lml, name, 0, 0, lml->lm_lmidstr, lmco));
 105         FLAGS(olmp) |= FLG_RT_OBJECT;
 106         ELFPRV(olmp) = (void *)ofl;
 107 
 108         /*
 109          * Initialize string tables.
 110          */
 111         if (ld_init_strings(ofl) == S_ERROR) {
 112                 free(ofl);
 113                 free(olmp);
 114                 olmp = NULL;
 115                 return (NULL);
 116         }
 117 
 118         /*
 119          * Assign the output file name to be the initial object that got us
 120          * here.  This name is being used for diagnostic purposes only as we
 121          * don't actually generate an output file unless debugging is enabled.
 122          */
 123         ofl->ofl_name = name;
 124         NAME(olmp) = (char *)name;
 125         LIST(olmp) = lml;
 126 
 127         lm_append(lml, lmco, olmp);
 128         return (olmp);
 129 }
 130 
 131 /*
 132  * Define a structure to retain the mapping information of the original
 133  * relocatable object.  Typically, mmapobj(2) maps a relocatable object into one
 134  * mapping.  However, if padding has been enabled by a debugger, then additional
 135  * padding segments may have been added.  elf_obj_file() needs to know which
 136  * segment is the relocatable objects data, and retain the initial segment and
 137  * the associated segment number for unmapping this object later (see
 138  * elf_obj_fini()).  Note, even if padding is enabled, the final shared object
 139  * that is created by the link-editor for this relocatable object will have no
 140  * associated padding, as ld(1) has no capabilities to provide padding.
 141  */
 142 typedef struct {
 143         mmapobj_result_t        *md_mpp;
 144         uint_t                  md_mnum;
 145 } Mmap_desc;
 146 
 147 /*
 148  * Initial processing of a relocatable object.  If this is the first object
 149  * encountered we need to initialize some structures, then simply call the
 150  * link-edit functionality to provide the initial processing of the file (ie.
 151  * reads in sections and symbols, performs symbol resolution if more that one
 152  * object file have been specified, and assigns input sections to output
 153  * sections).
 154  */
 155 Rt_map *
 156 elf_obj_file(Lm_list *lml, Aliste lmco, Rt_map *clmp, const char *name,
 157     mmapobj_result_t *hmpp, mmapobj_result_t *mpp, uint_t mnum)
 158 {
 159         Rej_desc        rej;
 160         Mmap_desc       md;
 161 
 162         /*
 163          * If this is the first relocatable object (LD_PRELOAD could provide a
 164          * list of objects), initialize an input file descriptor and a link map.
 165          */
 166         if ((olmp == NULL) && ((olmp = elf_obj_init(lml, lmco, name)) == NULL))
 167                 return (NULL);
 168 
 169         DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
 170 
 171         /*
 172          * Keep track of the input image, as this must be free'd after all ELF
 173          * processing is completed.
 174          */
 175         md.md_mpp = mpp;
 176         md.md_mnum = mnum;
 177         if (alist_append(&mpalp, &md, sizeof (Mmap_desc),
 178             AL_CNT_MPOBJS) == NULL) {
 179                 remove_so(lml, olmp, clmp);
 180                 return (NULL);
 181         }
 182 
 183         /*
 184          * Pass the object mapping to the link-editor to commence processing the
 185          * file.
 186          */
 187         if (ld_process_mem(name, name, hmpp->mr_addr, hmpp->mr_msize,
 188             (Ofl_desc *)ELFPRV(olmp), &rej) == (Ifl_desc *)S_ERROR) {
 189                 remove_so(lml, olmp, clmp);
 190                 return (NULL);
 191         }
 192 
 193         return (olmp);
 194 }
 195 
 196 /*
 197  * Ensure any platform or machine capability names are valid.
 198  */
 199 inline static int
 200 check_plat_names(Syscapset *scapset, Alist *caps, Rej_desc *rej)
 201 {
 202         Capstr  *capstr;
 203         Aliste  idx;
 204 
 205         for (ALIST_TRAVERSE(caps, idx, capstr)) {
 206                 if (platcap_check(scapset, capstr->cs_str, rej) == 1)
 207                         return (1);
 208         }
 209         return (0);
 210 }
 211 
 212 inline static int
 213 check_mach_names(Syscapset *scapset, Alist *caps, Rej_desc *rej)
 214 {
 215         Capstr  *capstr;
 216         Aliste  idx;
 217 
 218         for (ALIST_TRAVERSE(caps, idx, capstr)) {
 219                 if (machcap_check(scapset, capstr->cs_str, rej) == 1)
 220                         return (1);
 221         }
 222         return (0);
 223 }
 224 
 225 /*
 226  * Finish relocatable object processing.  Having already initially processed one
 227  * or more objects, complete the generation of a shared object image by calling
 228  * the appropriate link-edit functionality (refer to sgs/ld/common/main.c).
 229  */
 230 Rt_map *
 231 elf_obj_fini(Lm_list *lml, Rt_map *lmp, Rt_map *clmp, int *in_nfavl)
 232 {
 233         Ofl_desc                *ofl = (Ofl_desc *)ELFPRV(lmp);
 234         Rt_map                  *nlmp, *tlmp;
 235         Ehdr                    *ehdr;
 236         Phdr                    *phdr;
 237         mmapobj_result_t        *mpp, *hmpp;
 238         uint_t                  phnum;
 239         int                     mnum;
 240         Lm_cntl                 *lmc;
 241         Aliste                  idx1;
 242         Mmap_desc               *mdp;
 243         Fdesc                   fd = { 0 };
 244         Grp_hdl                 *ghp;
 245         Rej_desc                rej = { 0 };
 246         Syscapset               *scapset;
 247         elfcap_mask_t           omsk;
 248         Alist                   *oalp;
 249 
 250         DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
 251 
 252         if (ld_reloc_init(ofl) == S_ERROR)
 253                 return (NULL);
 254         if (ld_sym_validate(ofl) == S_ERROR)
 255                 return (NULL);
 256 
 257         /*
 258          * At this point, all input section processing is complete.  If any
 259          * capabilities have been established, ensure that they are appropriate
 260          * for this system.
 261          */
 262         if (pnavl_recorded(&capavl, ofl->ofl_name, NULL, NULL))
 263                 scapset = alt_scapset;
 264         else
 265                 scapset = org_scapset;
 266 
 267         if ((((omsk = ofl->ofl_ocapset.oc_hw_1.cm_val) != 0) &&
 268             (hwcap1_check(scapset, omsk, &rej) == 0)) ||
 269             (((omsk = ofl->ofl_ocapset.oc_sf_1.cm_val) != 0) &&
 270             (sfcap1_check(scapset, omsk, &rej) == 0)) ||
 271             (((omsk = ofl->ofl_ocapset.oc_hw_2.cm_val) != 0) &&
 272             (hwcap2_check(scapset, omsk, &rej) == 0)) ||
 273             (((oalp = ofl->ofl_ocapset.oc_plat.cl_val) != NULL) &&
 274             (check_plat_names(scapset, oalp, &rej) == 0)) ||
 275             (((oalp = ofl->ofl_ocapset.oc_mach.cl_val) != NULL) &&
 276             (check_mach_names(scapset, oalp, &rej) == 0))) {
 277                 if ((lml_main.lm_flags & LML_FLG_TRC_LDDSTUB) && 
 278                     (FLAGS1(lmp) & FL1_RT_LDDSTUB) && (NEXT(lmp) == NULL)) {
 279                         /* LINTED */
 280                         (void) printf(MSG_INTL(ldd_reject[rej.rej_type]),
 281                             ofl->ofl_name, rej.rej_str);
 282                 }
 283                 return (NULL);
 284         }
 285 
 286         /*
 287          * Finish creating the output file.
 288          */
 289         if (ld_make_sections(ofl) == S_ERROR)
 290                 return (NULL);
 291         if (ld_create_outfile(ofl) == S_ERROR)
 292                 return (NULL);
 293         if (ld_update_outfile(ofl) == S_ERROR)
 294                 return (NULL);
 295         if (ld_reloc_process(ofl) == S_ERROR)
 296                 return (NULL);
 297 
 298         /*
 299          * At this point we have a memory image of the shared object.  The link
 300          * editor would normally simply write this to the required output file.
 301          * If we're debugging generate a standard temporary output file.
 302          */
 303         DBG_CALL(Dbg_file_output(ofl));
 304 
 305         /*
 306          * Allocate a mapping array to retain mapped segment information.
 307          */
 308         ehdr = ofl->ofl_nehdr;
 309         phdr = ofl->ofl_phdr;
 310 
 311         if ((mpp = hmpp = calloc(ehdr->e_phnum,
 312             sizeof (mmapobj_result_t))) == NULL)
 313                 return (NULL);
 314         for (mnum = 0, phnum = 0; phnum < ehdr->e_phnum; phnum++) {
 315                 if (phdr[phnum].p_type != PT_LOAD)
 316                         continue;
 317 
 318                 mpp[mnum].mr_addr = (caddr_t)((uintptr_t)phdr[phnum].p_vaddr +
 319                     (uintptr_t)ehdr);
 320                 mpp[mnum].mr_msize = phdr[phnum].p_memsz;
 321                 mpp[mnum].mr_fsize = phdr[phnum].p_filesz;
 322                 mpp[mnum].mr_prot = (PROT_READ | PROT_WRITE | PROT_EXEC);
 323                 mnum++;
 324         }
 325 
 326         /*
 327          * Generate a new link map representing the memory image created.
 328          */
 329         fd.fd_nname = ofl->ofl_name;
 330         if ((nlmp = elf_new_lmp(lml, CNTL(olmp), &fd, (Addr)hmpp->mr_addr,
 331             ofl->ofl_size, NULL, clmp, in_nfavl)) == NULL)
 332                 return (NULL);
 333 
 334         MMAPS(nlmp) = hmpp;
 335         MMAPCNT(nlmp) = mnum;
 336         PADSTART(nlmp) = (ulong_t)hmpp->mr_addr;
 337         PADIMLEN(nlmp) = mpp->mr_addr + mpp->mr_msize - hmpp->mr_addr;
 338 
 339         /*
 340          * Replace the original (temporary) link map with the new link map.
 341          */
 342         /* LINTED */
 343         lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, CNTL(nlmp));
 344         lml->lm_obj--;
 345 
 346         if ((tlmp = PREV_RT_MAP(nlmp)) == olmp)
 347                 tlmp = nlmp;
 348 
 349         if (PREV(olmp)) {
 350                 NEXT(PREV_RT_MAP(olmp)) = (Link_map *)nlmp;
 351                 PREV(nlmp) = PREV(olmp);
 352         } else {
 353                 PREV(nlmp) = NULL;
 354                 lmc->lc_head = nlmp;
 355                 if (CNTL(nlmp) == ALIST_OFF_DATA)
 356                         lml->lm_head = nlmp;
 357         }
 358 
 359         if (NEXT(olmp) != (Link_map *)nlmp) {
 360                 NEXT(nlmp) = NEXT(olmp);
 361                 PREV(NEXT_RT_MAP(olmp)) = (Link_map *)nlmp;
 362         }
 363 
 364         NEXT(tlmp) = NULL;
 365 
 366         lmc->lc_tail = tlmp;
 367         if (CNTL(nlmp) == ALIST_OFF_DATA)
 368                 lml->lm_tail = tlmp;
 369 
 370         HANDLES(nlmp) = HANDLES(olmp);
 371         GROUPS(nlmp) = GROUPS(olmp);
 372         STDEV(nlmp) = STDEV(olmp);
 373         STINO(nlmp) = STINO(olmp);
 374 
 375         FLAGS(nlmp) |= ((FLAGS(olmp) & ~FLG_RT_OBJECT) | FLG_RT_IMGALLOC);
 376         FLAGS1(nlmp) |= FLAGS1(olmp);
 377         MODE(nlmp) |= MODE(olmp);
 378 
 379         NAME(nlmp) = NAME(olmp);
 380 
 381         /*
 382          * Reassign any original handles to the new link-map.
 383          */
 384         for (APLIST_TRAVERSE(HANDLES(nlmp), idx1, ghp)) {
 385                 Grp_desc        *gdp;
 386                 Aliste          idx2;
 387 
 388                 ghp->gh_ownlmp = nlmp;
 389 
 390                 for (ALIST_TRAVERSE(ghp->gh_depends, idx2, gdp)) {
 391                         if (gdp->gd_depend == olmp) {
 392                                 gdp->gd_depend = nlmp;
 393                                 break;
 394                         }
 395                 }
 396         }
 397 
 398         ld_ofl_cleanup(ofl);
 399         free(ELFPRV(olmp));
 400         free(olmp);
 401         olmp = 0;
 402 
 403         /*
 404          * Unmap the original relocatable object.
 405          */
 406         for (ALIST_TRAVERSE(mpalp, idx1, mdp)) {
 407                 unmap_obj(mdp->md_mpp, mdp->md_mnum);
 408                 free(mdp->md_mpp);
 409         }
 410         free(mpalp);
 411         mpalp = NULL;
 412 
 413         /*
 414          * Now that we've allocated our permanent link map structure, expand the
 415          * PATHNAME() and insert this path name into the FullPathNode AVL tree.
 416          */
 417         (void) fullpath(nlmp, 0);
 418         if (fpavl_insert(lml, nlmp, PATHNAME(nlmp), 0) == 0)
 419                 return (NULL);
 420 
 421         /*
 422          * If we're being audited tell the audit library of the file we've just
 423          * opened.
 424          */
 425         if ((lml->lm_tflags | AFLAGS(nlmp)) & LML_TFLG_AUD_MASK) {
 426                 if (audit_objopen(nlmp, nlmp) == 0)
 427                         return (NULL);
 428         }
 429         return (nlmp);
 430 }