1 /* $NetBSD: dlfcn_elf.c,v 1.14 2017/07/11 15:21:35 joerg Exp $ */ 2 3 /* 4 * Copyright (c) 2000 Takuya SHIOZAKI 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 #if defined(LIBC_SCCS) && !defined(lint) 30 __RCSID("$NetBSD: dlfcn_elf.c,v 1.14 2017/07/11 15:21:35 joerg Exp $"); 31 #endif /* LIBC_SCCS and not lint */ 32 33 #include "namespace.h" 34 #include <sys/atomic.h> 35 #include <assert.h> 36 #include <elf.h> 37 #include <errno.h> 38 #include <string.h> 39 #include <stdbool.h> 40 41 #undef dlopen 42 #undef dlclose 43 #undef dlsym 44 #undef dlerror 45 #undef dladdr 46 #undef dfinfo 47 48 #define dlopen ___dlopen 49 #define dlclose ___dlclose 50 #define dlsym ___dlsym 51 #define dlvsym ___dlvsym 52 #define dlerror ___dlerror 53 #define dladdr ___dladdr 54 #define dlinfo ___dlinfo 55 #define dl_iterate_phdr ___dl_iterate_phdr 56 57 #define ELFSIZE ARCH_ELFSIZE 58 #include "rtld.h" 59 60 #ifdef __weak_alias 61 __weak_alias(dlopen,___dlopen) 62 __weak_alias(dlclose,___dlclose) 63 __weak_alias(dlsym,___dlsym) 64 __weak_alias(dlvsym,___dlvsym) 65 __weak_alias(dlerror,___dlerror) 66 __weak_alias(dladdr,___dladdr) 67 __weak_alias(dlinfo,___dlinfo) 68 __weak_alias(dl_iterate_phdr,___dl_iterate_phdr) 69 70 __weak_alias(__dlopen,___dlopen) 71 __weak_alias(__dlclose,___dlclose) 72 __weak_alias(__dlsym,___dlsym) 73 __weak_alias(__dlvsym,___dlvsym) 74 __weak_alias(__dlerror,___dlerror) 75 __weak_alias(__dladdr,___dladdr) 76 __weak_alias(__dlinfo,___dlinfo) 77 __weak_alias(__dl_iterate_phdr,___dl_iterate_phdr) 78 __weak_alias(__dl_cxa_refcount, ___dl_cxa_refcount) 79 #endif 80 81 /* 82 * For ELF, the dynamic linker directly resolves references to its 83 * services to functions inside the dynamic linker itself. These 84 * weak-symbol stubs are necessary so that "ld" won't complain about 85 * undefined symbols. The stubs are executed only when the program is 86 * linked statically, or when a given service isn't implemented in the 87 * dynamic linker. They must return an error if called, and they must 88 * be weak symbols so that the dynamic linker can override them. 89 */ 90 91 static char dlfcn_error[] = "Service unavailable"; 92 93 /*ARGSUSED*/ 94 void * 95 dlopen(const char *name, int mode) 96 { 97 98 return NULL; 99 } 100 101 /*ARGSUSED*/ 102 int 103 dlclose(void *fd) 104 { 105 106 return -1; 107 } 108 109 /*ARGSUSED*/ 110 void * 111 dlsym(void *handle, const char *name) 112 { 113 114 return NULL; 115 } 116 117 /*ARGSUSED*/ 118 void * 119 dlvsym(void *handle, const char *name, const char *version) 120 { 121 122 return NULL; 123 } 124 125 /*ARGSUSED*/ 126 __aconst char * 127 dlerror(void) 128 { 129 130 return dlfcn_error; 131 } 132 133 /*ARGSUSED*/ 134 int 135 dladdr(const void *addr, Dl_info *dli) 136 { 137 138 return 0; 139 } 140 141 /*ARGSUSED*/ 142 int 143 dlinfo(void *handle, int req, void *v) 144 { 145 146 return -1; 147 } 148 149 static const char *dlpi_name; 150 static Elf_Addr dlpi_addr; 151 static const Elf_Phdr *dlpi_phdr; 152 static Elf_Half dlpi_phnum; 153 154 static void 155 dl_iterate_phdr_setup(void) 156 { 157 const AuxInfo *aux; 158 159 _DIAGASSERT(_dlauxinfo() != NULL); 160 161 for (aux = _dlauxinfo(); aux->a_type != AT_NULL; ++aux) { 162 switch (aux->a_type) { 163 case AT_BASE: 164 dlpi_addr = aux->a_v; 165 break; 166 case AT_PHDR: 167 dlpi_phdr = (void *)aux->a_v; 168 break; 169 case AT_PHNUM: 170 _DIAGASSERT(__type_fit(Elf_Half, aux->a_v)); 171 dlpi_phnum = (Elf_Half)aux->a_v; 172 break; 173 case AT_SUN_EXECNAME: 174 dlpi_name = (void *)aux->a_v; 175 break; 176 } 177 } 178 } 179 180 /*ARGSUSED*/ 181 int 182 dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *), 183 void *data) 184 { 185 static bool setup_done; 186 struct dl_phdr_info phdr_info; 187 188 if (!setup_done) { 189 /* 190 * This can race on the first call to dl_iterate_phdr. 191 * dl_iterate_phdr_setup only touches field of pointer size 192 * and smaller and such stores are atomic. 193 */ 194 dl_iterate_phdr_setup(); 195 membar_producer(); 196 setup_done = true; 197 } 198 199 memset(&phdr_info, 0, sizeof(phdr_info)); 200 phdr_info.dlpi_addr = dlpi_addr; 201 phdr_info.dlpi_phdr = dlpi_phdr; 202 phdr_info.dlpi_phnum = dlpi_phnum; 203 phdr_info.dlpi_name = dlpi_name; 204 205 return callback(&phdr_info, sizeof(phdr_info), data); 206 } 207 208 void ___dl_cxa_refcount(void *, ssize_t); 209 210 /*ARGSUSED*/ 211 void 212 ___dl_cxa_refcount(void *dso_symbol, ssize_t delta) 213 { 214 } 215