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