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 * Finish relocatable object processing. Having already initially processed one 201 * or more objects, complete the generation of a shared object image by calling 202 * the appropriate link-edit functionality (refer to sgs/ld/common/main.c). 203 */ 204 Rt_map * 205 elf_obj_fini(Lm_list *lml, Rt_map *lmp, int *in_nfavl) 206 { 207 Ofl_desc *ofl = (Ofl_desc *)ELFPRV(lmp); 208 Rt_map *nlmp, *tlmp; 209 Ehdr *ehdr; 210 Phdr *phdr; 211 mmapobj_result_t *mpp, *hmpp; 212 uint_t phnum; 213 int mnum; 214 Lm_cntl *lmc; 215 Aliste idx1; 216 Mmap_desc *mdp; 217 Fdesc fd = { 0 }; 218 Grp_hdl *ghp; 219 Rej_desc rej = { 0 }; 220 elfcap_mask_t cap_value; 221 222 DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD)); 223 224 if (ld_reloc_init(ofl) == S_ERROR) 225 return (NULL); 226 if (ld_sym_validate(ofl) == S_ERROR) 227 return (NULL); 228 229 /* 230 * At this point, all input section processing is complete. If any 231 * hardware or software capabilities have been established, ensure that 232 * they are appropriate for this platform. 233 */ 234 cap_value = CAPMASK_VALUE(&ofl->ofl_ocapset.c_hw_1); 235 if (cap_value && (hwcap_check(cap_value, &rej) == 0)) { 236 if ((lml_main.lm_flags & LML_FLG_TRC_LDDSTUB) && lmp && 237 (FLAGS1(lmp) & FL1_RT_LDDSTUB) && (NEXT(lmp) == NULL)) { 238 (void) printf(MSG_INTL(MSG_LDD_GEN_HWCAP_1), 239 ofl->ofl_name, rej.rej_str); 240 } 241 return (NULL); 242 } 243 244 cap_value = CAPMASK_VALUE(&ofl->ofl_ocapset.c_sf_1); 245 if (cap_value && (sfcap_check(cap_value, &rej) == 0)) { 246 if ((lml_main.lm_flags & LML_FLG_TRC_LDDSTUB) && lmp && 247 (FLAGS1(lmp) & FL1_RT_LDDSTUB) && (NEXT(lmp) == NULL)) { 248 (void) printf(MSG_INTL(MSG_LDD_GEN_SFCAP_1), 249 ofl->ofl_name, rej.rej_str); 250 } 251 return (NULL); 252 } 253 254 /* 255 * Finish creating the output file. 256 */ 257 if (ld_make_sections(ofl) == S_ERROR) 258 return (NULL); 259 if (ld_create_outfile(ofl) == S_ERROR) 260 return (NULL); 261 if (ld_update_outfile(ofl) == S_ERROR) 262 return (NULL); 263 if (ld_reloc_process(ofl) == S_ERROR) 264 return (NULL); 265 266 /* 267 * At this point we have a memory image of the shared object. The link 268 * editor would normally simply write this to the required output file. 269 * If we're debugging generate a standard temporary output file. 270 */ 271 DBG_CALL(Dbg_file_output(ofl)); 272 273 /* 274 * Allocate a mapping array to retain mapped segment information. 275 */ 276 ehdr = ofl->ofl_nehdr; 277 phdr = ofl->ofl_phdr; 278 279 if ((mpp = hmpp = calloc(ehdr->e_phnum, 280 sizeof (mmapobj_result_t))) == NULL) 281 return (NULL); 282 for (mnum = 0, phnum = 0; phnum < ehdr->e_phnum; phnum++) { 283 if (phdr[phnum].p_type != PT_LOAD) 284 continue; 285 286 mpp[mnum].mr_addr = (caddr_t)((uintptr_t)phdr[phnum].p_vaddr + 287 (uintptr_t)ehdr); 288 mpp[mnum].mr_msize = phdr[phnum].p_memsz; 289 mpp[mnum].mr_fsize = phdr[phnum].p_filesz; 290 mpp[mnum].mr_prot = (PROT_READ | PROT_WRITE | PROT_EXEC); 291 mnum++; 292 } 293 294 /* 295 * Generate a new link map representing the memory image created. 296 */ 297 fd.fd_nname = ofl->ofl_name; 298 if ((nlmp = elf_new_lmp(lml, CNTL(olmp), &fd, (Addr)hmpp->mr_addr, 299 ofl->ofl_size, 0, in_nfavl)) == NULL) 300 return (NULL); 301 302 MMAPS(nlmp) = hmpp; 303 MMAPCNT(nlmp) = mnum; 304 PADSTART(nlmp) = (ulong_t)hmpp->mr_addr; 305 PADIMLEN(nlmp) = mpp->mr_addr + mpp->mr_msize - hmpp->mr_addr; 306 307 /* 308 * Replace the original (temporary) link map with the new link map. 309 */ 310 /* LINTED */ 311 lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, CNTL(nlmp)); 312 lml->lm_obj--; 313 314 if ((tlmp = PREV_RT_MAP(nlmp)) == olmp) 315 tlmp = nlmp; 316 317 if (PREV(olmp)) { 318 NEXT(PREV_RT_MAP(olmp)) = (Link_map *)nlmp; 319 PREV(nlmp) = PREV(olmp); 320 } else { 321 PREV(nlmp) = NULL; 322 lmc->lc_head = nlmp; 323 if (CNTL(nlmp) == ALIST_OFF_DATA) 324 lml->lm_head = nlmp; 325 } 326 327 if (NEXT(olmp) != (Link_map *)nlmp) { 328 NEXT(nlmp) = NEXT(olmp); 329 PREV(NEXT_RT_MAP(olmp)) = (Link_map *)nlmp; 330 } 331 332 NEXT(tlmp) = NULL; 333 334 lmc->lc_tail = tlmp; 335 if (CNTL(nlmp) == ALIST_OFF_DATA) 336 lml->lm_tail = tlmp; 337 338 HANDLES(nlmp) = HANDLES(olmp); 339 GROUPS(nlmp) = GROUPS(olmp); 340 STDEV(nlmp) = STDEV(olmp); 341 STINO(nlmp) = STINO(olmp); 342 343 FLAGS(nlmp) |= ((FLAGS(olmp) & ~FLG_RT_OBJECT) | FLG_RT_IMGALLOC); 344 FLAGS1(nlmp) |= FLAGS1(olmp); 345 MODE(nlmp) |= MODE(olmp); 346 347 NAME(nlmp) = NAME(olmp); 348 349 /* 350 * Reassign any original handles to the new link-map. 351 */ 352 for (APLIST_TRAVERSE(HANDLES(nlmp), idx1, ghp)) { 353 Grp_desc *gdp; 354 Aliste idx2; 355 356 ghp->gh_ownlmp = nlmp; 357 358 for (ALIST_TRAVERSE(ghp->gh_depends, idx2, gdp)) { 359 if (gdp->gd_depend == olmp) { 360 gdp->gd_depend = nlmp; 361 break; 362 } 363 } 364 } 365 366 ld_ofl_cleanup(ofl); 367 free(ELFPRV(olmp)); 368 free(olmp); 369 olmp = 0; 370 371 /* 372 * Unmap the original relocatable object. 373 */ 374 for (ALIST_TRAVERSE(mpalp, idx1, mdp)) { 375 unmap_obj(mdp->md_mpp, mdp->md_mnum); 376 free(mdp->md_mpp); 377 } 378 free(mpalp); 379 mpalp = NULL; 380 381 /* 382 * Now that we've allocated our permanent link map structure, expand the 383 * PATHNAME() and insert this path name into the FullPathNode AVL tree. 384 */ 385 (void) fullpath(nlmp, 0); 386 if (fpavl_insert(lml, nlmp, PATHNAME(nlmp), 0) == 0) 387 return (NULL); 388 389 /* 390 * If we're being audited tell the audit library of the file we've just 391 * opened. 392 */ 393 if ((lml->lm_tflags | AFLAGS(nlmp)) & LML_TFLG_AUD_MASK) { 394 if (audit_objopen(lmp, lmp) == 0) 395 return (NULL); 396 } 397 return (nlmp); 398 } 399