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