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) 1994, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 26 #include <stdio.h> 27 #include <dlfcn.h> 28 #include <libelf.h> 29 #include <link.h> 30 #include <debug.h> 31 #include "msg.h" 32 #include "_libld.h" 33 34 /* 35 * Table which defines the default functions to be called by the library 36 * SUPPORT (-S <libname>). These functions can be redefined by the 37 * ld_support_loadso() routine. 38 */ 39 static Support_list support[LDS_NUM] = { 40 {MSG_ORIG(MSG_SUP_VERSION), NULL}, /* LDS_VERSION */ 41 {MSG_ORIG(MSG_SUP_INPUT_DONE), NULL}, /* LDS_INPUT_DONE */ 42 #if defined(_ELF64) 43 {MSG_ORIG(MSG_SUP_START_64), NULL}, /* LDS_START */ 44 {MSG_ORIG(MSG_SUP_ATEXIT_64), NULL}, /* LDS_ATEXIT */ 45 {MSG_ORIG(MSG_SUP_OPEN_64), NULL}, /* LDS_OPEN */ 46 {MSG_ORIG(MSG_SUP_FILE_64), NULL}, /* LDS_FILE */ 47 {MSG_ORIG(MSG_SUP_INSEC_64), NULL}, /* LDS_INSEC */ 48 {MSG_ORIG(MSG_SUP_SEC_64), NULL} /* LDS_SEC */ 49 #else /* Elf32 */ 50 {MSG_ORIG(MSG_SUP_START), NULL}, /* LDS_START */ 51 {MSG_ORIG(MSG_SUP_ATEXIT), NULL}, /* LDS_ATEXIT */ 52 {MSG_ORIG(MSG_SUP_OPEN), NULL}, /* LDS_OPEN */ 53 {MSG_ORIG(MSG_SUP_FILE), NULL}, /* LDS_FILE */ 54 {MSG_ORIG(MSG_SUP_INSEC), NULL}, /* LDS_INSEC */ 55 {MSG_ORIG(MSG_SUP_SEC), NULL} /* LDS_SEC */ 56 #endif 57 }; 58 59 /* 60 * Loads in a support shared object specified using the SGS_SUPPORT environment 61 * variable or the -S ld option, and determines which interface functions are 62 * provided by that object. 63 */ 64 uintptr_t 65 ld_sup_loadso(Ofl_desc *ofl, const char *obj) 66 { 67 void *handle, *fptr; 68 uint_t interface, version = LD_SUP_VERSION1; 69 70 /* 71 * Load the required support library. If we are unable to load it fail 72 * with a fatal error. 73 */ 74 if ((handle = dlopen(obj, (RTLD_LAZY | RTLD_FIRST))) == NULL) { 75 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SUP_NOLOAD), 76 obj, dlerror()); 77 return (S_ERROR); 78 } 79 80 for (interface = 0; interface < LDS_NUM; interface++) { 81 Func_list fl; 82 83 if ((fptr = dlsym(handle, support[interface].sup_name)) == NULL) 84 continue; 85 86 DBG_CALL(Dbg_support_load(ofl->ofl_lml, obj, 87 support[interface].sup_name)); 88 89 if (interface == LDS_VERSION) { 90 DBG_CALL(Dbg_support_action(ofl->ofl_lml, obj, 91 support[LDS_VERSION].sup_name, LDS_VERSION, 0)); 92 93 version = ((uint_t(*)())fptr)(LD_SUP_VCURRENT); 94 95 /* 96 * If version is LD_SUP_VNONE, unload the support 97 * library and act as if it was never requested. 98 * This provides the support library with a mechanism 99 * for opting out of the process. 100 * 101 * Note that this depends on LDS_VERSION being the 102 * first item in support[]. If this were not true, 103 * then other functions from the library might be 104 * entered into the function lists, and unloading 105 * the object would cause corruption. 106 */ 107 assert(LDS_VERSION == 0); 108 if (version == LD_SUP_VNONE) { 109 DBG_CALL(Dbg_support_vnone(ofl->ofl_lml, 110 obj)); 111 (void) dlclose(handle); 112 return (1); 113 } 114 115 /* 116 * If the support library has a version higher 117 * than we support, we are unable to accept it. 118 */ 119 if (version > LD_SUP_VCURRENT) { 120 ld_eprintf(ofl, ERR_FATAL, 121 MSG_INTL(MSG_SUP_BADVERSION), obj, 122 LD_SUP_VCURRENT, version); 123 (void) dlclose(handle); 124 return (S_ERROR); 125 } 126 } 127 128 fl.fl_obj = obj; 129 fl.fl_fptr = fptr; 130 fl.fl_version = version; 131 if (alist_append(&support[interface].sup_funcs, &fl, 132 sizeof (Func_list), AL_CNT_SUPPORT) == NULL) 133 return (S_ERROR); 134 } 135 return (1); 136 } 137 138 /* 139 * Wrapper routines for the ld support library calls. 140 */ 141 void 142 ld_sup_start(Ofl_desc *ofl, const Half etype, const char *caller) 143 { 144 Func_list *flp; 145 Aliste idx; 146 147 for (ALIST_TRAVERSE(support[LDS_START].sup_funcs, idx, flp)) { 148 DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj, 149 support[LDS_START].sup_name, LDS_START, ofl->ofl_name)); 150 (*flp->fl_fptr)(ofl->ofl_name, etype, caller); 151 } 152 } 153 154 void 155 ld_sup_atexit(Ofl_desc *ofl, int ecode) 156 { 157 Func_list *flp; 158 Aliste idx; 159 160 for (ALIST_TRAVERSE(support[LDS_ATEXIT].sup_funcs, idx, flp)) { 161 DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj, 162 support[LDS_ATEXIT].sup_name, LDS_ATEXIT, 0)); 163 (*flp->fl_fptr)(ecode); 164 } 165 } 166 167 void 168 ld_sup_open(Ofl_desc *ofl, const char **opath, const char **ofile, int *ofd, 169 int flags, Elf **oelf, Elf *ref, size_t off, const Elf_Kind ekind) 170 { 171 Func_list *flp; 172 Aliste idx; 173 174 for (ALIST_TRAVERSE(support[LDS_OPEN].sup_funcs, idx, flp)) { 175 const char *npath = *opath; 176 const char *nfile = *ofile; 177 Elf *nelf = *oelf; 178 int nfd = *ofd; 179 int _flags = 0; 180 181 /* 182 * This interface was introduced in VERSION3. Only call this 183 * function for libraries reporting support for version 3 or 184 * above. 185 */ 186 if (flp->fl_version < LD_SUP_VERSION3) 187 continue; 188 189 if (!(flags & FLG_IF_CMDLINE)) 190 _flags |= LD_SUP_DERIVED; 191 if (!(flags & FLG_IF_NEEDED)) 192 _flags |= LD_SUP_INHERITED; 193 if (flags & FLG_IF_EXTRACT) 194 _flags |= LD_SUP_EXTRACTED; 195 196 /* 197 * If the present object is an extracted archive member, make 198 * sure the archive offset is reset so that the caller can 199 * obtain an ELF descriptor to the same member (an elf_begin() 200 * moves the offset to the next member). 201 */ 202 if (flags & FLG_IF_EXTRACT) 203 (void) elf_rand(ref, off); 204 205 DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj, 206 support[LDS_OPEN].sup_name, LDS_OPEN, *opath)); 207 (*flp->fl_fptr)(&npath, &nfile, &nfd, _flags, &nelf, ref, off, 208 ekind); 209 210 /* 211 * If the file descriptor, ELF descriptor, or file names have 212 * been modified, then diagnose the differences and return the 213 * new data. As a basic test, make sure the support library 214 * hasn't nulled out data ld(1) will try and dereference. 215 */ 216 if ((npath != *opath) || (nfd != *ofd) || (nelf != *oelf)) { 217 DBG_CALL(Dbg_file_modified(ofl->ofl_lml, flp->fl_obj, 218 *opath, npath, *ofd, nfd, *oelf, nelf)); 219 if (npath) 220 *opath = npath; 221 if (nfile) 222 *ofile = nfile; 223 *ofd = nfd; 224 *oelf = nelf; 225 } 226 } 227 } 228 229 void 230 ld_sup_file(Ofl_desc *ofl, const char *ifile, const Elf_Kind ekind, int flags, 231 Elf *elf) 232 { 233 Func_list *flp; 234 Aliste idx; 235 236 for (ALIST_TRAVERSE(support[LDS_FILE].sup_funcs, idx, flp)) { 237 int _flags = 0; 238 239 if (!(flags & FLG_IF_CMDLINE)) 240 _flags |= LD_SUP_DERIVED; 241 if (!(flags & FLG_IF_NEEDED)) 242 _flags |= LD_SUP_INHERITED; 243 if (flags & FLG_IF_EXTRACT) 244 _flags |= LD_SUP_EXTRACTED; 245 246 DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj, 247 support[LDS_FILE].sup_name, LDS_FILE, ifile)); 248 (*flp->fl_fptr)(ifile, ekind, _flags, elf); 249 } 250 } 251 252 uintptr_t 253 ld_sup_input_section(Ofl_desc *ofl, Ifl_desc *ifl, const char *sname, 254 Shdr **oshdr, Word ndx, Elf_Scn *scn, Elf *elf) 255 { 256 Func_list *flp; 257 Aliste idx; 258 uint_t flags = 0; 259 Elf_Data *data = NULL; 260 261 for (ALIST_TRAVERSE(support[LDS_INSEC].sup_funcs, idx, flp)) { 262 Shdr *nshdr = *oshdr; 263 264 /* 265 * This interface was introduced in VERSION2. Only call this 266 * function for libraries reporting support for version 2 or 267 * above. 268 */ 269 if (flp->fl_version < LD_SUP_VERSION2) 270 continue; 271 272 if ((data == NULL) && 273 ((data = elf_getdata(scn, NULL)) == NULL)) { 274 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETDATA), 275 ifl->ifl_name); 276 return (S_ERROR); 277 } 278 279 DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj, 280 support[LDS_INSEC].sup_name, LDS_INSEC, sname)); 281 (*flp->fl_fptr)(sname, &nshdr, ndx, data, elf, &flags); 282 283 /* 284 * If the section header has been re-allocated (known to occur 285 * with libCCexcept.so), then diagnose the section header 286 * difference and return the new section header. 287 */ 288 if (nshdr != *oshdr) { 289 Ehdr *ehdr = ifl->ifl_ehdr; 290 291 DBG_CALL(Dbg_shdr_modified(ofl->ofl_lml, flp->fl_obj, 292 ehdr->e_ident[EI_OSABI], ehdr->e_machine, ndx, 293 *oshdr, nshdr, sname)); 294 *oshdr = nshdr; 295 } 296 } 297 return (0); 298 } 299 300 void 301 ld_sup_section(Ofl_desc *ofl, const char *scn, Shdr *shdr, Word ndx, 302 Elf_Data *data, Elf *elf) 303 { 304 Func_list *flp; 305 Aliste idx; 306 307 for (ALIST_TRAVERSE(support[LDS_SEC].sup_funcs, idx, flp)) { 308 DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj, 309 support[LDS_SEC].sup_name, LDS_SEC, scn)); 310 (*flp->fl_fptr)(scn, shdr, ndx, data, elf); 311 } 312 } 313 314 void 315 ld_sup_input_done(Ofl_desc *ofl) 316 { 317 Func_list *flp; 318 Aliste idx; 319 uint_t flags = 0; 320 321 for (ALIST_TRAVERSE(support[LDS_INPUT_DONE].sup_funcs, idx, flp)) { 322 /* 323 * This interface was introduced in VERSION2. Only call this 324 * function for libraries reporting support for version 2 or 325 * above. 326 */ 327 if (flp->fl_version < LD_SUP_VERSION2) 328 continue; 329 330 DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj, 331 support[LDS_INPUT_DONE].sup_name, LDS_INPUT_DONE, 0)); 332 (*flp->fl_fptr)(&flags); 333 } 334 } 335